翼度科技»论坛 编程开发 JavaScript 查看内容

SpringBoot整合websocket

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
1.websocket介绍

WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。websocket 协议是在 http 协议上的一种补充协议,是 html5 的新特性,是一种持久化的协议。
2.应用场景


  • 系统实时通告
  • 聊天室
  • ....
3.spring boot 整合(亲测有效)

(1)导入pom
  1.   <dependency>
  2.       <groupId>org.springframework.boot</groupId>
  3.       <artifactId>spring-boot-starter-websocket</artifactId>
  4.   </dependency>
复制代码
(2)websocket配置类:
  1. package com.ruoyi.framework.config.websocked;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.socket.server.standard.ServerEndpointExporter;
  5. @Configuration
  6. public class WebSocketConfig {
  7.     @Bean
  8.     public ServerEndpointExporter serverEndpointExporter() {
  9.         return new ServerEndpointExporter();
  10.     }
  11. }
复制代码
作用:可以将带有 @ServerEndpoint 注解的 WebSocket 端点注册到应用程序中,以便能够处理 WebSocket 连接。
(3)websocket操作类:
  1. package com.ruoyi.project.websorcked;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.stereotype.Component;
  5. import javax.websocket.*;
  6. import javax.websocket.server.PathParam;
  7. import javax.websocket.server.ServerEndpoint;
  8. import java.util.concurrent.ConcurrentHashMap;
  9. import java.util.concurrent.CopyOnWriteArraySet;
  10. @Component
  11. @ServerEndpoint("/websocket/{userId}")
  12. public class WebSocketServer {
  13.     /**
  14.      * 日志工具
  15.      */
  16.     private Logger logger = LoggerFactory.getLogger(this.getClass());
  17.     /**
  18.      * 与某个客户端的连接会话,需要通过它来给客户端发送数据
  19.      */
  20.     private Session session;
  21.     /**
  22.      * 用户id
  23.      */
  24.     private String userId;
  25.     /**
  26.      * 用来存放每个客户端对应的MyWebSocket对象
  27.      */
  28.     private static CopyOnWriteArraySet<WebSocketServer> webSockets = new CopyOnWriteArraySet<>();
  29.     /**
  30.      * 用来存在线连接用户信息
  31.      */
  32.     private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<String, Session>();
  33.     /**
  34.      * 链接成功调用的方法
  35.      */
  36.     @OnOpen
  37.     public void onOpen(Session session, @PathParam(value = "userId") String userId) {
  38.         try {
  39.             this.session = session;
  40.             this.userId = userId;
  41.             webSockets.add(this);
  42.             sessionPool.put(userId, session);
  43.             logger.info("【websocket消息】有新的连接,总数为:" + webSockets.size());
  44.         } catch (Exception e) {
  45.         }
  46.     }
  47.     /**
  48.      * 链接关闭调用的方法
  49.      */
  50.     @OnClose
  51.     public void onClose() {
  52.         try {
  53.             webSockets.remove(this);
  54.             sessionPool.remove(this.userId);
  55.             logger.info("【websocket消息】连接断开,总数为:" + webSockets.size());
  56.         } catch (Exception e) {
  57.         }
  58.     }
  59.     /**
  60.      * 收到客户端消息后调用的方法
  61.      */
  62.     @OnMessage
  63.     public void onMessage(String message) {
  64.         logger.info("【websocket消息】收到客户端消息:" + message);
  65.     }
  66.     /**
  67.      * 发送错误时的处理
  68.      *
  69.      * @param session
  70.      * @param error
  71.      */
  72.     @OnError
  73.     public void onError(Session session, Throwable error) {
  74.         logger.error("用户错误,原因:" + error.getMessage());
  75.         error.printStackTrace();
  76.     }
  77.     /**
  78.      * 此为广播消息
  79.      */
  80.     public static void sendAllMessage(String message) {
  81. //        logger.info("【websocket消息】广播消息:" + message);
  82.         System.out.println("【websocket消息】广播消息:" + message);
  83.         for (WebSocketServer webSocket : webSockets) {
  84.             try {
  85.                 if (webSocket.session.isOpen()) {
  86.                     webSocket.session.getAsyncRemote().sendText(message);
  87.                 }
  88.             } catch (Exception e) {
  89.                 e.printStackTrace();
  90.             }
  91.         }
  92.     }
  93.     /**
  94.      * 此为单点消息
  95.      */
  96.     public void sendOneMessage(String userId, String message) {
  97.         Session session = sessionPool.get(userId);
  98.         if (session != null && session.isOpen()) {
  99.             try {
  100.                 logger.info("【websocket消息】 单点消息:" + message);
  101.                 session.getAsyncRemote().sendText(message);
  102.             } catch (Exception e) {
  103.                 e.printStackTrace();
  104.             }
  105.         }
  106.     }
  107.     /**
  108.      * 此为单点消息(多人)
  109.      */
  110.     public void sendMoreMessage(String[] userIds, String message) {
  111.         for (String userId : userIds) {
  112.             Session session = sessionPool.get(userId);
  113.             if (session != null && session.isOpen()) {
  114.                 try {
  115.                     logger.info("【websocket消息】 单点消息:" + message);
  116.                     session.getAsyncRemote().sendText(message);
  117.                 } catch (Exception e) {
  118.                     e.printStackTrace();
  119.                 }
  120.             }
  121.         }
  122.     }
  123. }
