package com.mvc.netty;

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;

/**
 * 空闲状态检测
 */
public class MyIdleStateHandler extends ChannelInboundHandlerAdapter {
    private static Logger log = LoggerFactory.getLogger(MyIdleStateHandler.class);
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.ALL_IDLE) {
                log.info("通道空闲超时:[{}],isActive={}",ctx.channel().remoteAddress(),ctx.channel().isActive());
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}