新增错误类型返回

This commit is contained in:
wenc000 2020-08-01 20:32:20 +08:00
parent 2005b31ae9
commit b67523690f
2 changed files with 29 additions and 5 deletions

View File

@ -1,10 +1,13 @@
package com.cm.central.control.client.socket.handler;
import com.alibaba.fastjson.JSONObject;
import com.cm.central.control.client.socket.config.properties.SocketClientProperties;
import com.cm.central.control.client.socket.manager.SocketClientManager;
import com.cm.central.control.client.socket.socket.SocketClientRunnable;
import com.cm.socket.enums.SocketCodeEnum;
import com.cm.socket.enums.SocketTypeMessageEnum;
import com.cm.socket.pojo.Message;
import com.cm.socket.pojo.SocketResult;
import com.cm.socket.service.ISocketService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@ -41,15 +44,24 @@ public class SocketClientHandler extends SimpleChannelInboundHandler<Message> {
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Message message) throws Exception {
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Message message) throws Exception {
if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_PONG.getType()) {
pingServiceImpl.readMessage(ctx, message);
pingServiceImpl.readMessage(channelHandlerContext, message);
} else if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_CLIENT_LOGIN.getType()) {
loginServiceImpl.readMessage(ctx, message);
loginServiceImpl.readMessage(channelHandlerContext, message);
} else if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_EXCEPTION.getType()) {
exceptionServiceImpl.readMessage(ctx, message);
exceptionServiceImpl.readMessage(channelHandlerContext, message);
} else if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_ERROR.getType()) {
errorServiceImpl.readMessage(ctx, message);
errorServiceImpl.readMessage(channelHandlerContext, message);
} else {
SocketResult<String> socketResult = new SocketResult<>();
socketResult.setCode(SocketCodeEnum.ERROR.getCode());
socketResult.setErrorMsg("消息类型暂不支持");
Message resultMessage = new Message();
resultMessage.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
resultMessage.setType(SocketTypeMessageEnum.MESSAGE_TYPE_ERROR.getType());
resultMessage.setContent(JSONObject.toJSONString(socketResult));
channelHandlerContext.channel().writeAndFlush(message);
}
}

View File

@ -1,8 +1,11 @@
package com.cm.central.control.handler;
import com.alibaba.fastjson.JSONObject;
import com.cm.central.control.manager.SocketServerManager;
import com.cm.socket.enums.SocketCodeEnum;
import com.cm.socket.enums.SocketTypeMessageEnum;
import com.cm.socket.pojo.Message;
import com.cm.socket.pojo.SocketResult;
import com.cm.socket.service.ISocketService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@ -43,6 +46,15 @@ public class SocketServerHandler extends SimpleChannelInboundHandler<Message> {
socketClientExceptionServiceImpl.readMessage(channelHandlerContext, message);
} else if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_CLIENT_INFO.getType()) {
socketClientInfoServiceImpl.readMessage(channelHandlerContext, message);
} else {
SocketResult<String> socketResult = new SocketResult<>();
socketResult.setCode(SocketCodeEnum.ERROR.getCode());
socketResult.setErrorMsg("消息类型暂不支持");
Message resultMessage = new Message();
resultMessage.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
resultMessage.setType(SocketTypeMessageEnum.MESSAGE_TYPE_ERROR.getType());
resultMessage.setContent(JSONObject.toJSONString(socketResult));
channelHandlerContext.channel().writeAndFlush(message);
}
}