复制代码
说明

  • (1)@ServerEndpoint(“/websocket/{userId}”) 前端通过此 URI 和后端交互,建立连接
  • (2)@Component 不用说将此类交给 spring 管理
  • (3)@OnOpen websocket 建立连接的注解,前端触发上面 URI 时会进入此注解标注的方法
  • (4)@OnMessage 收到前端传来的消息后执行的方法
  • (5)@OnClose 顾名思义关闭连接,销毁 session
    (4)前端样例:
  1. var userId = "your_user_id"; // 替换为实际的用户 ID
  2. var socket = new WebSocket("ws://your_server_address/websocket/" + userId);
  3. socket.onopen = function(event) {
  4.     // WebSocket 连接已打开
  5. };
  6. socket.onmessage = function(event) {
  7.     // 收到来自服务器的消息
  8.     var message = event.data;
  9.     console.log("Received message: " + message);
  10. };
  11. socket.onclose = function(event) {
  12.     // WebSocket 连接已关闭
  13. };
复制代码
(5)前端demo:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.   <title>websocket通讯</title>
  6. </head>
  7. <body>
  8. <p>【socket开启者的ID信息】:<input id="userId" name="userId" type="text" value="10">
  9. <p>【客户端向服务器发送的内容】:<input id="toUserId" name="toUserId" type="text" value="20">
  10.   <input id="contentText" name="contentText" type="text" value="hello websocket">
  11. <p>【操作】:<button><a onclick="openSocket()">开启socket</a></button>
  12. <p>【操作】:<button><a onclick="sendMessage()">发送消息</a></button>
  13. <p>【操作】:<button><a onclick="getMessage()">获取后台广播消息</a></button>
  14. </body>
  15. </html>
复制代码
(6)效果图:

(7)通过后端接口发送消息到客户端,客户端成功接收:
代码如下
  1. package com.ruoyi.project.websorcked;
  2. import com.ruoyi.framework.web.domain.AjaxResult;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiImplicitParam;
  5. import io.swagger.annotations.ApiImplicitParams;
  6. import io.swagger.annotations.ApiOperation;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.RestController;
  12. @RestController
  13. @RequestMapping(value = "/api/v1/websocket")
  14. @Api(tags = "websocket接口", value = "AlarmDpController")
  15. public class WebSocketController {
  16.     @Autowired
  17.     private WebSocketServer webSocketServer;
  18.     /**
  19.      * 模拟数据发送
  20.      */
  21.     @ApiOperation(value = "模拟数据发送", notes = "模拟数据发送")
  22.     @ApiImplicitParams({
  23.             @ApiImplicitParam(paramType = "query", name = "message", value = "模拟消息", required = true, dataType = "String"),
  24.     })
  25.     @RequestMapping(value = "/sendTestMessage", method = RequestMethod.GET)
  26.     public AjaxResult sendTestMessage(@RequestParam("message")String message) {
  27.         AjaxResult ajaxJson = new AjaxResult();
  28.         try {
  29.             webSocketServer.sendAllMessage(message);
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32. //            return AjaxJson.returnExceptionInfo(LoitStatusMsg.LOIT_USER_LOGIN_FAIL);
  33.         }
  34.         return ajaxJson;
  35.     }
  36. }
复制代码
4.遇到的问题

(1) websocket发送连接请求到服务端,报错Filed
解决如下,因为我使用的是若依的项目,使用的安全框架为shiro,在shiro的config类中添加白名单放行


来源:https://www.cnblogs.com/sy2022/p/17611638.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具