jt-framework icon indicating copy to clipboard operation
jt-framework copied to clipboard

服务端无法根据心跳主动断开连接

Open wyheklh opened this issue 2 years ago • 1 comments

设置心跳值后,主动发送消息,服务端才会在指定时间间隔断掉连接,如果不发消息,连接就会一直保持,成为僵尸连接

wyheklh avatar May 21 '22 13:05 wyheklh

请先临时使用下面代码覆盖掉默认实现,下一个版本修复。

    @Bean(BEAN_NAME_NETTY_HANDLER_NAME_808_HEART_BEAT)
    public Jt808TerminalHeatBeatHandler heatBeatHandler(Jt808SessionManager jt808SessionManager) {
        return new Jt808TerminalHeatBeatHandler(jt808SessionManager) {
            @Override
            public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                if (!(evt instanceof IdleStateEvent)) {
                    super.userEventTriggered(ctx, evt);
                    return;
                }

                if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
                    log.debug("disconnected idle connection");
                    jt808SessionManager.removeBySessionIdAndClose(jt808SessionManager.generateSessionId(ctx.channel()), IDLE_TIMEOUT);
                }

                try {
                    ctx.channel().close();
                } catch (Exception ignored) {
                    // ignored
                }
            }
        };
    }

hylexus avatar May 22 '22 13:05 hylexus