登录:当账号登录的时候,将已登录玩家踢下

This commit is contained in:
luying
2022-06-18 17:36:11 +08:00
parent a7b4cbd7a0
commit e49c35fa4d
5 changed files with 61 additions and 14 deletions
@@ -9,7 +9,7 @@ import { genCode, generateStr, resResult } from '../../../pubUtils/util';
import { COM_BTL_QUALITY, HERO_SELECT, DEBUG_MAGIC_WORD, REDIS_KEY, TASK_TYPE, ENTERY_ROLE_PICK, COUNTER, DEFAULT_LV, TA_USERSET_TYPE, LOG_TYPE, JEWEL_SELECT, ITEM_SELECT, SKIN_SELECT, PUSH_ROUTE } from '../../../consts';
// import { loginRefresh } from '../../../services/playerEventService';
import { nowSeconds, getZeroPoint } from '../../../pubUtils/timeUtil';
import { rmRoleFromQueue, roleLeave, getRoleOnlineInfo, roleLogin } from '../../../services/redisService';
import { rmRoleFromQueue, roleLeave, getRoleOnlineInfo, roleLogin, getOnlineRoleByUserCode } from '../../../services/redisService';
import { addRoleToGuildChannel, addRoleToSysChannel, addRoleToWorldChannel, leaveGuildAuctionChannel, leaveGuildChannel, leaveSysChannel, leaveWorldAuctionChannel, leaveWorldChannel, recentGuildMsgs, recentPrivateChatInfos, recentSysMsgs, recentWorldMsgs } from '../../../services/chatService';
import { reportOneOnline, savePlayTime } from '../../../services/authenticateService';
@@ -47,21 +47,22 @@ export class EntryHandler {
console.log('user token not found');
return resResult(STATUS.TOKEN_ERR);
}
let onlineRoleId = await getOnlineRoleByUserCode(user.userCode);
if (!!onlineRoleId) { // 多地登陆踢下线
let connect = await getRoleOnlineInfo(onlineRoleId);
if (connect.sid = self.app.getServerId()) {
await kickUser(self.app, onlineRoleId);
} else {
await self.app.rpc.connector.connectorRemote.remoteLogin.toServer(connect.sid, onlineRoleId);
}
}
let role = await RoleModel.findByUidAndSetTime(user.uid, serverId, null, true);
if (!role) {
return resResult(STATUS.ROLE_NOT_FOUND);
}
let connect = await getRoleOnlineInfo(role.roleId);
if (connect.isOnline) { // 多地登陆踢下线
if (connect.sid = self.app.getServerId()) {
await kickUser(self.app, role.roleId);
} else {
await self.app.rpc.connector.connectorRemote.remoteLogin.toServer(connect.sid, role.roleId);
}
}
let serverName = this.app.getServerId();
await roleLogin(role.roleId, user.userCode, serverName, user.pkgName, role.createTime, role.serverId); // 保存在线用户
await this.addSession(user, role, session);
+13 -1
View File
@@ -312,6 +312,7 @@ export async function clearChannelServers() {
*/
export async function roleLogin(roleId: string, userCode: string, sid: string, pkgName: string, createTime: number, serverId: number) {
await redisClient().hincrbyAsync(REDIS_KEY.ONLINE_CNT, roleId, 1);
await redisClient().hsetAsync(REDIS_KEY.USER_CODE, userCode, roleId);
return await redisClient().hsetAsync(REDIS_KEY.ONLINE_USERS, roleId, `${userCode}|${sid}|${pkgName}|${createTime}|${serverId}`);
}
@@ -323,7 +324,9 @@ export async function roleLeave(roleId: string) {
let role = await getRoleOnlineInfo(roleId);
let count = await redisClient().hincrbyAsync(REDIS_KEY.ONLINE_CNT, roleId, -1);
if(count <= 0) {
await redisClient().hdelAsync(REDIS_KEY.ONLINE_CNT, roleId);
await redisClient().hdelAsync(REDIS_KEY.ONLINE_USERS, roleId);
await redisClient().hdelAsync(REDIS_KEY.USER_CODE, role.userCode);
}
return role;
}
@@ -337,6 +340,11 @@ export async function isRoleOnline(roleId: string) {
return !!result;
}
export async function getOnlineRoleByUserCode(userCode: string) {
let roleId = await redisClient().hgetAsync(REDIS_KEY.USER_CODE, userCode);
return roleId;
}
/**
* 获得在线玩家userCode和sid
* @param roleId
@@ -550,7 +558,8 @@ export async function redisSubScribe() {
let treatRoleChannel = getRedisSubChannel(REDIS_KEY.TREAT_ROLE_CHANNEL, env);
let treatGuildChannel = getRedisSubChannel(REDIS_KEY.TREAT_GUILD_CHANNEL, env);
let surveyChannel = getRedisSubChannel(REDIS_KEY.SURVEY_CHANNEL, env);
await redisClientPub().subscribeAsync(payChannel, treatRoleChannel, treatGuildChannel, refundChannel, surveyChannel);
let userChannel = getRedisSubChannel(REDIS_KEY.USER_CHANNEL, env);
await redisClientPub().subscribeAsync(payChannel, treatRoleChannel, treatGuildChannel, refundChannel, surveyChannel, userChannel);
redisClientPub().on('message', (channel, message) => {
console.log('**********redisSubScribe*******')
console.log('channel: ', channel, payChannel);
@@ -575,6 +584,9 @@ export async function redisSubScribe() {
let servers = pinus.app.getServersByType('role');
let server = getRandSingleEelm(servers);
pinus.app.rpc.role.roleRemote.sendSurveyMail.toServer(server.id, message);
} else if(channel == userChannel) {
let [sid, roleId] = (message||'').split('|');
if(!!sid && !!roleId) pinus.app.rpc.connector.connectorRemote.remoteLogin.toServer(sid, roleId);
}
});
}