Java for Web学习笔记(四六):WebSocket(3)Java Server

Maven相关库


    javax.websocket
    javax.websocket-api
    1.1
    provided

注意,这里是provided,不是compiled,已经集成在JavaEE 7中。上面是支持WebSocket Server和Client的,如果我们只需要Client,可以使用:


    javax.websocket
    javax.websocket-client-api
    1.1
    provided

一个简单的WebSocket Server

承接之前WebSocket Client的小例子,server在收到WebSocket请求后发出Hello消息,然后采用异步方式,每隔1秒发送一个消息,连发三次,然后关闭连接。

//【1】进行websocket server endpoint的标注,给出匹配的url。当收到一个webSocket请求时,会创建一个实例,这点和Servlet不一样,需要注意。
@ServerEndpoint("/test/{id}")
public class TestServer {
    //【2】server和client握手后,同样提供了@OnOpen, @OnClose, @OnError和@OnMessage触发发放,这些方法可以通过@PathParam()来获取path的信息,如本例中的id。我们可以通过javax.websocket.Session来发送信息和关闭连接

    //【2.1】可选的Session参数,可选的EndpointConfig参数,以及path信息
    @OnOpen
    public void onOpen(Session session, @PathParam("id") String id){
        System.out.println("onOpen : (" + id + ")");
        try {
            session.getBasicRemote().sendText("Hello, " + id);

            new Thread(new Runnable() {
                public void run() {
                    for(int i = 0 ; i < 3 ; i ++){
                        try {
                            Thread.sleep(1000);
                            session.getBasicRemote().sendText("count " + i);
                        } catch (Exception e) {
                        }                        
                    }
                   try {
                       session.close();
                   } catch (IOException ignoreE) {
                   }
               }
            }).start();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }

    //【2.2】消息的参数会比较复杂
    //    1、String给出text消息,如本例
    //    2、String以及boolean表示chunks,true表示为最后一个chunks。
    //    3、One Java primitive or primitive wrapper to receive an entire text message converted to that type
    //    4、One java.io.Reader to receive a text message as a blocking stream
    //    5、One byte[] or one java.nio.ByteBuffer to receive an entire binary message
    //    6、One byte[] or one ByteBuffer, plus one boolean to receive a binary message in chunks
    //    7、One java.io.InputStream to receive a binary message as a blocking stream
    //    8、One PongMessage for custom handling of heartbeat responses
    //    9、Any Java object if the endpoint has a Decoder.Text, Decoder.Binary, Decoder .TextStream, or Decoder.BinaryStream registered to convert that type. The message type of text or binary must match the registered decoder.
    @OnMessage
    public void onMessage(Session session, String message, @PathParam("id") String id){
        System.out.println("onMessage : (" + id + ") " + message);
    }

    //【2.3】可选的Session参数,可选的CloseReason参数,以及path信息
    @OnClose
    public void onClose(Session session, @PathParam("id") String id){
        System.out.println("onClose : (" + id + ")");
    }

    //【2.4】@OnError提供可选的Session参数,必选的Throwable,以及path信息,本例无处理
}

更为复杂一点的处理


两个client之间的通信

server可以作为agent之类的,实现两个或者多个client之间的通信,方式很简单,将session关联即可。

@ServerEndpoint("/ticTacToe/{gameId}/{username}")
public class GameServer {
    private static class Game{
        public long gameId;
        public Session player1;
        public Session player2;
        public TicTacToeGame ticTacToeGame;    
    }

    //我们通过gameId可以获得两个关联的client的session。
    private static Map games = new Hashtable<>();

    ... 略 ...
}

使用JSON作为Message

Gson是一个选择,我们也可以使用可能更为传统的方式:

    
        com.fasterxml.jackson.core
        jackson-core
        2.8.7
        compile
    

    
        com.fasterxml.jackson.core
        jackson-annotations
        2.8.7
        compile
    

    
        com.fasterxml.jackson.core
        jackson-databind
        2.8.7
        compile
    

    
        com.fasterxml.jackson.datatype
        jackson-datatype-jsr310
        2.8.7
        compile
    
@ServerEndpoint("/ticTacToe/{gameId}/{username}")
public class GameServer {
    ... 见上面代码片段,略 ...

    //(1)消息的基础格式
    public static abstract class Message{
        private final String action;
        public Message(String action){
            this.action = action;
        }
        public String getAction() {
            return action;
        }        
    }

    //(2)具体的消息类型
    public static class GameStartedMessage extends Message{
        private final TicTacToeGame game;

        public GameStartedMessage(TicTacToeGame game) {
            super("gameStarted");
            this.game = game;
        }

        public TicTacToeGame getGame() {
            return game;
        }        
    }

    ... 其他的消息类型,略 ...
    //【1】定义json封装
    private static ObjectMapper mapper = new ObjectMapper();

    //【2】发送JSON消息
    private void sendJsonMessage(Session session, Game game, Message message){
        try {
            session.getBasicRemote().sendText(GameServer.mapper.writeValueAsString(message));
        } catch (Exception e) {
            handleException(e,game);
        } 
    }

    //【3】解析JSON消息
    @OnMessage
    public void onMessage(Session session, String message, @PathParam("gameId") long gameId){
        Game game = GameServer.games.get(gameId);
        ... ...
        //解析JSON
        MoveMessage move = GameServer.mapper.readValue(message, MoveMessage.class);
        ... ...
        //发送JSON
        this.sendJsonMessage((isPlayer1 ? game.player2 : game.player1), game,
                    new OpponentMadeMoveMessage(move));
    }
}

从session中获取信息

在jsp文件中,我们用javascript写到

server = new WebSocket('ws://' + window.location.host +
                            ''
                            +''
                            +'');

我们要读出握手的HTTP请求的相关信息,例如action参数的值

//在WebSocket server中,如果我们需要获取action的信息
//getRequestParameterMap:Return the request parameters associated with the request this session was opened under.
List actions = session.getRequestParameterMap().get("action");
if(actions != null && actions.size() == 1){
    String action = actions.get(0);
    ... ...
}

相关链接: 我的Professional Java for Web Applications相关文章

你可能感兴趣的:(JAVA,读书笔记,愷风(Wei)之Java,for,Web学习笔记)