2
liusuyi
2026-05-12 9b5c9db9189493fbc6db48f55a7b7311b2a3253c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.ard.work.websocket.handler;
 
import com.ard.work.websocket.utils.PTZWebSocketUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
 
@Component
@Slf4j
public class PTZWebSocketHandler extends TextWebSocketHandler {
 
    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        // 处理接收到的消息
        String payload = message.getPayload();
        log.info("【ptz_websocket】Received message: " + payload);
        log.info("【ptz_websocket】size: " + PTZWebSocketUtils.ONLINE_USER_SESSIONS.size());
    }
 
    @Override
    public void afterConnectionEstablished(WebSocketSession session) {
        log.info("【ptz_websocket】Connected: " + session.getId());
        String userId = (String) session.getAttributes().get("userId");
        PTZWebSocketUtils.ONLINE_USER_SESSIONS.put(userId, session);
        log.info("【ptz_websocket】size: " + PTZWebSocketUtils.ONLINE_USER_SESSIONS.size());
 
    }
 
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
        log.info("【ptz_websocket】Disconnected: " + session.getId());
        String userId = (String) session.getAttributes().get("userId");
        PTZWebSocketUtils.ONLINE_USER_SESSIONS.remove(userId);
        log.info("【ptz_websocket】size: " + PTZWebSocketUtils.ONLINE_USER_SESSIONS.size());
    }
}