Spring Boot集成WebSocket项目实战的示例代码

 更新时间:2025年10月24日 09:01:10   作者:IT橘子皮  
本文主要介绍了Spring Boot集成WebSocket项目实战的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

WebSocket是一种在单个TCP连接上进行全双工通信的协议,它使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。下面我将详细介绍如何在Spring Boot项目中集成WebSocket,并提供完整的实战代码。

一、项目配置与依赖

1. 添加依赖

首先在pom.xml中添加必要依赖:

<dependencies>
    <!-- WebSocket支持 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    
    <!-- Web支持 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Lombok简化代码 -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    
    <!-- JSON处理 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.83</version>
    </dependency>
</dependencies>

2. WebSocket配置类

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();
    }
}

二、WebSocket服务端实现

1. 消息实体类

import lombok.Data;

@Data
public class WebSocketMessage {
    private String type;       // 消息类型:heartbeat/unicast/broadcast
    private String from;      // 发送者ID
    private String to;        // 接收者ID(单播时使用)
    private String content;    // 消息内容
    private Long timestamp;   // 时间戳
}

2. WebSocket服务核心类

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Component
@ServerEndpoint("/ws/{userId}")
public class WebSocketServer {
    
    // 在线连接数
    private static final AtomicInteger onlineCount = new AtomicInteger(0);
    
    // 存放每个客户端对应的WebSocketServer对象
    private static final ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
    
    // 与某个客户端的连接会话
    private Session session;
    
    // 接收的用户ID
    private String userId;
    
    // 最后心跳时间
    private long lastHeartbeatTime = System.currentTimeMillis();
    
    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId) {
        this.session = session;
        this.userId = userId;
        
        if (webSocketMap.containsKey(userId)) {
            webSocketMap.remove(userId);
            webSocketMap.put(userId, this);
        } else {
            webSocketMap.put(userId, this);
            addOnlineCount();
        }
        
        log.info("用户连接:{},当前在线人数:{}", userId, getOnlineCount());
        
        // 启动心跳检测线程
        new HeartbeatThread().start();
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        if (webSocketMap.containsKey(userId)) {
            webSocketMap.remove(userId);
            subOnlineCount();
        }
        log.info("用户退出:{},当前在线人数:{}", userId, getOnlineCount());
    }

    /**
     * 收到客户端消息后调用的方法
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("收到用户{}的消息:{}", userId, message);
        
        WebSocketMessage msg = JSON.parseObject(message, WebSocketMessage.class);
        
        // 心跳检测
        if ("heartbeat".equals(msg.getType())) {
            this.lastHeartbeatTime = System.currentTimeMillis();
            return;
        }
        
        // 处理业务消息
        switch (msg.getType()) {
            case "unicast":
                sendToUser(msg.getTo(), message);
                break;
            case "broadcast":
                broadcast(message);
                break;
            default:
                log.warn("未知的消息类型:{}", msg.getType());
        }
    }

    /**
     * 发生错误时调用
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("用户{}的连接发生错误:{}", userId, error.getMessage());
        error.printStackTrace();
    }

    /**
     * 单播消息
     */
    public static void sendToUser(String toUserId, String message) {
        if (webSocketMap.containsKey(toUserId)) {
            webSocketMap.get(toUserId).sendMessage(message);
        } else {
            log.warn("用户{}不在线,消息发送失败", toUserId);
        }
    }

    /**
     * 广播消息
     */
    public static void broadcast(String message) {
        webSocketMap.forEach((userId, server) -> {
            if (server.session.isOpen()) {
                server.sendMessage(message);
            }
        });
    }

    /**
     * 服务器主动推送
     */
    public void sendMessage(String message) {
        try {
            this.session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            log.error("发送消息失败:{}", e.getMessage());
        }
    }

    public static synchronized int getOnlineCount() {
        return onlineCount.get();
    }

    public static synchronized void addOnlineCount() {
        onlineCount.incrementAndGet();
    }

    public static synchronized void subOnlineCount() {
        onlineCount.decrementAndGet();
    }
    
    /**
     * 心跳检测线程
     */
    private class HeartbeatThread extends Thread {
        @Override
        public void run() {
            while (session.isOpen()) {
                try {
                    // 每30秒检测一次
                    Thread.sleep(30000);
                    
                    // 超过60秒未收到心跳,关闭连接
                    if (System.currentTimeMillis() - lastHeartbeatTime > 60000) {
                        session.close();
                        break;
                    }
                } catch (Exception e) {
                    log.error("心跳检测异常:{}", e.getMessage());
                    break;
                }
            }
        }
    }
}

3. WebSocket控制器(可选)

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/ws")
public class WebSocketController {
    
