content: package hxy.server.socket.engine;
  
  import io.netty.channel.ChannelHandlerContext;
  import io.netty.channel.ChannelInboundHandlerAdapter;
  import io.netty.handler.timeout.IdleState;
  import io.netty.handler.timeout.IdleStateEvent;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  /**
   * @ClassName HeartBeatServerHandler
   * @Description 心跳控制
   * @Author hxy
   * @Date 2020/4/8 19:12
   */
  public class HeartBeatServerHandler extends ChannelInboundHandlerAdapter {
      private static Logger logger = LoggerFactory.getLogger(HeartBeatServerHandler.class);
  
      private int lossConnectCount = 0;
  
      @Override
      public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
          if (evt instanceof IdleStateEvent) {
              IdleStateEvent event = (IdleStateEvent) evt;
              if (event.state() == IdleState.READER_IDLE) {
                  lossConnectCount++;
                  if (lossConnectCount > 2) {
                      logger.info("close when non-heart-beating:{}", ctx.channel().toString());
                      ctx.channel().close();
                  }
              }
          } else {
              super.userEventTriggered(ctx, evt);
          }
      }
  
      @Override
      public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
          lossConnectCount = 0;
      }
  
      @Override
      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
          ctx.close();
      }
  
  }
  