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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * @package: com.xkcoding.websocket.socketio.config
  76. * @description: websocket服务器配置
  77. * @author: yangkai.shen
  78. * @date: Created in 2018-12-18 16:42
  79. * @copyright: Copyright (c) 2018
  80. * @version: V1.0
  81. * @modified: yangkai.shen
  82. */
  83. @Configuration
  84. @EnableConfigurationProperties({WsConfig.class})
  85. public class ServerConfig {
  86. @Bean
  87. public SocketIOServer server(WsConfig wsConfig) {
  88. com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
  89. config.setHostname(wsConfig.getHost());
  90. config.setPort(wsConfig.getPort());
  91. //这个listener可以用来进行身份验证
  92. config.setAuthorizationListener(data -> {
  93. // http://localhost:8081?token=xxxxxxx
  94. // 例如果使用上面的链接进行connect,可以使用如下代码获取用户密码信息,本文不做身份验证
  95. String token = data.getSingleUrlParam("token");
  96. // 校验token的合法性,实际业务需要校验token是否过期等等,参考 spring-boot-demo-rbac-security 里的 JwtUtil
  97. // 如果认证不通过会返回一个 Socket.EVENT_CONNECT_ERROR 事件
  98. return StrUtil.isNotBlank(token);
  99. });
  100. return new SocketIOServer(config);
  101. }
  102. /**
  103. * Spring 扫描自定义注解
  104. */
  105. @Bean
  106. public SpringAnnotationScanner springAnnotationScanner(SocketIOServer server) {
  107. return new SpringAnnotationScanner(server);
  108. }
  109. }
  110. ```
  111. ### 1.3. MessageEventHandler.java
  112. > 核心事件处理类,主要处理客户端发起的消息事件,以及主动往客户端发起事件
  113. ```java
  114. /**
  115. * <p>
  116. * 消息事件处理
  117. * </p>
  118. *
  119. * @package: com.xkcoding.websocket.socketio.handler
  120. * @description: 消息事件处理
  121. * @author: yangkai.shen
  122. * @date: Created in 2018-12-18 18:57
  123. * @copyright: Copyright (c) 2018
  124. * @version: V1.0
  125. * @modified: yangkai.shen
  126. */
  127. @Component
  128. @Slf4j
  129. public class MessageEventHandler {
  130. @Autowired
  131. private SocketIOServer server;
  132. @Autowired
  133. private DbTemplate dbTemplate;
  134. /**
  135. * 添加connect事件,当客户端发起连接时调用
  136. *
  137. * @param client 客户端对象
  138. */
  139. @OnConnect
  140. public void onConnect(SocketIOClient client) {
  141. if (client != null) {
  142. String token = client.getHandshakeData().getSingleUrlParam("token");
  143. // 模拟用户id 和token一致
  144. String userId = client.getHandshakeData().getSingleUrlParam("token");
  145. UUID sessionId = client.getSessionId();
  146. dbTemplate.save(userId, sessionId);
  147. log.info("连接成功,【token】= {},【sessionId】= {}", token, sessionId);
  148. } else {
  149. log.error("客户端为空");
  150. }
  151. }
  152. /**
  153. * 添加disconnect事件,客户端断开连接时调用,刷新客户端信息
  154. *
  155. * @param client 客户端对象
  156. */
  157. @OnDisconnect
  158. public void onDisconnect(SocketIOClient client) {
  159. if (client != null) {
  160. String token = client.getHandshakeData().getSingleUrlParam("token");
  161. // 模拟用户id 和token一致
  162. String userId = client.getHandshakeData().getSingleUrlParam("token");
  163. UUID sessionId = client.getSessionId();
  164. dbTemplate.deleteByUserId(userId);
  165. log.info("客户端断开连接,【token】= {},【sessionId】= {}", token, sessionId);
  166. client.disconnect();
  167. } else {
  168. log.error("客户端为空");
  169. }
  170. }
  171. /**
  172. * 加入群聊
  173. *
  174. * @param client 客户端
  175. * @param request 请求
  176. * @param data 群聊
  177. */
  178. @OnEvent(value = Event.JOIN)
  179. public void onJoinEvent(SocketIOClient client, AckRequest request, JoinRequest data) {
  180. log.info("用户:{} 已加入群聊:{}", data.getUserId(), data.getGroupId());
  181. client.joinRoom(data.getGroupId());
  182. server.getRoomOperations(data.getGroupId()).sendEvent(Event.JOIN, data);
  183. }
  184. @OnEvent(value = Event.CHAT)
  185. public void onChatEvent(SocketIOClient client, AckRequest request, SingleMessageRequest data) {
  186. Optional<UUID> toUser = dbTemplate.findByUserId(data.getToUid());
  187. if (toUser.isPresent()) {
  188. log.info("用户 {} 刚刚私信了用户 {}:{}", data.getFromUid(), data.getToUid(), data.getMessage());
  189. sendToSingle(toUser.get(), data);
  190. client.sendEvent(Event.CHAT_RECEIVED, "发送成功");
  191. } else {
  192. client.sendEvent(Event.CHAT_REFUSED, "发送失败,对方不想理你");
  193. }
  194. }
  195. @OnEvent(value = Event.GROUP)
  196. public void onGroupEvent(SocketIOClient client, AckRequest request, GroupMessageRequest data) {
  197. Collection<SocketIOClient> clients = server.getRoomOperations(data.getGroupId()).getClients();
  198. boolean inGroup = false;
  199. for (SocketIOClient socketIOClient : clients) {
  200. if (ObjectUtil.equal(socketIOClient.getSessionId(), client.getSessionId())) {
  201. inGroup = true;
  202. break;
  203. }
  204. }
  205. if (inGroup) {
  206. log.info("群号 {} 收到来自 {} 的群聊消息:{}", data.getGroupId(), data.getFromUid(), data.getMessage());
  207. sendToGroup(data);
  208. } else {
  209. request.sendAckData("请先加群!");
  210. }
  211. }
  212. /**
  213. * 单聊
  214. */
  215. public void sendToSingle(UUID sessionId, SingleMessageRequest message) {
  216. server.getClient(sessionId).sendEvent(Event.CHAT, message);
  217. }
  218. /**
  219. * 广播
  220. */
  221. public void sendToBroadcast(BroadcastMessageRequest message) {
  222. log.info("系统紧急广播一条通知:{}", message.getMessage());
  223. for (UUID clientId : dbTemplate.findAll()) {
  224. if (server.getClient(clientId) == null) {
  225. continue;
  226. }
  227. server.getClient(clientId).sendEvent(Event.BROADCAST, message);
  228. }
  229. }
  230. /**
  231. * 群聊
  232. */
  233. public void sendToGroup(GroupMessageRequest message) {
  234. server.getRoomOperations(message.getGroupId()).sendEvent(Event.GROUP, message);
  235. }
  236. }
  237. ```
  238. ### 1.4. ServerRunner.java
  239. > websocket 服务器启动类
  240. ```java
  241. /**
  242. * <p>
  243. * websocket服务器启动
  244. * </p>
  245. *
  246. * @package: com.xkcoding.websocket.socketio.init
  247. * @description: websocket服务器启动
  248. * @author: yangkai.shen
  249. * @date: Created in 2018-12-18 17:07
  250. * @copyright: Copyright (c) 2018
  251. * @version: V1.0
  252. * @modified: yangkai.shen
  253. */
  254. @Component
  255. @Slf4j
  256. public class ServerRunner implements CommandLineRunner {
  257. @Autowired
  258. private SocketIOServer server;
  259. @Override
  260. public void run(String... args) {
  261. server.start();
  262. log.info("websocket 服务器启动成功。。。");
  263. }
  264. }
  265. ```
  266. ## 2. 运行方式
  267. 1. 启动 `SpringBootDemoWebsocketSocketioApplication.java`
  268. 2. 使用不同的浏览器,访问 http://localhost:8080/demo/index.html
  269. ## 3. 运行效果
  270. **浏览器1:**![image-20181219152318079](assets/image-20181219152318079-5204198.png)
  271. **浏览器2:**![image-20181219152330156](assets/image-20181219152330156-5204210.png)
  272. ## 4. 参考
  273. ### 4.1. 后端
  274. 1. Netty-socketio 官方仓库:https://github.com/mrniko/netty-socketio
  275. 2. SpringBoot系列 - 集成SocketIO实时通信:https://www.xncoding.com/2017/07/16/spring/sb-socketio.html
  276. 3. Spring Boot 集成 socket.io 后端实现消息实时通信:http://alexpdh.com/2017/09/03/springboot-socketio/
  277. 4. Spring Boot实战之netty-socketio实现简单聊天室:http://blog.csdn.net/sun_t89/article/details/52060946
  278. ### 4.2. 前端
  279. 1. socket.io 官网:https://socket.io/
  280. 2. axios.js 用法:https://github.com/axios/axios#example

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。

Contributors (1)