- 心跳包:不带消息体
- 客户端定时20秒发送心跳包 -> 服务端回应
- 客户端检查60秒内未读到消息,则认为断线,关闭连接,启动断线重连
- 服务端检查60秒内未读到消息,则认为断线,关闭连接,移除连接映射关系
Paste_Image.png
/**
* heart beat command
* Created by lif on 2015/10/10.
*/
public class HeartBeatHandler extends ChannelInboundHandlerAdapter implements MsgHandler {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.READER_IDLE) {
// Read timeout
System.out.println("READER_IDLE: read timeout from " + ctx.channel().remoteAddress());
String userId = CommonUtil.getLoginInfo(ctx.channel()).getUser_id();
ConnectionManager.removeConn(userId);
// close connection
if (ctx.channel().isOpen()) {
ctx.channel().close();
}
}
}
}
网友评论