liusuyi
2024-10-31 b821735241678a21ddc033aba04ebaf7ce65a3d2
ard-work/src/main/java/com/ard/tianyu/netty/handler/ClientHandler.java
@@ -1,8 +1,7 @@
package com.ard.tianyu.netty.handler;
import com.ard.tianyu.netty.TyNettyTcpClient;
import com.ard.tianyu.netty.command.BaseCommand;
import com.ard.tianyu.netty.command.HeartbeatCommand;
import com.ard.tianyu.netty.command.BaseCmd;
import com.ard.tianyu.service.ITyService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
@@ -19,7 +18,7 @@
@Component
@Slf4j
@ChannelHandler.Sharable
public class ClientHandler extends SimpleChannelInboundHandler<BaseCommand> {
public class ClientHandler extends SimpleChannelInboundHandler<BaseCmd> {
    @Resource
    private ITyService tyService;
@@ -28,11 +27,11 @@
    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();
        //tyService.processMessage(host + "/" + port, msg);
    public void channelRead0(ChannelHandlerContext ctx, BaseCmd command) {
        InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String host = ipSocket.getHostString();
        int port = ipSocket.getPort();
        tyService.processReplyMessage(host + ":" + port, command);
    }
    @Override
@@ -56,9 +55,9 @@
            if (event.state() == IdleState.READER_IDLE) {
                log.info("客户端读超时");
            } else if (event.state() == IdleState.WRITER_IDLE) {
                log.info("写空闲发送⼼跳!");
                HeartbeatCommand msg = new HeartbeatCommand(System.currentTimeMillis());
                ctx.writeAndFlush(msg);
              //  log.info("写空闲发送⼼跳!");
               // HeartbeatCommand msg = new HeartbeatCommand(System.currentTimeMillis());
                //ctx.writeAndFlush(msg);
            } else if (event.state() == IdleState.ALL_IDLE) {
                log.info("客户端所有操作超时");
            }
@@ -75,7 +74,7 @@
        ctx.channel().eventLoop().schedule(() -> {
            log.error("天宇服务端链接不上,开始重连操作...");
            try {
                tyNettyTcpClient.initOneConnection(host, port);
                tyNettyTcpClient.initConnection(host, port);
            } catch (Exception e) {
                log.error("天宇服务端连接异常: {}", e.getMessage());
            }