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

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

@@ -49,10 +49,15 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
*/
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
String clientIp = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getClientIp();
logger.info("接收服务器响应msg:[" + msg + "]");
String clientIp = "no-ip";
try {
clientIp = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getClientIp();
logger.info("接收服务器响应msg:[" + msg + "]");
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
if (message == null || message.getMessageType() == null) {
logger.warn("收到无法解析的消息忽略。来源IP:[" + clientIp + "], 内容:[" + msg + "]");
return;
}
switch (message.getMessageType()) {
case HB:
HeartBeat hb = JsonUtils.decode(message.getMessage(),HeartBeat.class);
@@ -104,11 +109,14 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) {
String clientIP = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
logger.error("Client ip [" + clientIP + "] has inactive");
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
messageService.Offline(deviceName,venueId);
ChannelParam param = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get();
if (param != null && param.getVenueId() != null) {
String clientIP = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
logger.error("Client ip [" + clientIP + "] has inactive");
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
messageService.Offline(deviceName,venueId);
}
}
/**
@@ -121,11 +129,12 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.error("ServerHandler exceptionCaught",cause);
Channel channel = ctx.channel();
ChannelParam param = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get();
// Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
// String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
// DeviceType deviceType = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceType();
// messageService.Offline(deviceName,venueId,deviceType);
if(channel.isActive()) {
if(channel.isActive() && param != null && param.getVenueId() != null) {
// 错误产生,关闭连接
ctx.close();
}
@@ -141,10 +150,14 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
IdleState state = ((IdleStateEvent) evt).state();
if (state == IdleState.READER_IDLE){
logger.info("IdleStateEvent READER_IDLE 超时");
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
messageService.Offline(deviceName,venueId);
ctx.channel().close();
ChannelParam param = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get();
if (param != null && param.getVenueId() != null) {
// 原有逻辑
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
messageService.Offline(deviceName,venueId);
ctx.channel().close();
}
}
}
Set<String> connections = messageService.countConnection();

View File

@@ -416,7 +416,7 @@ public class ServerMessageHandlerAdapter implements MessageService {
* 判断是否有该通道
*/
public boolean contains(String deviceName, Integer venueId) {
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId + NettyConstant.SPIT_WORD;
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId;
return links.containsKey(clientId);
}