/** * 登陆请求 * * @return * @throws IOException */ public Response login() throws IOException { initTestToken(); UserToken userToken = checkUserLogin(tk, getUserId(), pid, sid); if (userToken != null) { // 暂时屏蔽 kickoutOtherSession(userToken); UserSessionManager.getInstance() .setUserSession(userToken.getUserId(), request.getSession().getSid()); Session session = ServerConnectorMgr.getInstance().getServerSession("logic"); if (session == null) { LOG.info("无法获取服务器连接session"); } InetSocketAddress address = (InetSocketAddress) request.getSession().getChannel().getRemoteAddress(); Message msg = getProxyMessage(); msg.setAct("User.login"); // ut暂时无用 msg.setAttribate("ut", ut); msg.setAttribate("tk", tk); msg.setAttribate("ip", address.getHostName()); msg.setAttribate("uid", userToken.getUserId()); request.getSession().setAttribute("userToken", userToken); session.send(msg); } return null; }
public Response reLogin() throws IOException { // initTestToken(); // tk = "tk00000013"; UserToken userToken = TokenManager.getInstance().getToken(tk); if (userToken == null && tk != null) { OfflineUserToken offlineUserToken = OfflineTokenManager.getInstance().getToken(tk); // 离线不超过一个小时可以再次登录 if (offlineUserToken != null && System.currentTimeMillis() - offlineUserToken.getLogoutTime() < 12 * 60 * 60 * 1000) { userToken = new UserToken(); userToken.setUserId(offlineUserToken.getUserId()); userToken.setPartnerUserId(offlineUserToken.getPartnerUserId()); userToken.setServerId(offlineUserToken.getServerId()); userToken.setPartnerId(offlineUserToken.getPartnerId()); userToken.setToken(offlineUserToken.getToken()); TokenManager.getInstance().setToken(userToken.getToken(), userToken); // 从离线那清掉 OfflineTokenManager.getInstance().removeToken(tk); } } if (userToken == null) { throw new ServiceException( LOGIN_NOT_LOGIN, "userToken为空[" + tk + ":" + getUserId() + ":" + pid + "]"); } // UserToken userToken = checkUserLogin(tk, getUserId(), pid, sid); if (userToken != null) { // 暂时屏蔽 kickoutOtherSession(userToken); UserSessionManager.getInstance() .setUserSession(userToken.getUserId(), request.getSession().getSid()); Session session = ServerConnectorMgr.getInstance().getServerSession("logic"); if (session == null) { LOG.info("无法获取服务器连接session"); } InetSocketAddress address = (InetSocketAddress) request.getSession().getChannel().getRemoteAddress(); Message msg = getProxyMessage(); msg.setAct("User.reLogin"); msg.setAttribate("ut", ut); msg.setAttribate("tk", tk); msg.setAttribate("ip", address.getHostName()); msg.setAttribate("uid", userToken.getUserId()); request.getSession().setAttribute("userToken", userToken); session.send(msg); } return null; }
/** * 检测掉线状况,给逻辑服调用,如果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; } }
/** * 踢掉同一个用户的其它登陆链接 * * @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); } } }