更新WebSocket操作API实现

pull/22/head
rays 2021-06-05 18:25:35 +08:00
parent 72cfc96ad5
commit d5cd8bb072
4 changed files with 26 additions and 19 deletions

View File

@ -4,6 +4,8 @@ import lombok.Getter;
/**
*
* <p>
*
*
* @author majianguo
* @date 2021/6/3 9:14
@ -13,8 +15,6 @@ public enum ClientMessageTypeEnum {
/**
*
* <p>
*
*/
USER_ADD_MSG_TYPE("200001", "用户添加一个监听的消息类型"),

View File

@ -1,9 +1,17 @@
package cn.stylefeng.roses.kernel.socket.websocket.message;
import cn.stylefeng.roses.kernel.socket.api.SocketOperatorApi;
import lombok.Data;
/**
* WebSocket
* <p>
* serverMsgTypeclientMsgType
* 1.serverMsgType
* (type:100001),
* 2.clientMsgType
* (type:299999)
* {@link SocketOperatorApi#msgTypeCallback}
*
* @author majianguo
* @date 2021/6/1 2:56
@ -12,9 +20,14 @@ import lombok.Data;
public class WebSocketMessagePOJO {
/**
*
* ()
*/
private String type;
private String serverMsgType;
/**
* ()
*/
private String clientMsgType;
/**
* Id

View File

@ -33,15 +33,11 @@ public class WebSocketOperator implements SocketOperatorApi {
if (ObjectUtil.isEmpty(socketSession)) {
throw new SocketException(SocketExceptionEnum.SESSION_NOT_EXIST);
}
// 判断用户是否监听
if (socketSession.getMessageTypes().contains(msgType)) {
WebSocketMessagePOJO webSocketMessagePOJO = new WebSocketMessagePOJO();
webSocketMessagePOJO.setData(msg);
webSocketMessagePOJO.setType(msgType);
// 发送内容
socketSession.getSocketOperatorApi().writeAndFlush(webSocketMessagePOJO);
}
WebSocketMessagePOJO webSocketMessagePOJO = new WebSocketMessagePOJO();
webSocketMessagePOJO.setData(msg);
webSocketMessagePOJO.setServerMsgType(msgType);
// 发送内容
socketSession.getSocketOperatorApi().writeAndFlush(webSocketMessagePOJO);
}
@Override
@ -54,7 +50,7 @@ public class WebSocketOperator implements SocketOperatorApi {
for (SocketSession<GettySocketOperator> socketSession : socketSessionList) {
WebSocketMessagePOJO webSocketMessagePOJO = new WebSocketMessagePOJO();
webSocketMessagePOJO.setData(msg);
webSocketMessagePOJO.setType(msgType);
webSocketMessagePOJO.setServerMsgType(msgType);
// 发送内容
socketSession.getSocketOperatorApi().writeAndFlush(webSocketMessagePOJO);
}

View File

@ -59,7 +59,7 @@ public class WebSocketMessageHandler extends SimpleChannelInboundHandler<WebSock
WebSocketMessagePOJO webSocketMessagePOJO = JSON.toJavaObject(JSON.parseObject(data), WebSocketMessagePOJO.class);
// 心跳包
if (ClientMessageTypeEnum.USER_HEART.getCode().equals(webSocketMessagePOJO.getType())) {
if (ClientMessageTypeEnum.USER_HEART.getCode().equals(webSocketMessagePOJO.getClientMsgType())) {
// 更新用户最后活跃时间
String userId = ChannelIdAndUserBindCenter.getUserId(socketChannel.getChannelId());
if (ObjectUtil.isNotEmpty(userId)) {
@ -70,8 +70,6 @@ public class WebSocketMessageHandler extends SimpleChannelInboundHandler<WebSock
// 用户ID为空不处理直接跳过
if (ObjectUtil.isEmpty(webSocketMessagePOJO.getFormUserId())) {
ChannelIdAndUserBindCenter.closed(socketChannel.getChannelId());
socketChannel.close();
return;
}
@ -95,13 +93,13 @@ public class WebSocketMessageHandler extends SimpleChannelInboundHandler<WebSock
userSession.setLastActiveTime(System.currentTimeMillis());
// 找到该消息的处理器
SocketMsgCallbackInterface socketMsgCallbackInterface = SocketMessageCenter.getSocketMsgCallbackInterface(webSocketMessagePOJO.getType());
SocketMsgCallbackInterface socketMsgCallbackInterface = SocketMessageCenter.getSocketMsgCallbackInterface(webSocketMessagePOJO.getClientMsgType());
if (ObjectUtil.isNotEmpty(socketMsgCallbackInterface)) {
// 获取会话
SocketSession<GettySocketOperator> session = SessionCenter.getSessionByUserId(webSocketMessagePOJO.getFormUserId());
// 触发回调
socketMsgCallbackInterface.callback(webSocketMessagePOJO.getType(), webSocketMessagePOJO, session);
socketMsgCallbackInterface.callback(webSocketMessagePOJO.getClientMsgType(), webSocketMessagePOJO, session);
} else {
socketChannel.writeAndFlush(new TextWebSocketFrame("{\"code\":\"404\"}"));
}