package com.supervision.websocket.handler; import com.supervision.websocket.cache.WebSocketUserCache; import lombok.extern.slf4j.Slf4j; 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; @Slf4j public class AskWebSocketHandler extends TextWebSocketHandler { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 获取本次 String id = session.getId(); WebSocketUserCache.login(id, session); // 连接建立时的处理逻辑 } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { // 处理接收到的文本消息 log.info("收到消息:{}", message.toString()); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { // 连接关闭时的处理逻辑 String id = session.getId(); WebSocketUserCache.logout(id); } }