Search in sources :

Example 1 with ActiveMessage

use of io.transport.sdk.protocol.message.internal.ActiveMessage in project transporter by wang4ever.

the class AbstractMessageHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        getLogger().warn("连接空闲. " + ctx.channel() + ", " + event.state());
        switch(event.state()) {
            case // 读超时事件:可能此通道无业务或断线, 发送心跳检查一下.
            READER_IDLE:
                ctx.channel().writeAndFlush(new ActiveMessage());
                break;
            case // 写超时事件:异常情况(连心跳都未发送,不易触发), 直接close重连.
            WRITER_IDLE:
                // 重连
                this.reconnect(ctx);
                break;
            case // 读&写事件:异常情况(连心跳都未发送,不易触发), 直接close重连.
            ALL_IDLE:
                // 重连
                this.reconnect(ctx);
                break;
            default:
                throw new UnsupportedOperationException("不支持的空闲检测类型.");
        }
    }
    super.userEventTriggered(ctx, evt);
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) ActiveMessage(io.transport.sdk.protocol.message.internal.ActiveMessage)

Aggregations

IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1 ActiveMessage (io.transport.sdk.protocol.message.internal.ActiveMessage)1