Java for Web学习笔记(四六):WebSocket(3)Java Server
2017-03-25 18:49
495 查看
Maven相关库
<dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency>
注意,这里是provided,不是compiled,已经集成在JavaEE 7中。上面是支持WebSocket Server和Client的,如果我们只需要Client,可以使用:
<dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-client-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency>
一个简单的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<Long, Game> games = new Hashtable<>(); ... 略 ... }
使用JSON作为Message
Gson是一个选择,我们也可以使用可能更为传统的方式:<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.7</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.7</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.8.7</version> <scope>compile</scope> </dependency>
@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 + '<c:url value="/game/${gameId}/${username}">' +'<c:param name="action" value="${action}" />' +'</c:url>');
我们要读出握手的HTTP请求的相关信息,例如action参数的值
//在WebSocket server中,如果我们需要获取action的信息 //getRequestParameterMap:Return the request parameters associated with the request this session was opened under. List<String> actions = session.getRequestParameterMap().get("action"); if(actions != null && actions.size() == 1){ String action = actions.get(0); ... ... }
相关链接:
我的Professional Java for Web Applications相关文章
相关文章推荐
- Java for Web学习笔记(四七):WebSocket(4)Java Client和二进制消息
- Java Web 学习笔记 The origin server did not find a current representation for the target resource or is
- Java for Web学习笔记(九一):消息和集群(6)利用websocket实现订阅和发布(下)
- Java for Web学习笔记(四四):WebSocket(1)演化历程
- Java for Web学习笔记(四五):WebSocket(2)JavaScript Client
- Java for Web学习笔记(二十):Session(4)在集群中使用Session
- Java for Web学习笔记(四二):Filter(4)用于压缩
- Java for Web学习笔记(五六):Spring框架简介(5)自动识别
- Java for Web学习笔记(十七):Session(1)Session的携带
- Java for Web学习笔记(六十):Controller替代Servlet(2)方法中的参数
- Java for Web学习笔记(五一):Log(3)代码中使用log4j2
- Java for Web学习笔记(二九):JSTL(5)FMT Tag(上)
- Java for Web学习笔记(八):Servlet(6)doGet()和doPost()是线程还是队列
- Java for Web学习笔记(三八):自定义tag(6)一些注意
- Java for Web学习笔记(十四):JSP(4)JSP Tag
- Java for Web学习笔记(四一):Filter(3)用于Log
- Java for Web学习笔记(二五):JSTL(1)使用JSTL
- Java for Web学习笔记(五七):Spring框架简介(6)代码设置
- Java for Web学习笔记(二四):EL(4)流(Stream)
- Java for Web学习笔记(二六):JSTL(2)Core Tag(上)