[超详细]SpringBoot整合WebSocket

2023-08-29

1. 什么是WebSocket?

WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,它允许在浏览器和服务器之间进行实时的、双向的通信。相对于传统的基于请求和响应的 HTTP 协议,WebSocket 提供了一种更有效、更实时的通信方式,适用于需要实时更新、实时通知和实时交互的应用。

WebSocket 的一些关键特点包括:

    全双工通信: WebSocket 允许服务器和客户端在同一连接上同时进行双向通信。这意味着服务器可以随时向客户端推送数据,而不必等待客户端发送请求。
    持久连接: WebSocket 连接一旦建立,会一直保持打开状态,不会像传统的 HTTP 连接那样在每次请求和响应之后关闭。这减少了每次连接建立和关闭的开销,使通信更加高效。
    低延迟: 由于连接保持打开状态,WebSocket 通信具有较低的延迟,适用于实时性要求较高的应用,如实时聊天、实时数据更新等。
    少量的数据交换: 与 HTTP 请求和响应相比,WebSocket 数据交换的开销较小。WebSocket 的帧头相对较小,因此有效载荷的比例更高。
    兼容性: 现代浏览器和大多数服务器支持 WebSocket。此外,WebSocket 协议还定义了一个子协议 STOMP(Streaming Text Oriented Messaging Protocol),用于更高级的消息传递和订阅功能。
    安全性: 与其他网络通信协议一样,WebSocket 通信也需要一些安全性的考虑。可以使用加密协议(如 TLS)来保护数据在网络传输中的安全性。

2. 代码实战

2.1 SpringBoot导入依赖

在pom.xml中导入以下依赖,版本由SpringBoot管理

<!-- websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

2.2 创建配置类

创建WebSocketConfig配置类,并将其注入到Bean容器中

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}

2.3 创建WebSocketServer类

创建WebSocketServer类,并将其注入到Bean容器中

注意:@ServerEndpoint("/WebSocket"),该注解用于配置建立WebSocket连接的路径,可以按需修改。

该类一般拥有以下功能:

    WebSocket 端点注册: WebSocket 服务器需要注册一个或多个 WebSocket 端点(Endpoints)。每个端点对应一种处理逻辑,可以处理客户端发送过来的消息,以及向客户端发送消息。这些端点通过注解或配置来定义。
    建立和维护连接: WebSocket 服务器负责监听客户端的连接请求,一旦有客户端连接,服务器会创建一个 WebSocket 会话(Session)来管理这个连接。服务器需要能够维护这些连接,包括打开、关闭、保持心跳等操作。
    消息处理: 一旦客户端连接成功,WebSocket 服务器需要处理客户端发送过来的消息。这可以在 WebSocket 端点中的方法上定义处理逻辑。服务器可以根据不同的业务需求处理不同类型的消息。
    处理异常: 与任何网络通信一样,WebSocket 连接可能会面临各种异常情况,如断开连接、网络问题等。WebSocket 服务器需要能够处理这些异常情况,进行适当的清理和处理。

可以将该类理解为WebSocket生命周期中会调用的方法。

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import javax.websocket.*;
import javax.websocket.server.ServerEndpoint; @Slf4j
@Component
@ServerEndpoint("/WebSocket")
public class WebSocketServer { private Session session; @OnOpen
public void onOpen(Session session) {
this.session = session;
WebSocketManager.sentToUser(session, "WebSocket is connected!");
WebSocketManager.addWebSocketServer(this);
log.info("与SessionId:{}建立连接", session.getId());
} @OnClose
public void onClose() {
WebSocketManager.removeWebSocketServer(this);
log.info("WebSocket连接关闭");
} @OnMessage
public void onMessage(String message, Session session) {
log.info("来自SessionId:{}的消息:{}", session.getId(), message);
} @OnError
public void onError(Session session, Throwable error) {
log.error("Session:{}的WebSocket发生错误", session.getId(), error);
} public Session getSession() {
return session;
} public String getSessionId() {
return session.getId();
}
}

2.4 创建WebSocketServer管理类

该类用于管理WebSocketServer(其实主要是管理Session),如果不需要发送消息给特定用户,那么无需创建该类,在WebSocketServer类中维护一个类变量即可。

import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet; @Slf4j
public class WebSocketManager { private final static CopyOnWriteArraySet<WebSocketServer> webSocketServerSet = new CopyOnWriteArraySet<>(); private final static ConcurrentHashMap<String, WebSocketServer> webSocketServerMap = new ConcurrentHashMap<>(); public static void addWebSocketServer(WebSocketServer webSocketServer){
if (webSocketServer != null){
webSocketServerSet.add(webSocketServer);
webSocketServerMap.put(webSocketServer.getSessionId(), webSocketServer);
}
} public static void removeWebSocketServer(WebSocketServer webSocketServer){
webSocketServerSet.remove(webSocketServer);
webSocketServerMap.remove(webSocketServer.getSessionId());
} /**
* 通过SessionId发送消息给特定用户
* @param
* @param msg
*/
public static void sentToUser(String sessionId, String msg){
Session session = webSocketServerMap.get(sessionId).getSession();
sentToUser(session, msg);
} /**
* 通过Session发送消息给特定用户
* @param session
* @param msg
*/
public static void sentToUser(Session session, String msg){
if (session == null){
log.error("不存在该Session,无法发送消息");
return;
}
session.getAsyncRemote().sendText(msg);
} /**
* 发送消息给所有用户
* @param msg
*/
public static void sentToAllUser(String msg){
for (WebSocketServer webSocketServer : webSocketServerSet) {
sentToUser(webSocketServer.getSession(), msg);
}
log.info("向所有用户发送WebSocket消息完毕,消息:{}", msg);
}
}

3. 测试

使用Postman等工具进行WebSocket连接,连接路径为WebSocket(与前文的@ServerEndpoint内容一致)。建立连接后会收到"WebSocket is connected!"

4. 后续完善

如果你的项目中有登录功能,那么应该在你的登录时将用户的Session记录下来。你可以将Session记录在本地缓存、Redis和Grava缓存等能够建立关联关系的地方。

[超详细]SpringBoot整合WebSocket的相关教程结束。