liusuyi
2025-10-22 edda44f186e4d37f3ea521a531c38b3f0a43e5a9
优化tcp心跳消息发送和回复
2 files modified
8 ■■■■■ changed files
ard-work/src/main/java/com/ard/work/component/netty/TyTcpClientManager.java 6 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/netty/handler/ClientHandler.java 2 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/netty/TyTcpClientManager.java
@@ -49,7 +49,7 @@
                    @Override
                    protected void initChannel(SocketChannel socketChannel) {
                        ChannelPipeline pipeline = socketChannel.pipeline();
                        pipeline.addLast("heart", new IdleStateHandler(0, 3, 0, TimeUnit.SECONDS));
                        pipeline.addLast("heart", new IdleStateHandler(0, 5, 0, TimeUnit.SECONDS));
                        pipeline.addLast("encoder", new MessageProtocolEncoder());
                        pipeline.addLast("decoder", new MessageProtocolDecoder());
                        pipeline.addLast("handler", clientHandler);
@@ -143,9 +143,9 @@
    }
    /**
     * 定时向所有已连接的边缘服务器服务器发送心跳
     * 定时向所有已连接的边缘服务器服务器发送心跳(写空闲已经发心跳了,这里暂时取消)
     */
    @Scheduled(fixedRate = 5000) // 每5秒执行一次
   // @Scheduled(fixedRate = 5000) // 每5秒执行一次
    public void sendHeartBeat() {
        long timestamp = System.currentTimeMillis();
        TyTcpServerCache.getChannels().stream()
ard-work/src/main/java/com/ard/work/component/netty/handler/ClientHandler.java
@@ -68,7 +68,7 @@
                    // 写空闲时主动发心跳
                    HeartbeatCmd heartbeat = new HeartbeatCmd(timestamp);
                    ctx.writeAndFlush(heartbeat);
                    log.debug("写空闲 -> 发送心跳包 时间戳={}", timestamp);
                    log.info("写空闲 -> 发送心跳包 时间戳={}", timestamp);
                    break;
                case ALL_IDLE:
                    log.info("读写都空闲 - 可考虑关闭连接或重连");