/** * 检测掉线状况,给逻辑服调用,如果socket已经断开,则调用logout接口 * * @return * @throws IOException */ public Response checkOnline() throws IOException { String userSessionId = UserSessionManager.getInstance().getUserSessionId(uid); Session userSession = DefaultSessionManager.getInstance().getSession(userSessionId); if (userSession == null || userSession.getChannel() == null || !userSession.getChannel().isConnected()) { Message msg = response.getMessage(); msg.setAct("User.logout"); msg.setAttribate("uid", uid); response.setMessage(msg); return response; } else { return null; } }
/** * 用户退出,清除userToken、Session并断开连接 * * @return */ public Response logout() { Session session = request.getSession(); UserToken userToken = (UserToken) session.getAttribute("userToken"); // Session logicSession = // ServerConnectorMgr.getInstance().getServerSession("logic"); // if (logicSession != null) { // // 发送退出消息到逻辑服 // Message msg = response.getMessage(); // msg.setAct("User.logout"); // msg.setAttribate("uid", userToken.getUserId()); // try { // logicSession.send(msg); // } catch (IOException e) { // LOG.error(e.getMessage(), e); // } // } // 清楚userToken和session TokenManager.getInstance().removeToken(userToken.getToken()); DefaultSessionManager.getInstance().closeSession(session); return null; }
/** * 踢掉同一个用户的其它登陆链接 * * @param userToken */ private void kickoutOtherSession(UserToken userToken) { String sessionId = UserSessionManager.getInstance().getUserSessionId(userToken.getUserId()); Session session = DefaultSessionManager.getInstance().getSession(sessionId); if (session != null) { Message msg = new Message(); msg.setAct("error"); msg.setRc(CodeConstants.KICKOUT); try { ChannelFuture future = session.send(msg); future.addListener( new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { future.getChannel().close(); } }); } catch (IOException e) { LOG.error(e.getMessage(), e); } } }