[小项目]-netty实现聊天功能
2020-03-19 11:12 Spiderman25 阅读(75) 评论(0) 收藏 举报这个已经看不到了
https://blog.csdn.net/u010853261/article/details/54380866
这个能看
https://www.cnblogs.com/coderJiebao/p/Netty11.html
https://www.cnblogs.com/spiderman25/articles/18028499
在服务端SimpleChatServerHandler添加如下代码
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { IdleStateEvent event = (IdleStateEvent)evt; String eventType = null; switch (event.state()){ case READER_IDLE: eventType = "读空闲"; readIdleTimes ++; // 读空闲的计数加1 break; case WRITER_IDLE: eventType = "写空闲"; // 不处理 break; case ALL_IDLE: eventType ="读写空闲"; // 不处理 break; } System.out.println(ctx.channel().remoteAddress() + "超时事件:" +eventType); //if(readIdleTimes > 3){ System.out.println(" [server]读空闲超过3次,关闭连接"); ctx.channel().writeAndFlush("you are out\n"); ctx.channel().close(); //} }
客户端重载除了以上方法。
当客户端上线时,服务器执行顺序
handlerAdded
channelActive
当客户端超时时,服务器执行顺序
userEventTriggered,执行完ctx.channel().close()后,channels就会减少
channelInactive
handlerRemoved
当客户端被关闭时,服务器执行顺序
exceptionCaught
channelInactive,这时channels已经看到少了
handlerRemoved
当客户端上线时,客户端执行顺序
handlerAdded
channelActive
当客户端超时时,客户端执行顺序
channelRead0
channelInactive
handlerRemoved
当服务器被关闭时,客户端执行顺序
exceptionCaught
channelInactive
handlerRemoved