    @PostMapping("/sendToUser")
    public String sendToUser(@RequestParam String toUserId, @RequestParam String message) {
        WebSocketServer.sendToUser(toUserId, message);
        return "消息已发送";
    }
    
    @PostMapping("/broadcast")
    public String broadcast(@RequestParam String message) {
        WebSocketServer.broadcast(message);
        return "广播已发送";
    }
}

三、前端实现

1. HTML页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WebSocket测试</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <div>
        <h2>WebSocket测试</h2>
        <div>
            <label>用户ID:</label>
            <input type="text" id="userId" value="user1">
            <button onclick="connect()">连接</button>
            <button onclick="disconnect()" disabled id="disconnectBtn">断开</button>
        </div>
        <div>
            <label>目标用户:</label>
            <input type="text" id="toUserId" value="user2">
            <label>消息内容:</label>
            <input type="text" id="message" value="Hello WebSocket">
            <button onclick="sendUnicast()">单播</button>
            <button onclick="sendBroadcast()">广播</button>
        </div>
        <div>
            <h3>消息日志:</h3>
            <div id="messageLog" style="height:300px;overflow-y:scroll;border:1px solid #ccc;"></div>
        </div>
    </div>

    <script>
        var websocket = null;
        var heartbeatInterval = null;
        
        // 连接WebSocket
        function connect() {
            var userId = $('#userId').val();
            if (!userId) {
                alert('请输入用户ID');
                return;
            }
            
            if ('WebSocket' in window) {
                websocket = new WebSocket('ws://' + window.location.host + '/ws/' + userId);
            } else {
                alert('当前浏览器不支持WebSocket');
                return;
            }
            
            websocket.onopen = function() {
                logMessage('连接已建立');
                $('#disconnectBtn').prop('disabled', false);
                
                // 启动心跳检测
                startHeartbeat();
            };
            
            websocket.onmessage = function(event) {
                logMessage('收到消息: ' + event.data);
            };
            
            websocket.onclose = function() {
                logMessage('连接已关闭');
                $('#disconnectBtn').prop('disabled', true);
                
                // 停止心跳检测
                stopHeartbeat();
            };
            
            websocket.onerror = function(error) {
                logMessage('发生错误: ' + error.data);
            };
        }
        
        // 断开连接
        function disconnect() {
            if (websocket != null) {
                websocket.close();
            }
        }
        
        // 发送单播消息
        function sendUnicast() {
            if (websocket == null || websocket.readyState != WebSocket.OPEN) {
                alert('请先建立连接');
                return;
            }
            
            var message = {
                type: 'unicast',
                from: $('#userId').val(),
                to: $('#toUserId').val(),
                content: $('#message').val(),
                timestamp: new Date().getTime()
            };
            
            websocket.send(JSON.stringify(message));
            logMessage('已发送单播消息: ' + JSON.stringify(message));
        }
        
        // 发送广播消息
        function sendBroadcast() {
            if (websocket == null || websocket.readyState != WebSocket.OPEN) {
                alert('请先建立连接');
                return;
            }
            
            var message = {
                type: 'broadcast',
                from: $('#userId').val(),
                content: $('#message').val(),
                timestamp: new Date().getTime()
            };
            
            websocket.send(JSON.stringify(message));
            logMessage('已发送广播消息: ' + JSON.stringify(message));
        }
        
        // 启动心跳检测
        function startHeartbeat() {
            // 每20秒发送一次心跳
            heartbeatInterval = setInterval(function() {
                if (websocket.readyState == WebSocket.OPEN) {
                    var heartbeat = {
                        type: 'heartbeat',
                        from: $('#userId').val(),
                        timestamp: new Date().getTime()
                    };
                    websocket.send(JSON.stringify(heartbeat));
                    logMessage('已发送心跳');
                }
            }, 20000);
        }
        
        // 停止心跳检测
        function stopHeartbeat() {
            if (heartbeatInterval != null) {
                clearInterval(heartbeatInterval);
                heartbeatInterval = null;
            }
        }
        
        // 记录日志
        function logMessage(message) {
            var logDiv = $('#messageLog');
            logDiv.append('<p>' + new Date().toLocaleString() + ' - ' + message + '</p>');
            logDiv.scrollTop(logDiv[0].scrollHeight);
        }
    </script>
</body>
</html>

四、功能测试

  1. 连接测试​:

    • 启动Spring Boot应用
    • 打开两个浏览器窗口,分别输入不同用户ID连接
    • 观察控制台日志,确认连接建立
  2. 单播测试​:

    • 在窗口A输入目标用户ID(窗口B的用户ID)和消息内容
    • 点击"单播"按钮,确认窗口B收到消息
  3. 广播测试​:

    • 在任意窗口点击"广播"按钮
    • 确认所有连接的窗口都收到消息
  4. 心跳检测测试​:

    • 观察控制台日志,确认每20秒发送一次心跳
    • 断开网络连接60秒以上,确认自动断开WebSocket连接

