Springboot 2.2前后端简易实现webSocket通信
一、WebSocket简述
WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范。WebSocket API也被W3C定为标准。
WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。

其他特点包括:
(1)建立在 TCP 协议之上,服务器端的实现比较容易。
(2)与 HTTP 协议有着良好的兼容性。默认端口也是80和443,并且握手阶段采用 HTTP 协议,因此握手时不容易屏蔽,能通过各种 HTTP 代理服务器。
(3)数据格式比较轻量,性能开销小,通信高效。
(4)可以发送文本,也可以发送二进制数据。
(5)没有同源限制,客户端可以与任意服务器通信。
(6)协议标识符是ws(如果加密,则为wss),服务器网址就是 URL。

二、Springboot 2.2中应用
1.maven依赖
<!-- websocket依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2.配置类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * websocket配置类 * * @author zxx * @date 2022-08-22 09:51 * @since 1.0.0 */ @Configuration public class WebSocketConfig { /** * 注入ServerEndpointExporter, * 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint * 如果使用Springboot默认内置的tomcat容器,则必须注入ServerEndpoint的bean; * 如果使用外置的web容器,则不需要提供ServerEndpointExporter,下面的注入可以注解掉 */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
3.服务类
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.abc5w.newsflash_backend_java.apiService.RoadshowQAService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * websocket操作类 * * @author zxx * @date 2022-08-22 10:00 * @since 1.0.0 */ @Component @Slf4j @ServerEndpoint("/ws/{userId}") public class WebSocketServer { // 先定义,autowired会在类加载后自动注入(解决@Component类中@Service等注解注入失败的情况) private static RoadshowQAService roadshowQAService; // 当前用户id private String userId; //与某个客户端的连接会话,需要通过它来给客户端发送数据 private Session session; //新:使用map对象优化,便于根据sid来获取对应的WebSocket //虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。 private static ConcurrentHashMap<String, WebSocketServer> websocketMap = new ConcurrentHashMap<>(); // 用来存在线连接数 private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<>(); /** * roadshowQAService注入 * @param roadshowQAService 自定义service类 */ @Autowired public void setRoadshowQAService(RoadshowQAService roadshowQAService) { this.roadshowQAService = roadshowQAService; } /** * 链接成功调用的方法 */ @OnOpen public void onOpen(Session session, @PathParam(value = "userId") String userId) { try { this.session = session; this.userId = userId; websocketMap.put(userId, this); sessionPool.put(userId, session); log.info("【websocket消息】有新的连接,总数为:" + websocketMap.size()); } catch (Exception e) { e.printStackTrace(); } } /** * 链接关闭调用的方法 */ @OnClose public void onClose() { try { if (websocketMap.containsKey(this.userId)) { websocketMap.remove(this.userId); sessionPool.remove(this.userId); } log.info("【websocket消息】连接断开,总数为:" + websocketMap.size()); } catch (Exception e) { } } /** * 收到客户端消息后调用的方法 * * @param message */ @OnMessage public void onMessage(String message) { log.info("【websocket消息】收到客户端消息:" + message); // 广播收到消息 sendAllMessage(message); // 调用自己的业务逻辑(此处可自行更换) JSONObject jsonObject = JSONUtil.parseObj(message); Map<String, Object> retMap = roadshowQAService.getSimilarQA( jsonObject.get("orgCode").toString(), jsonObject.get("question").toString(), 3); // 再次广播结果 sendAllMessage(JSONUtil.parseObj(retMap).toString()); } /** * 发送错误时的处理 * * @param session * @param error */ @OnError public void onError(Session session, Throwable error) { log.error("用户错误,原因:" + error.getMessage()); error.printStackTrace(); } // 此为广播消息 public void sendAllMessage(String message) { log.info("【websocket消息】广播消息:" + message); for (Map.Entry<String, WebSocketServer> map : websocketMap.entrySet()) { WebSocketServer webSocketServer = map.getValue(); if (webSocketServer.session.isOpen()) { webSocketServer.session.getAsyncRemote().sendText(message); } } } // 此为单点消息 public void sendOneMessage(String userId, String message) { Session session = sessionPool.get(userId); if (session != null && session.isOpen()) { try { log.info("【websocket消息】单点消息:" + message); session.getAsyncRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } } // 此为单点消息(多人) public void sendMoreMessage(String[] userIds, String message) { for (String userId : userIds) { Session session = sessionPool.get(userId); if (session != null && session.isOpen()) { try { log.info("【websocket消息】 单点消息:" + message); session.getAsyncRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } } } }
4.前端页面
<script th:inline="javascript"> $(function() {
// socket链接 openSocket();
// 发送数据(测试,看自己如何发)
sendMessage(JSON.stringify(data)); }); let socket; function openSocket() { if (typeof (WebSocket) == "undefined") { console.log("您的浏览器不支持WebSocket"); } else { console.log("您的浏览器支持WebSocket");
// 未前后端分离,所以直接获取本地服务器 let socketUrl = window.location.protocol + "//" + document.location.hostname + ":8080/ws/" + userId; // 替换为socket连接 socketUrl = socketUrl.replace("http", "ws").replace("https", "wss"); // 创建socket连接 socket = new WebSocket(socketUrl); //打开事件 socket.onopen = function () { console.log("websocket已打开"); //socket.send("这是来自客户端的消息" + location.href + new Date()); }; //获得消息事件 socket.onmessage = function (msg) { console.log(msg.data); let jsonData = JSON.parse(msg.data); // 赋值消息到文本域(接到消息了自行决定如何操作) // $("#replyContent").append("用户『" + jsonData.username + "』说:" + jsonData.question + "\n"); }; //关闭事件 socket.onclose = function () { console.log("websocket已关闭"); }; //发生了错误事件 socket.onerror = function () { console.log("websocket发生了错误"); } } } function sendMessage(msg) { if (typeof (WebSocket) == "undefined") { console.log("您的浏览器不支持WebSocket"); } else { console.log(msg); socket.send(msg); } } </script>
服务端日志:


浙公网安备 33010602011771号