-
管道链配置心跳状态handler,参数一:读超时,参数二:写超时,参数三:所有超时(0表示不监测) @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); //10S心跳检测连接 pipeline.addLast("idle", new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS)); pipeline.addLast(group, "handler", dataDecodeHandler); }
- handler重写userEventTriggered(超时触发)
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.READER_IDLE) {//读超时 logger.error("10秒未收到客户端{}消息,关闭连接.", ctx.channel().remoteAddress().toString()); ctx.channel().close(); } else if (event.state() == IdleState.WRITER_IDLE) {//写 } else if (event.state() == IdleState.ALL_IDLE) {//全部 } } else { super.userEventTriggered(ctx, evt); } }
Netty-心跳检测配置
最新推荐文章于 2024-09-04 18:57:04 发布