SpringBoot集成WebSocket实现客户端与服务端长连接通信

场景:
1、WebSocket协议是用于前后端长连接交互的技术,此技术多用于交互不断开的场景。特点是连接不间断、更轻量,只有在关闭浏览器窗口、或者关闭浏览器、或主动close,当前会话对象才会关闭。
2、相较于 Http/Https 通信只能由客户端主动发起请求,而 Socket 通信不仅能由客户端主动发起请求、服务端也可能主动给客户端推送消息

这里只是简单的记录一下使用方式

一、服务端

1、导入 websocket 依赖

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

2、配置 WebSocket 通信协议标准(服务端点导出)对象

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

说明:
如果没有配置 WebSocket 通信协议标准对象,可能会导致如下错误:

错误一:
VM105:1 WebSocket connection to 'ws://xxx:7002' failed: Error during WebSocket handshake: Unexpected response code: 404

错误二:
This request has been blocked; this endpoint must be available over WSS.

错误三:
VM78:1 WebSocket connection to 'wss://xxx:7002' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

错误四(没有携带token或密钥):
VM78:1 WebSocket connection to 'wss://xxx:7002' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

3、服务端的四个注解方法,对应触发事件

@OnOpen:连接成功交互初始化
@OnMessage:消息事件
@OnClose:关闭事件
@OnError:异常事件

4、websocket 事件服务类,可以理解为Restful Api的映射类(controller)

@Slf4j
@Component
@ServerEndpoint("/websocket/msg/{userId}")
public class WsMessageService {

    //与某个客户端的连接会话,通过此会话对象给客户端发送数据
    private Session session;

    //concurrent包的线程安全Set,用来存放每个客户端对应的WebSocket对象。
    //注:泛型是当前类名
    //private static Set<WsMessageService> webSockets = new CopyOnWriteArraySet<>();
    private static Map<String, WsMessageService> webSocketsBeanMap = new ConcurrentHashMap<>();

    //用来保存在线连接数
    //private static Map<String, Session> sessionPool = new HashMap<>();

    //每次连接都是一个新的会话对象,线程安全的
    String userId;


    @OnOpen
    public void onOpen(Session session, @PathParam(value = "userId") String userId) {
        this.session = session;
        this.userId = userId;
        webSocketsBeanMap.put(userId, this);
        log.info("OnOpen连接成功,userId:{},当前在线人数:{}", userId, this.getOnLineCount());
    }

    @OnMessage
    public void onMessage(String message) throws IOException {
        Session session = webSocketsBeanMap.get(this.userId).session;
        if (ObjectUtil.isNull(session) || !session.isOpen()) {
            return;
        }
        log.info("收到客户端的消息:" + message);
        this.session.getBasicRemote().sendText(String.valueOf(this.getOnLineCount()));
    }

    @OnClose
    public void onClose() throws IOException {
        log.info("会话关闭,关闭会话的用户Id为:{}", this.userId);
        webSocketsBeanMap.remove(this.userId);
        log.info("当前在线人数:{}", this.getOnLineCount());
    }

    @OnError
    public void onError(Session session, Throwable error) {
        log.error("连接错误:" + error.getMessage());
        error.printStackTrace();
    }


    /**
     * <p>返回在线人数</p>
     *
     * @author hkl
     * @date 2023/2/16
     */
    private int getOnLineCount() {
        return webSocketsBeanMap.size();
    }

}

 到这里服务端demo已经完成,可以使用浏览器、HTML页面、Apipost测试

二、测试验证

连接语法:ws://IP地址:端口号

1、使用 Apipost 工具测试

【1】下载安装 Apipost

【2】输入访问地址、连接、发送消息,如下

2、用浏览器测试
第1步:var ws = new WebSocket("ws://localhost:7000/mpj/websocket/1");
第2步:console.log("连接状态:", ws.readyState);

连接状态说明:
0:CONNECTING,表示正在连接。
1:OPEN,表示连接成功,可以通信了。
2:CLOSING,表示连接正在关闭。
3:CLOSED,表示连接已经关闭,或者打开连接失败

第3步:ws.send("hello"); 

示例如下:

 服务端收到消息:

3、使用 html 页面编写js脚本测试

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"content="width=device-width, initial-scale=1.0">
    <title>WebSocket测试</title>
    <script>
        function initWebSocket(wsUri) {
            var websocket = new WebSocket(wsUri);
            websocket.onopen = function(evt) {
                console.log('连接建立中... '+wsUri);
            };
            websocket.onclose = function(evt) {
                console.log('连接关闭中...', evt);
            };
            websocket.onmessage = function(evt) {
                console.log('收到来自服务端的消息:', evt.data);
            };
            websocket.onerror = function(evt) {
                console.log('发生错误...', evt);
            };
            return websocket;
        }
    
        //在此配置 websocket 的地址
        var websocket = initWebSocket("ws://localhost:6000/mpj/websocket/msg/1");
        var msg, i = 0;
        var loop = setInterval(function(){
            msg = "Hello " +(i++);
            if(websocket.readyState == WebSocket.OPEN) {
                websocket.send(msg);
                console.log('已发送消息:' +msg);
            } else{
                clearInterval(loop);
                console.log('连接已关闭,拜拜~');
            }
        }, 3000);
    </script>
</head>
<body>请按 F12 打开控制台查看消息
</body>
</html>

运行如下:

 

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Spring Boot中实现WebSocket,可以遵循以下步骤: 1. 在pom.xml文件中添加spring-boot-starter-websocket依赖项。 2. 创建一个WebSocket配置类,使用@EnableWebSocket注解启用WebSocket,并继承WebSocketMessageBrokerConfigurer类。 3. 在配置类中,重写registerStompEndpoints()方法,用于注册WebSocket端点。可以使用withSockJS()方法启用SockJS支持,以便在不支持WebSocket的浏览器上提供备用传输选项。 4. 创建一个WebSocket处理器类,实现WebSocketHandler接口,并重写相关方法来处理WebSocket连接和消息。 5. 在处理器类中,可以使用@MessageMapping注解来定义处理特定消息的方法。 6. 运行Spring Boot应用程序,WebSocket将在指定的端点上启用。 以下是一个简单的示例代码: ```java // 1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> // 2. 创建WebSocket配置类 @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/api/websocket").withSockJS(); } } // 3. 创建WebSocket处理器类 @Component public class WebSocketHandler implements WebSocketHandler { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 处理连接建立后的操作 } @Override public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { // 处理收到的消息 } @Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { // 处理传输错误 } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { // 处理连接关闭后的操作 } @Override public boolean supportsPartialMessages() { return false; } } // 4. 处理特定消息的方法 @Controller public class MessageController { @MessageMapping("/sendMessage") public void sendMessage(String message) { // 处理收到的消息 } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值