You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package com.supervision.websocket.handler;
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
import com.supervision.websocket.cache.WebSocketUserCache;
|
|
|
|
import com.supervision.websocket.dto.SocketMessageDTO;
|
|
|
|
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();
|
|
|
|
// 缓存sessionId
|
|
|
|
WebSocketUserCache.login(id, session);
|
|
|
|
// 返回sessionId到前端
|
|
|
|
SocketMessageDTO socketMessageDTO = new SocketMessageDTO();
|
|
|
|
socketMessageDTO.setSocketId(id);
|
|
|
|
socketMessageDTO.setData("链接成功");
|
|
|
|
session.sendMessage(new TextMessage(JSONUtil.toJsonStr(socketMessageDTO)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
|
|
|
|
// 处理接收到的文本消息
|
|
|
|
log.info("收到消息:{}", message.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
|
|
|
// 连接关闭时的处理逻辑
|
|
|
|
String id = session.getId();
|
|
|
|
WebSocketUserCache.logout(id);
|
|
|
|
}
|
|
|
|
}
|