作者 crossoverJie

:sparkles: Introducing new features.服务端下线逻辑优化

@@ -34,9 +34,11 @@ public class RouteHandler { @@ -34,9 +34,11 @@ public class RouteHandler {
34 * @throws IOException 34 * @throws IOException
35 */ 35 */
36 public void userOffLine(CIMUserInfo userInfo, NioSocketChannel channel) throws IOException { 36 public void userOffLine(CIMUserInfo userInfo, NioSocketChannel channel) throws IOException {
37 - LOGGER.info("用户[{}]下线", userInfo.getUserName()); 37 + if (userInfo != null){
  38 + LOGGER.info("用户[{}]下线", userInfo.getUserName());
  39 + SessionSocketHolder.removeSession(userInfo.getUserId());
  40 + }
38 SessionSocketHolder.remove(channel); 41 SessionSocketHolder.remove(channel);
39 - SessionSocketHolder.removeSession(userInfo.getUserId());  
40 42
41 //清除路由关系 43 //清除路由关系
42 clearRouteInfo(userInfo); 44 clearRouteInfo(userInfo);
@@ -39,7 +39,9 @@ public class ServerHeartBeatHandlerImpl implements HeartBeatHandler { @@ -39,7 +39,9 @@ public class ServerHeartBeatHandlerImpl implements HeartBeatHandler {
39 long now = System.currentTimeMillis(); 39 long now = System.currentTimeMillis();
40 if (lastReadTime != null && now - lastReadTime > heartBeatTime){ 40 if (lastReadTime != null && now - lastReadTime > heartBeatTime){
41 CIMUserInfo userInfo = SessionSocketHolder.getUserId((NioSocketChannel) ctx.channel()); 41 CIMUserInfo userInfo = SessionSocketHolder.getUserId((NioSocketChannel) ctx.channel());
42 - LOGGER.warn("客户端[{}]心跳超时[{}]ms,需要关闭连接!",userInfo.getUserName(),now - lastReadTime); 42 + if (userInfo != null){
  43 + LOGGER.warn("客户端[{}]心跳超时[{}]ms,需要关闭连接!",userInfo.getUserName(),now - lastReadTime);
  44 + }
43 routeHandler.userOffLine(userInfo, (NioSocketChannel) ctx.channel()); 45 routeHandler.userOffLine(userInfo, (NioSocketChannel) ctx.channel());
44 ctx.channel().close(); 46 ctx.channel().close();
45 } 47 }