use of io.netty.handler.timeout.IdleStateEvent in project transporter by wang4ever.
the class AbstractChannelMessageHandler method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
IdleStateEvent event = (IdleStateEvent) evt;
logger.warn("连接空闲. {}, client={}", event.state(), this.client.asText());
switch(event.state()) {
case // 读超时事件:可能客户端设备No-run或断线, 为节省资源则close.
READER_IDLE:
this.closeClient();
break;
case // 写超时事件:可能此连接上无业务同时也未收到心跳, 为节省资源则close.
WRITER_IDLE:
this.closeClient();
break;
case // 读&写:异常情况(可能客户端设备No-run或断线), 为节省资源则close.
ALL_IDLE:
this.closeClient();
break;
default:
throw new UnsupportedOperationException("不支持的空闲检测类型.");
}
}
super.userEventTriggered(ctx, evt);
}
use of io.netty.handler.timeout.IdleStateEvent 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);
}
use of io.netty.handler.timeout.IdleStateEvent in project lightconf by haifeiWu.
the class ClientHandler method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
switch(e.state()) {
case WRITER_IDLE:
PingMsg pingMsg = new PingMsg();
ctx.writeAndFlush(pingMsg);
break;
default:
break;
}
}
}
use of io.netty.handler.timeout.IdleStateEvent in project pancm_project by xuwujing.
the class NettyClientHandlerDemo5 method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
System.out.println("循环触发时间:" + MyTools.getNowTime(""));
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
System.out.println("event.state():" + event.state() + ",IdleState.READER_IDLE:" + IdleState.READER_IDLE);
if (event.state() == IdleState.WRITER_IDLE) {
System.out.println("TRY_TIMES:" + TRY_TIMES);
if (currentTime <= TRY_TIMES) {
System.out.println("currentTime:" + currentTime);
currentTime++;
ctx.channel().writeAndFlush(HEARTBEAT_SEQUENCE.duplicate());
}
}
}
}
use of io.netty.handler.timeout.IdleStateEvent in project pancm_project by xuwujing.
the class NettyServerHandlerDemo5 method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
System.out.println("event.state():" + event.state() + ",IdleState.READER_IDLE:" + IdleState.READER_IDLE);
if (event.state() == IdleState.READER_IDLE) {
loss_connect_time++;
System.out.println("5 秒没有接收到客户端的信息了");
if (loss_connect_time > 2) {
System.out.println("关闭这个不活跃的channel");
ctx.channel().close();
}
}
} else {
super.userEventTriggered(ctx, evt);
}
}
Aggregations