Java for Web学习笔记(四六):WebSocket(3)Java Server
来源:互联网 发布:芈月到底爱谁知乎 编辑:程序博客网 时间:2024/06/02 00:04
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相关文章
0 0
- Java for Web学习笔记(四六):WebSocket(3)Java Server
- Java for Web学习笔记(四七):WebSocket(4)Java Client和二进制消息
- Java for Web学习笔记(四四):WebSocket(1)演化历程
- Java for Web学习笔记(四五):WebSocket(2)JavaScript Client
- Java for Web学习笔记(四八):WebSocket(5)encoder,decoder和configurator
- Java for Web学习笔记(九十):消息和集群(5)利用websocket实现订阅和发布(上)
- Java for Web学习笔记(七二):Service和Repository(7)在Spring框架中使用WebSocket
- Java for Web学习笔记(二):Web Containers
- Java for Web学习笔记(九一):消息和集群(6)利用websocket实现订阅和发布(下)
- Java for Web学习笔记(二七):JSTL(3)Core Tag(中)
- Java for Web学习笔记(五):Servlet(3)HttpServletRequest
- Java for Web学习笔记(十三):JSP(3)Directive
- Java for Web学习笔记(十九):Session(3)Session Listener
- Java for Web学习笔记(二三):EL(3)EL的视图
- Java for Web学习笔记(三五):自定义tag(3)TLDS和Tag Handler
- Java for Web学习笔记(四一):Filter(3)用于Log
- Java for Web学习笔记(五一):Log(3)代码中使用log4j2
- Java for Web学习笔记(六一):Controller替代Servlet(3)body映射到参数
- mybatis 详解
- 【unity&游戏策划】技能 等级与经验值的 几种曲线方程式
- 快速排序lua实现 递归和栈两种实现
- 数据结构之二叉树(二叉查找树)
- Lua快速排序算法+代码
- Java for Web学习笔记(四六):WebSocket(3)Java Server
- 2017安卓面试题-JAVA篇
- dubbo的架构
- 数字游戏
- Java使用JNI调用C/C++(windows和android平台)
- AC之uva750(代码改编自167)
- Hadoop分布式文件系统(1)
- C++简单排序算法之冒泡排序
- BZOJ P1430小猴打架