断链问题,防止客户端将设备误杀

This commit is contained in:
2026-04-08 16:35:27 +08:00
parent a1e22e9773
commit 9846990604
4 changed files with 39 additions and 24 deletions

View File

@@ -59,7 +59,7 @@ public class OldDoorService implements DoorService {
@Override
public void WatchEvent(ConnectorDetial connectorDetial, INData inData) {
logger.error("WatchEvent open door ...");
logger.info("WatchEvent open door ...");
}
};
}

View File

@@ -3,7 +3,6 @@ package com.sv.netty;
import com.sv.intergration.MessageService;
import com.sv.netty.config.VenueMessage;
import com.sv.netty.utils.JsonUtils;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@@ -53,8 +52,16 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
logger.info("get server msg:[" + msg + "]");
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
MessageService.getInstance().execute(message);
try {
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
if (message == null || message.getMessageType() == null) {
logger.error("收到无法解析的消息,忽略: " + msg);
return;
}
MessageService.getInstance().execute(message);
} catch (Exception e) {
logger.error("处理服务器消息异常,不影响连接: " + msg, e);
}
}
/**
@@ -66,7 +73,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws UnknownHostException {
if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.ALL_IDLE) {
if (event.state() == IdleState.WRITER_IDLE) {
ctx.write(MessageHandler.sendHeartBeat());
ctx.flush();
}
@@ -81,12 +88,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.info("ClientHandler exceptionCaught");
cause.printStackTrace();
Channel channel = ctx.channel();
if(channel.isActive()) {
ctx.close();
}
logger.error("ClientHandler exceptionCaught, 保持连接不关闭", cause);
}
}