五、高级功能扩展

1. 消息持久化

// 在WebSocketServer类中添加
private void saveMessage(WebSocketMessage message) {
    // 实现消息存储逻辑,可存入数据库或Redis
    log.info("存储消息: {}", JSON.toJSONString(message));
}

2. 断线重连机制(前端)

// 修改前端connect函数
var reconnectAttempts = 0;
var maxReconnectAttempts = 5;
var reconnectInterval = 5000; // 5秒

function connect() {
    // ...原有代码...
    
    websocket.onclose = function() {
        logMessage('连接已关闭');
        $('#disconnectBtn').prop('disabled', true);
        stopHeartbeat();
        
        // 断线重连逻辑
        if (reconnectAttempts < maxReconnectAttempts) {
            reconnectAttempts++;
            logMessage('尝试重新连接(' + reconnectAttempts + '/' + maxReconnectAttempts + ')');
            setTimeout(connect, reconnectInterval);
        }
    };
    
    // 连接成功后重置重试计数
    websocket.onopen = function() {
        reconnectAttempts = 0;
        // ...原有代码...
    };
}

3. 使用STOMP协议(可选)

如果需要更复杂的消息路由和订阅/发布模式,可以考虑使用STOMP协议:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer {
    
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic", "/queue");
        config.setApplicationDestinationPrefixes("/app");
        config.setUserDestinationPrefix("/user");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws-stomp")
                .setAllowedOriginPatterns("*")
                .withSockJS();
    }
}

六、优化

实际项目中,还可以根据需求进一步优化:

  • 添加JWT认证机制
  • 实现消息历史记录查询
  • 增加用户在线状态管理

对于高并发场景,可以考虑以下优化:

  • 使用session.getAsyncRemote().sendText()替代同步发送
  • 增加消息缓冲区
  • 使用Redis等中间件实现分布式WebSocket

到此这篇关于Spring Boot集成WebSocket项目实战的示例代码的文章就介绍到这了,更多相关Spring Boot集成WebSocket内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring boot中使用Spring-data-jpa方便快捷的访问数据库(推荐)

    Spring boot中使用Spring-data-jpa方便快捷的访问数据库(推荐)

    Spring Data JPA 是 Spring 基于 ORM 框架、JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。这篇文章主要介绍了Spring-boot中使用Spring-data-jpa方便快捷的访问数据库,需要的朋友可以参考下
    2018-05-05
  • java 数据结构中栈和队列的实例详解

    java 数据结构中栈和队列的实例详解

    这篇文章主要介绍了java 数据结构中栈和队列的实例详解的相关资料,主要使用数组与线性表的方法来实现,需要的朋友可以参考下
    2017-09-09
  • SpringCloud与Dubbo集成Nacos时服务重复注册问题的分析与解决

    SpringCloud与Dubbo集成Nacos时服务重复注册问题的分析与解决

    Nacos作为阿里巴巴开源的服务注册与发现工具,广泛应用于Spring Cloud和Dubbo等微服务框架中,然而,在实际开发中,我们可能会遇到服务重复注册的问题,下面我们就来详细分析一下这一问题
    2025-03-03
  • Java并发编程之重入锁与读写锁

    Java并发编程之重入锁与读写锁

    这篇文章主要介绍了Java并发编程之重入锁与读写锁,文中相关实例代码详细,测试可用,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09
  • 浅析Java中的SPI原理

    浅析Java中的SPI原理

    SPI:由调用方制定接口标准,实现方来针对接口提供不同的实现,SPI其实就是"为接口查找实现"的一种服务发现机制。本文将浅谈一下SPI机制的原理,需要的可以参考一下
    2022-09-09
  • SpringBoot FreeWorker模板技术解析

    SpringBoot FreeWorker模板技术解析

    这篇文章主要介绍了SpringBoot FreeWorker模板技术解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 关于Java下奇怪的Base64详解

    关于Java下奇怪的Base64详解

    这篇文章主要给大家介绍了关于Java下奇怪的Base64的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • java实现简单中国象棋

    java实现简单中国象棋

    这篇文章主要为大家详细介绍了java实现简单中国象棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 关于在Java中使用预定义类

    关于在Java中使用预定义类

    这篇文章主要介绍了关于在Java中使用预定义类,预定义类就是Java类库(或第三方库)中已经定义好的类,例如,Math 类和 Date 类,需要的朋友可以参考下
    2023-05-05
  • 将本地的jar包打到Maven的仓库中实例

    将本地的jar包打到Maven的仓库中实例

    下面小编就为大家分享一篇将本地的jar包打到Maven的仓库中实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-02-02

最新评论