|
@@ -28,16 +28,15 @@ console.log('SOCKET_PATH', process.env.SOCKET_PATH);
|
|
|
// parser: require('socket.io-msgpack-parser'),
|
|
|
})
|
|
|
export class SocketGateway
|
|
|
- implements OnGatewayInit, OnGatewayDisconnect, OnGatewayConnection
|
|
|
-{
|
|
|
+ implements OnGatewayInit, OnGatewayDisconnect, OnGatewayConnection {
|
|
|
constructor(
|
|
|
@InjectRedis() private readonly redis: Redis,
|
|
|
private readonly roomService: RoomService,
|
|
|
private readonly delayService: DelayService,
|
|
|
- ) {}
|
|
|
+ ) { }
|
|
|
public readonly logger = new Logger('socketGateway');
|
|
|
@WebSocketServer() server: Server;
|
|
|
- public _loginLimit = new Map<string, string>();
|
|
|
+ // public _loginLimit = new Map<string, string>();
|
|
|
|
|
|
async handleConnection(@ConnectedSocket() socket: Socket, ...args: any[]) {
|
|
|
// this.delayService
|
|
@@ -45,9 +44,12 @@ export class SocketGateway
|
|
|
async handleDisconnect(@ConnectedSocket() socket: Socket) {
|
|
|
const deviceId = socket.data.deviceId;
|
|
|
if (deviceId) {
|
|
|
- const did = this._loginLimit.get(deviceId);
|
|
|
+ const redisFlag = `kankan:socket:deviceId`;
|
|
|
+ // const did = this._loginLimit.get(deviceId);
|
|
|
+ const did = await this.redis.hget(redisFlag, deviceId);
|
|
|
if (did === socket.id) {
|
|
|
- this._loginLimit.delete(deviceId);
|
|
|
+ await this.redis.hdel(redisFlag, deviceId);
|
|
|
+ // this._loginLimit.delete(deviceId);
|
|
|
}
|
|
|
}
|
|
|
await this.roomService.handleUserOffline(socket);
|
|
@@ -76,26 +78,33 @@ export class SocketGateway
|
|
|
// (socket as any).userId = message.userId;
|
|
|
// (socket as any).roomId = message.roomId;
|
|
|
socket.data.user = message;
|
|
|
- const isRepeat = this.handleRepeatJoin(socket, message);
|
|
|
+ const isRepeat = await this.handleRepeatJoin(socket, message);
|
|
|
+ // console.log('isRepeat', isRepeat);
|
|
|
if (!isRepeat) {
|
|
|
socket.join(message.roomId);
|
|
|
await this.roomService.handleUserJoin(socket, message);
|
|
|
}
|
|
|
}
|
|
|
- handleRepeatJoin(socket: Socket, message: UserInfoParams): boolean {
|
|
|
+ async handleRepeatJoin(
|
|
|
+ socket: Socket,
|
|
|
+ message: UserInfoParams,
|
|
|
+ ): Promise<boolean> {
|
|
|
const from = message.isClient ? 0 : 1;
|
|
|
const flag = `${message.userId}-${from}`;
|
|
|
socket.data.deviceId = flag;
|
|
|
- if (!this._loginLimit.has(flag)) {
|
|
|
- this._loginLimit.set(flag, socket.id);
|
|
|
- return false;
|
|
|
+ const redisFlag = `kankan:socket:deviceId`;
|
|
|
+ const isExist = await this.redis.hexists(redisFlag, flag);
|
|
|
+ if (!isExist) {
|
|
|
+ await this.redis.hset(redisFlag, flag, socket.id);
|
|
|
+ // this._loginLimit.set(flag, socket.id);
|
|
|
+ return Promise.resolve(false);
|
|
|
} else {
|
|
|
socket.emit('manager-error', {
|
|
|
type: 'repeat-login',
|
|
|
code: 306,
|
|
|
});
|
|
|
this.logger.warn(`306:${message.userId}`, 'repeat-login');
|
|
|
- return true;
|
|
|
+ return Promise.resolve(true);
|
|
|
}
|
|
|
}
|
|
|
|