You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # spring-boot-demo-websocket-socketio
  2. > 此 demo 主要演示了 Spring Boot 如何使用 `netty-socketio` 集成 WebSocket,实现一个简单的聊天室。
  3. ## 1. 代码
  4. ### 1.1. pom.xml
  5. ```xml
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  9. <modelVersion>4.0.0</modelVersion>
  10. <artifactId>spring-boot-demo-websocket-socketio</artifactId>
  11. <version>1.0.0-SNAPSHOT</version>
  12. <packaging>jar</packaging>
  13. <name>spring-boot-demo-websocket-socketio</name>
  14. <description>Demo project for Spring Boot</description>
  15. <parent>
  16. <groupId>com.xkcoding</groupId>
  17. <artifactId>spring-boot-demo</artifactId>
  18. <version>1.0.0-SNAPSHOT</version>
  19. </parent>
  20. <properties>
  21. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  22. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  23. <java.version>1.8</java.version>
  24. <netty-socketio.version>1.7.16</netty-socketio.version>
  25. </properties>
  26. <dependencies>
  27. <dependency>
  28. <groupId>com.corundumstudio.socketio</groupId>
  29. <artifactId>netty-socketio</artifactId>
  30. <version>${netty-socketio.version}</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-web</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-configuration-processor</artifactId>
  39. <optional>true</optional>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46. <dependency>
  47. <groupId>cn.hutool</groupId>
  48. <artifactId>hutool-all</artifactId>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.projectlombok</groupId>
  52. <artifactId>lombok</artifactId>
  53. <optional>true</optional>
  54. </dependency>
  55. </dependencies>
  56. <build>
  57. <finalName>spring-boot-demo-websocket-socketio</finalName>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-maven-plugin</artifactId>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>
  66. ```
  67. ### 1.2. ServerConfig.java
  68. > websocket服务器配置,包括服务器IP、端口信息、以及连接认证等配置
  69. ```java
  70. /**
  71. * <p>
  72. * websocket服务器配置
  73. * </p>
  74. *
  75. * @author yangkai.shen
  76. * @date Created in 2018-12-18 16:42
  77. */
  78. @Configuration
  79. @EnableConfigurationProperties({WsConfig.class})
  80. public class ServerConfig {
  81. @Bean
  82. public SocketIOServer server(WsConfig wsConfig) {
  83. com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
  84. config.setHostname(wsConfig.getHost());
  85. config.setPort(wsConfig.getPort());
  86. //这个listener可以用来进行身份验证
  87. config.setAuthorizationListener(data -> {
  88. // http://localhost:8081?token=xxxxxxx
  89. // 例如果使用上面的链接进行connect,可以使用如下代码获取用户密码信息,本文不做身份验证
  90. String token = data.getSingleUrlParam("token");
  91. // 校验token的合法性,实际业务需要校验token是否过期等等,参考 spring-boot-demo-rbac-security 里的 JwtUtil
  92. // 如果认证不通过会返回一个 Socket.EVENT_CONNECT_ERROR 事件
  93. return StrUtil.isNotBlank(token);
  94. });
  95. return new SocketIOServer(config);
  96. }
  97. /**
  98. * Spring 扫描自定义注解
  99. */
  100. @Bean
  101. public SpringAnnotationScanner springAnnotationScanner(SocketIOServer server) {
  102. return new SpringAnnotationScanner(server);
  103. }
  104. }
  105. ```
  106. ### 1.3. MessageEventHandler.java
  107. > 核心事件处理类,主要处理客户端发起的消息事件,以及主动往客户端发起事件
  108. ```java
  109. /**
  110. * <p>
  111. * 消息事件处理
  112. * </p>
  113. *
  114. * @author yangkai.shen
  115. * @date Created in 2018-12-18 18:57
  116. */
  117. @Component
  118. @Slf4j
  119. public class MessageEventHandler {
  120. @Autowired
  121. private SocketIOServer server;
  122. @Autowired
  123. private DbTemplate dbTemplate;
  124. /**
  125. * 添加connect事件,当客户端发起连接时调用
  126. *
  127. * @param client 客户端对象
  128. */
  129. @OnConnect
  130. public void onConnect(SocketIOClient client) {
  131. if (client != null) {
  132. String token = client.getHandshakeData().getSingleUrlParam("token");
  133. // 模拟用户id 和token一致
  134. String userId = client.getHandshakeData().getSingleUrlParam("token");
  135. UUID sessionId = client.getSessionId();
  136. dbTemplate.save(userId, sessionId);
  137. log.info("连接成功,【token】= {},【sessionId】= {}", token, sessionId);
  138. } else {
  139. log.error("客户端为空");
  140. }
  141. }
  142. /**
  143. * 添加disconnect事件,客户端断开连接时调用,刷新客户端信息
  144. *
  145. * @param client 客户端对象
  146. */
  147. @OnDisconnect
  148. public void onDisconnect(SocketIOClient client) {
  149. if (client != null) {
  150. String token = client.getHandshakeData().getSingleUrlParam("token");
  151. // 模拟用户id 和token一致
  152. String userId = client.getHandshakeData().getSingleUrlParam("token");
  153. UUID sessionId = client.getSessionId();
  154. dbTemplate.deleteByUserId(userId);
  155. log.info("客户端断开连接,【token】= {},【sessionId】= {}", token, sessionId);
  156. client.disconnect();
  157. } else {
  158. log.error("客户端为空");
  159. }
  160. }
  161. /**
  162. * 加入群聊
  163. *
  164. * @param client 客户端
  165. * @param request 请求
  166. * @param data 群聊
  167. */
  168. @OnEvent(value = Event.JOIN)
  169. public void onJoinEvent(SocketIOClient client, AckRequest request, JoinRequest data) {
  170. log.info("用户:{} 已加入群聊:{}", data.getUserId(), data.getGroupId());
  171. client.joinRoom(data.getGroupId());
  172. server.getRoomOperations(data.getGroupId()).sendEvent(Event.JOIN, data);
  173. }
  174. @OnEvent(value = Event.CHAT)
  175. public void onChatEvent(SocketIOClient client, AckRequest request, SingleMessageRequest data) {
  176. Optional<UUID> toUser = dbTemplate.findByUserId(data.getToUid());
  177. if (toUser.isPresent()) {
  178. log.info("用户 {} 刚刚私信了用户 {}:{}", data.getFromUid(), data.getToUid(), data.getMessage());
  179. sendToSingle(toUser.get(), data);
  180. client.sendEvent(Event.CHAT_RECEIVED, "发送成功");
  181. } else {
  182. client.sendEvent(Event.CHAT_REFUSED, "发送失败,对方不想理你");
  183. }
  184. }
  185. @OnEvent(value = Event.GROUP)
  186. public void onGroupEvent(SocketIOClient client, AckRequest request, GroupMessageRequest data) {
  187. Collection<SocketIOClient> clients = server.getRoomOperations(data.getGroupId()).getClients();
  188. boolean inGroup = false;
  189. for (SocketIOClient socketIOClient : clients) {
  190. if (ObjectUtil.equal(socketIOClient.getSessionId(), client.getSessionId())) {
  191. inGroup = true;
  192. break;
  193. }
  194. }
  195. if (inGroup) {
  196. log.info("群号 {} 收到来自 {} 的群聊消息:{}", data.getGroupId(), data.getFromUid(), data.getMessage());
  197. sendToGroup(data);
  198. } else {
  199. request.sendAckData("请先加群!");
  200. }
  201. }
  202. /**
  203. * 单聊
  204. */
  205. public void sendToSingle(UUID sessionId, SingleMessageRequest message) {
  206. server.getClient(sessionId).sendEvent(Event.CHAT, message);
  207. }
  208. /**
  209. * 广播
  210. */
  211. public void sendToBroadcast(BroadcastMessageRequest message) {
  212. log.info("系统紧急广播一条通知:{}", message.getMessage());
  213. for (UUID clientId : dbTemplate.findAll()) {
  214. if (server.getClient(clientId) == null) {
  215. continue;
  216. }
  217. server.getClient(clientId).sendEvent(Event.BROADCAST, message);
  218. }
  219. }
  220. /**
  221. * 群聊
  222. */
  223. public void sendToGroup(GroupMessageRequest message) {
  224. server.getRoomOperations(message.getGroupId()).sendEvent(Event.GROUP, message);
  225. }
  226. }
  227. ```
  228. ### 1.4. ServerRunner.java
  229. > websocket 服务器启动类
  230. ```java
  231. /**
  232. * <p>
  233. * websocket服务器启动
  234. * </p>
  235. *
  236. * @author yangkai.shen
  237. * @date Created in 2018-12-18 17:07
  238. */
  239. @Component
  240. @Slf4j
  241. public class ServerRunner implements CommandLineRunner {
  242. @Autowired
  243. private SocketIOServer server;
  244. @Override
  245. public void run(String... args) {
  246. server.start();
  247. log.info("websocket 服务器启动成功。。。");
  248. }
  249. }
  250. ```
  251. ## 2. 运行方式
  252. 1. 启动 `SpringBootDemoWebsocketSocketioApplication.java`
  253. 2. 使用不同的浏览器,访问 http://localhost:8080/demo/index.html
  254. ## 3. 运行效果
  255. **浏览器1:**![image-20181219152318079](http://static.xkcoding.com/spring-boot-demo/websocket/socketio/064155.jpg)
  256. **浏览器2:**![image-20181219152330156](http://static.xkcoding.com/spring-boot-demo/websocket/socketio/064154.jpg)
  257. ## 4. 参考
  258. ### 4.1. 后端
  259. 1. Netty-socketio 官方仓库:https://github.com/mrniko/netty-socketio
  260. 2. SpringBoot系列 - 集成SocketIO实时通信:https://www.xncoding.com/2017/07/16/spring/sb-socketio.html
  261. 3. Spring Boot 集成 socket.io 后端实现消息实时通信:http://alexpdh.com/2017/09/03/springboot-socketio/
  262. 4. Spring Boot实战之netty-socketio实现简单聊天室:http://blog.csdn.net/sun_t89/article/details/52060946
  263. ### 4.2. 前端
  264. 1. socket.io 官网:https://socket.io/
  265. 2. axios.js 用法:https://github.com/axios/axios#example