zhaowenyu
2025-11-08 84be590529a31bda8c3d17665dbd6843a81cb2af
ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java
@@ -4,7 +4,7 @@
import com.ard.sample.component.websocket.enums.MessageType;
import com.ard.sample.component.websocket.model.Payload;
import com.ard.sample.component.websocket.model.WebSocketMessage;
import org.checkerframework.checker.units.qual.A;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
@@ -16,6 +16,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@Component
public class DockWebSocketHandler extends TextWebSocketHandler {
@@ -46,7 +47,8 @@
        userSessions.put(userId, session);
        // 为新会话初始化锁对象
        sessionLocks.put(session, new Object());
        System.out.println("用户[" + userId + "]已连接,当前在线人数:" + userSessions.size());
        //System.out.println("用户[" + userId + "]已连接,当前在线人数:" + userSessions.size());
        log.info("用户[{}]已连接,当前在线人数:{}", userId, userSessions.size());
    }
    /**
@@ -89,7 +91,8 @@
        if (subscribe) {
            // 订阅操作
            subscriptions.computeIfAbsent(subscriptionKey, k -> ConcurrentHashMap.newKeySet()).add(userId);
            System.out.println("用户[" + userId + "]订阅了 设备[" + sn + "]的[" + type + "]类型消息");
            //System.out.println("用户[" + userId + "]订阅了 设备[" + sn + "]的[" + type + "]类型消息");
            log.info("用户[{}]订阅了 设备[{}]的[{}]类型消息", userId, sn, type);
            if(MessageType.LIVESTREAM.getType().equals(type)){
                // 开启直播
                // 开启直播逻辑
@@ -108,7 +111,8 @@
                if (subscriptions.get(subscriptionKey).isEmpty()) {
                    subscriptions.remove(subscriptionKey);
                }
                System.out.println("用户[" + userId + "]取消订阅了 设备[" + sn + "]的[" + type + "]类型消息");
                //System.out.println("用户[" + userId + "]取消订阅了 设备[" + sn + "]的[" + type + "]类型消息");
                log.info("用户[{}]取消订阅了 设备[{}]的[{}]类型消息", userId, sn, type);
            }
        }
    }
@@ -124,7 +128,8 @@
            sessionLocks.remove(session); // 移除会话锁
            // 清除该用户的所有订阅
            removeUserAllSubscriptions(userId);
            System.out.println("用户[" + userId + "]已断开连接,当前在线人数:" + userSessions.size());
            //System.out.println("用户[" + userId + "]已断开连接,当前在线人数:" + userSessions.size());
            log.info("用户[{}]已断开连接,当前在线人数:{}", userId, userSessions.size());
        }
    }
@@ -133,6 +138,10 @@
     */
    private void removeUserAllSubscriptions(String userId) {
        for (Map.Entry<String, Set<String>> entry : subscriptions.entrySet()) {
            if (entry.getKey().contains(MessageType.LIVESTREAM.getType())) {
                String sn = entry.getKey().split(":")[0];
                _liveStreamService.StopLiveStreaming(sn);
            }
            entry.getValue().remove(userId);
            if (entry.getValue().isEmpty()) {
                subscriptions.remove(entry.getKey());
@@ -153,17 +162,6 @@
        }
        try {
            // 构建发送的消息
//            WebSocketMessage message = new WebSocketMessage();
//            message.setType(type);
//
//            Payload payload = new Payload();
//            payload.setSn(sn);
//            payload.setSubscribe(true); // 仅作为标识,无实际意义
//            message.setPayload(payload);
//
//            String jsonMessage = JSON.toJSONString(message);
            // 向所有订阅者发送消息
            for (String userId : subscriberIds) {
                WebSocketSession session = userSessions.get(userId);
@@ -193,7 +191,8 @@
//                }
            }
        } catch (IOException e) {
            System.err.println("发送消息错误:" + e.getMessage());
//            System.err.println("发送消息错误:" + e.getMessage());
            log.error("发送消息错误:{}", e.getMessage());
        }
    }