|
@@ -7,6 +7,7 @@ import * as duration from 'dayjs/plugin/duration';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { UsersService } from '../users/users.service';
|
|
import { UsersService } from '../users/users.service';
|
|
import { RoomService } from '../room.service';
|
|
import { RoomService } from '../room.service';
|
|
|
|
+import { TempService } from '../temp/temp.service';
|
|
dayjs.extend(duration);
|
|
dayjs.extend(duration);
|
|
@Injectable()
|
|
@Injectable()
|
|
export class DelayService {
|
|
export class DelayService {
|
|
@@ -17,6 +18,7 @@ export class DelayService {
|
|
private readonly userService: UsersService,
|
|
private readonly userService: UsersService,
|
|
@Inject(forwardRef(() => RoomService))
|
|
@Inject(forwardRef(() => RoomService))
|
|
public readonly roomService: RoomService,
|
|
public readonly roomService: RoomService,
|
|
|
|
+ public readonly tempService: TempService,
|
|
) {}
|
|
) {}
|
|
|
|
|
|
init(): void {
|
|
init(): void {
|
|
@@ -28,40 +30,65 @@ export class DelayService {
|
|
this.subRedis.subscribe(subscribeEvent);
|
|
this.subRedis.subscribe(subscribeEvent);
|
|
this.subRedis.on('message', (channel, key) => {
|
|
this.subRedis.on('message', (channel, key) => {
|
|
if (channel === subscribeEvent) {
|
|
if (channel === subscribeEvent) {
|
|
- this.handleExpiredSubscribe(key);
|
|
|
|
|
|
+ this.handleExpiredKeys(key);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
- async handleExpiredSubscribe(key: string) {
|
|
|
|
- if (key.includes('kankan:socket:delay')) {
|
|
|
|
- const tKey = key.replace('kankan:socket:delay:', '');
|
|
|
|
- const params = tKey.split(':');
|
|
|
|
- const RoomId = params[0];
|
|
|
|
- const UserId = params[1];
|
|
|
|
- const delayUser = await this.userService.getUsersBy(RoomId, UserId);
|
|
|
|
- if (delayUser) {
|
|
|
|
- const roomUsers = await this.userService.getRoomUsers(RoomId);
|
|
|
|
- const filterRoomUser = roomUsers.filter((i) => i.UserId !== UserId);
|
|
|
|
- console.log('delayUser', delayUser.UserId);
|
|
|
|
- const res = await this.userService.deleteRoomUser(RoomId, UserId);
|
|
|
|
- if (res) {
|
|
|
|
- if (delayUser.Role === 'leader') {
|
|
|
|
- this.roomService.handleRoomDismiss(RoomId);
|
|
|
|
- } else {
|
|
|
|
- this.roomService.socketGateway.server.to(RoomId).emit('action', {
|
|
|
|
- type: 'user-exit',
|
|
|
|
- user: delayUser,
|
|
|
|
- members: filterRoomUser,
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- this.roomService.socketGateway.server.sockets.sockets.forEach(
|
|
|
|
- (soc) => {
|
|
|
|
- if (soc.id === delayUser.id) {
|
|
|
|
- soc.disconnect(true);
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- );
|
|
|
|
|
|
+ async handleExpiredKeys(key: string) {
|
|
|
|
+ switch (true) {
|
|
|
|
+ //过期下线的
|
|
|
|
+ case this.isContainKeys(key, 'kankan:socket:delay'):
|
|
|
|
+ this.handleOfflineExpiredSubscribe(key);
|
|
|
|
+ break;
|
|
|
|
+ //临时房间下线的
|
|
|
|
+ case this.isContainKeys(key, 'kankan:socket:tempRoom'):
|
|
|
|
+ this.handleTempRoomExpiredSubscribe(key);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ isContainKeys(key: string, exist: string) {
|
|
|
|
+ return key.indexOf(exist) > -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async handleTempRoomExpiredSubscribe(key: string) {
|
|
|
|
+ const tKey = key.replace('kankan:socket:tempRoom:', '');
|
|
|
|
+ const params = tKey.split(':');
|
|
|
|
+ const RoomId = params[0];
|
|
|
|
+ // const UserId = params[1];
|
|
|
|
+ this.roomService.logger.warn(`解散临时房间 :${RoomId}`, 'temp-room');
|
|
|
|
+ await this.roomService.handleRoomDismiss(RoomId);
|
|
|
|
+ this.roomService.socketGateway.server.in(RoomId).disconnectSockets();
|
|
|
|
+ }
|
|
|
|
+ async handleOfflineExpiredSubscribe(key: string) {
|
|
|
|
+ const tKey = key.replace('kankan:socket:delay:', '');
|
|
|
|
+ const params = tKey.split(':');
|
|
|
|
+ const RoomId = params[0];
|
|
|
|
+ const UserId = params[1];
|
|
|
|
+ const delayUser = await this.userService.getUsersBy(RoomId, UserId);
|
|
|
|
+ if (delayUser) {
|
|
|
|
+ const roomUsers = await this.userService.getRoomUsers(RoomId);
|
|
|
|
+ const filterRoomUser = roomUsers.filter((i) => i.UserId !== UserId);
|
|
|
|
+ console.log('delayUser', delayUser.UserId);
|
|
|
|
+ const res = await this.userService.deleteRoomUser(RoomId, UserId);
|
|
|
|
+ if (res) {
|
|
|
|
+ if (delayUser.Role === 'leader') {
|
|
|
|
+ this.roomService.handleRoomDismiss(RoomId);
|
|
|
|
+ } else {
|
|
|
|
+ this.roomService.socketGateway.server.to(RoomId).emit('action', {
|
|
|
|
+ type: 'user-exit',
|
|
|
|
+ user: delayUser,
|
|
|
|
+ members: filterRoomUser,
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
+ this.roomService.socketGateway.server.sockets.sockets.forEach((soc) => {
|
|
|
|
+ if (soc.id === delayUser.id) {
|
|
|
|
+ soc.disconnect(true);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|