Browse Source

netty日志优化

master
lijw 8 months ago
parent
commit
5cfdb89ea9
2 changed files with 18 additions and 6 deletions
  1. +2
    -2
      src/main/java/com/inspect/tcpserver/tcp/NettyServer.java
  2. +16
    -4
      src/main/java/com/inspect/tcpserver/tcp/NettyServerHandler.java

+ 2
- 2
src/main/java/com/inspect/tcpserver/tcp/NettyServer.java View File

@ -159,7 +159,7 @@ public class NettyServer {
allBuf.writeByte(0xEB);
allBuf.writeByte(0x90);
redisTemplate.opsForValue().set(String.valueOf(sendIndex), allBuf.toString(CharsetUtil.US_ASCII), 60L, TimeUnit.SECONDS);
nettyServerHandler.sendMsg(uuid, clientKey, allBuf, compact(xml));
nettyServerHandler.sendMsg(uuid, clientKey, allBuf, compact(xml), request);
sendIndex++;
} else {
logger.warn(Color.RED + "###### 客户端[{}]离线! ######" + Color.END, clientKey);
@ -188,7 +188,7 @@ public class NettyServer {
allBuf.writeByte(0xEB);
allBuf.writeByte(0x90);
redisTemplate.opsForValue().set(String.valueOf(sendIndex), allBuf.toString(CharsetUtil.US_ASCII), 60L, TimeUnit.SECONDS);
nettyServerHandler.sendMsg(uuid, client, allBuf, compact(xml));
nettyServerHandler.sendMsg(uuid, client, allBuf, compact(xml), request);
sendIndex++;
try {
Thread.sleep(1);


+ 16
- 4
src/main/java/com/inspect/tcpserver/tcp/NettyServerHandler.java View File

@ -18,19 +18,31 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
this.nettyServer = nettyServer;
}
public void sendMsg(String uuid, String clientKey, ByteBuf byteBuf, String xml) {
public void sendMsg(String uuid, String clientKey, ByteBuf byteBuf, String xml, boolean request) {
ChannelHandlerContext ctx = ChannelCache.getInstance().get(clientKey);
if(ctx != null) {
ctx.writeAndFlush(Unpooled.wrappedBuffer(byteBuf)).addListener(
(ChannelFuture future) -> {
if (future.isSuccess()) {
logger.info(Color.CYAN + "###### 活动连接:{},向客户端[{}]下发消息成功:{}######" + Color.END, ChannelCache.getInstance().getClients(), clientKey, xml);
if(request) {
logger.info(Color.CYAN + "###### 活动连接:{},向客户端[{}]下发消息成功:{}######" + Color.END, ChannelCache.getInstance().getClients(), clientKey, xml);
} else {
logger.info(Color.CYAN + "###### 活动连接:{},向客户端[{}]响应成功######" + Color.END, ChannelCache.getInstance().getClients(), clientKey);
}
} else {
logger.error(Color.RED + "###### 活动连接:{},向客户端[{}]下发消息失败:{}######" + Color.END, ChannelCache.getInstance().getClients(), clientKey, xml);
if(request) {
logger.error(Color.RED + "###### 活动连接:{},向客户端[{}]下发消息失败:{}######" + Color.END, ChannelCache.getInstance().getClients(), clientKey, xml);
} else {
logger.error(Color.RED + "###### 活动连接:{},向客户端[{}]响应失败######" + Color.END, ChannelCache.getInstance().getClients(), clientKey);
}
}
});
} else {
logger.error(Color.RED + "###### 活动连接:{},无法向客户端[{}]下发消息,ctx==null######" + Color.END, ChannelCache.getInstance().getClients(), clientKey);
if(request) {
logger.error(Color.RED + "###### 活动连接:{},无法向客户端[{}]下发消息,ctx==null######" + Color.END, ChannelCache.getInstance().getClients(), clientKey);
} else {
logger.error(Color.RED + "###### 活动连接:{},无法向客户端[{}]响应,ctx==null######" + Color.END, ChannelCache.getInstance().getClients(), clientKey);
}
}
}


Loading…
Cancel
Save