From ce309155d4a0d750f786ca242b36bba8fc0bbf14 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Tue, 22 Oct 2024 17:26:43 +0800
Subject: [PATCH] 优化

---
 ard-work/src/main/java/com/ard/tianyu/netty/handler/ClientHandler.java |   42 ++++++++++++++++++++----------------------
 1 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/ard-work/src/main/java/com/ard/tianyu/netty/handler/ClientHandler.java b/ard-work/src/main/java/com/ard/tianyu/netty/handler/ClientHandler.java
index 00b850f..be26ca9 100644
--- a/ard-work/src/main/java/com/ard/tianyu/netty/handler/ClientHandler.java
+++ b/ard-work/src/main/java/com/ard/tianyu/netty/handler/ClientHandler.java
@@ -10,9 +10,7 @@
 import io.netty.handler.timeout.IdleState;
 import io.netty.handler.timeout.IdleStateEvent;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
-
 import javax.annotation.Resource;
 import java.net.InetSocketAddress;
 import java.util.concurrent.TimeUnit;
@@ -23,17 +21,17 @@
 @ChannelHandler.Sharable
 public class ClientHandler extends SimpleChannelInboundHandler<BaseCommand> {
 
-    @Autowired
-    ITyService tyService;
+    @Resource
+    private ITyService tyService;
 
     @Resource
     private TyNettyTcpClient tyNettyTcpClient;
 
     @Override
-    public void channelRead0(ChannelHandlerContext ctx, BaseCommand command){
-        InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
-        String host = ipSocket.getHostString();
-        int port = ipSocket.getPort();
+    public void channelRead0(ChannelHandlerContext ctx, BaseCommand command) {
+        //InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
+        //String host = ipSocket.getHostString();
+        //int port = ipSocket.getPort();
         //tyService.processMessage(host + "/" + port, msg);
     }
 
@@ -47,7 +45,7 @@
 
     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
-        cause.printStackTrace();
+        log.error("发生异常: ", cause); // 使用日志记录异常
         ctx.close();
     }
 
@@ -58,9 +56,9 @@
             if (event.state() == IdleState.READER_IDLE) {
                 log.info("客户端读超时");
             } else if (event.state() == IdleState.WRITER_IDLE) {
-//                HeartbeatCommand msg = new HeartbeatCommand(System.currentTimeMillis());
-//                log.info("发送⼼跳!");//1652345657305  1652345707631
-//                ctx.writeAndFlush(msg);
+                log.info("写空闲发送⼼跳!");
+                HeartbeatCommand msg = new HeartbeatCommand(System.currentTimeMillis());
+                ctx.writeAndFlush(msg);
             } else if (event.state() == IdleState.ALL_IDLE) {
                 log.info("客户端所有操作超时");
             }
@@ -72,18 +70,18 @@
         InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
         int port = ipSocket.getPort();
         String host = ipSocket.getHostString();
-        log.error("与天宇设备" + host + ":" + port + "连接断开!");
-        ctx.channel().eventLoop().schedule(new Runnable() {
-            @Override
-            public void run() {
-                log.error("服务端链接不上,开始重连操作...");
-                try {
-                    tyNettyTcpClient.initOneConnection(host, port);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
+        log.error("与天宇服务{}:{}连接断开!", host, port);
+
+        ctx.channel().eventLoop().schedule(() -> {
+            log.error("天宇服务端链接不上,开始重连操作...");
+            try {
+                tyNettyTcpClient.initOneConnection(host, port);
+            } catch (Exception e) {
+                log.error("天宇服务端连接异常: {}", e.getMessage());
             }
         }, 5, TimeUnit.SECONDS);
+
         super.channelInactive(ctx);
     }
+
 }

--
Gitblit v1.9.3