106 lines
3.0 KiB
Java
106 lines
3.0 KiB
Java
package com.ruoyi.websocket.handler;
|
|
|
|
import com.ruoyi.websocket.constant.WebSocketConstants;
|
|
import com.ruoyi.websocket.holder.WebSocketSessionHolder;
|
|
import com.ruoyi.websocket.util.WebSocketUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.socket.*;
|
|
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
|
|
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* WebSocketHandler 实现类
|
|
*
|
|
* @author zendwang
|
|
*/
|
|
@Slf4j
|
|
@Component
|
|
public class RoomWebSocketHandler extends AbstractWebSocketHandler {
|
|
|
|
@Autowired
|
|
private IOpenLogic openLogic;
|
|
@Autowired
|
|
private MessageHandleApplication messageHandleApplication;
|
|
/**
|
|
* 连接成功后
|
|
*/
|
|
@Override
|
|
public void afterConnectionEstablished(WebSocketSession session) {
|
|
Map<String, Object> attributes = session.getAttributes();
|
|
if(attributes.get("token") != null){
|
|
WebSocketSessionHolder.addSession(attributes.get("token").toString(), session);
|
|
}
|
|
openLogic.processOn(session);
|
|
log.info("[connect] sessionId: {},userId:{}", session.getId(), session.getId());
|
|
}
|
|
|
|
/**
|
|
* 处理发送来的文本消息
|
|
*
|
|
* @param session
|
|
* @param message
|
|
* @throws Exception
|
|
*/
|
|
@Override
|
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
|
String token = String.valueOf(session.getAttributes().get(WebSocketConstants.TOKEN));
|
|
messageHandleApplication.processOn(session,message);
|
|
}
|
|
|
|
@Override
|
|
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
|
|
super.handleBinaryMessage(session, message);
|
|
}
|
|
|
|
/**
|
|
* 心跳监测的回复
|
|
*
|
|
* @param session
|
|
* @param message
|
|
* @throws Exception
|
|
*/
|
|
@Override
|
|
protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
|
|
WebSocketUtils.sendPongMessage(session);
|
|
}
|
|
|
|
/**
|
|
* 连接出错时
|
|
*
|
|
* @param session
|
|
* @param exception
|
|
* @throws Exception
|
|
*/
|
|
@Override
|
|
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
|
log.error("[transport error] sessionId: {} , exception:{}", session.getId(), exception.getMessage());
|
|
}
|
|
|
|
/**
|
|
* 连接关闭后
|
|
*
|
|
* @param session
|
|
* @param status
|
|
*/
|
|
@Override
|
|
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
|
String token = String.valueOf(session.getAttributes().get("token"));
|
|
WebSocketSessionHolder.removeSession(token);
|
|
log.info("[disconnect] sessionId: {},token:{}", session.getId(), token);
|
|
}
|
|
|
|
/**
|
|
* 是否支持分片消息
|
|
*
|
|
* @return
|
|
*/
|
|
@Override
|
|
public boolean supportsPartialMessages() {
|
|
return false;
|
|
}
|
|
|
|
}
|