gemercheung 2 tahun lalu
induk
melakukan
660ab818fa

+ 77 - 2
src/room-manager/room-manager.controller.ts

@@ -80,8 +80,83 @@ export class RoomManagerController {
         count: roomUsers.length,
       };
     }
+  }
+  @Get('/valid/:id')
+  @ApiOperation({ summary: '获取当前房间是否存在' })
+  @ApiParam({
+    name: 'id',
+    required: true,
+    description: '房间ID',
+    schema: { oneOf: [{ type: 'string' }] },
+    type: 'string',
+  })
+  @ApiResponse({
+    description: 'The record has been successfully created.',
+    type: Boolean,
+  })
+  async getRoomValid(@Param('id') id: string) {
+    this.roomService.logger.log(`roomId: ${id}`, 'api-getRoomValid');
+    if (id) {
+      const isValidRoom = await this.usersService.isValidRoom(id);
+      console.log('isValidRoom', isValidRoom);
+      return isValidRoom;
+    }
+  }
 
-    // const res = await this.roomService.handleRoomDismiss(id);
-    // return res;
+  @Get('/kick/:id/:userId')
+  @ApiOperation({ summary: '将用户踢出房间' })
+  @ApiParam({
+    name: 'id',
+    required: true,
+    description: '房间ID',
+    schema: { oneOf: [{ type: 'string' }] },
+    type: 'string',
+  })
+  @ApiParam({
+    name: 'userId',
+    required: true,
+    description: '用户ID',
+    schema: { oneOf: [{ type: 'string' }] },
+    type: 'string',
+  })
+  @ApiResponse({
+    description: 'The record has been successfully created.',
+    type: Boolean,
+  })
+  async getRoomUserKicked(
+    @Param('id') id: string,
+    @Param('userId') userId: string,
+  ) {
+    this.roomService.logger.log(
+      `roomId: ${id}userId:${userId}`,
+      'api-getRoomUserKicked',
+    );
+    if (id && userId) {
+      const roomId = id;
+      this.roomService.handleKickAction(roomId, userId);
+      // const delUser = await this.usersService.getUsersBy(roomId, userId);
+      // this.roomService.logger.warn(
+      //   `RoomId: ${roomId},userId:${userId} socketId:${delUser.id}`,
+      //   'kick-user',
+      // );
+      // const roomUsers = await this.usersService.getRoomUsers(roomId);
+      // const filterRoomUser = roomUsers.filter((i) => i.UserId !== userId);
+      // if (delUser) {
+      //   this.roomService.socketGateway.server.sockets.sockets.forEach((soc) => {
+      //     if (soc.id === delUser.id) {
+      //       soc.data.isKick = true;
+      //       soc.disconnect(true);
+      //     }
+      //   });
+      //   const res = await this.usersService.deleteRoomUser(roomId, userId);
+      //   if (res) {
+      //     this.roomService.socketGateway.server.to(roomId).emit('action', {
+      //       type: 'user-exit',
+      //       user: delUser,
+      //       members: filterRoomUser,
+      //     });
+      //   }
+      // }
+    }
   }
 }

+ 1 - 1
src/room-manager/room-manager.module.ts

@@ -10,4 +10,4 @@ import { UsersService } from 'src/room/users/users.service';
   controllers: [RoomManagerController],
   providers: [RoomManagerService, UsersService],
 })
-export class RoomManagerModule { }
+export class RoomManagerModule {}

+ 0 - 2
src/room-manager/room-manager.service.ts

@@ -22,6 +22,4 @@ export class RoomManagerService {
     }
     return [];
   }
-
-
 }

+ 3 - 3
src/room/actions/actions.service.ts

@@ -12,7 +12,7 @@ export class ActionsService {
     private roomService: RoomService,
     @Inject(forwardRef(() => UsersService))
     private userService: UsersService,
-  ) {}
+  ) { }
 
   async handleAllAction(socket: Socket, data: ActionsParams): Promise<void> {
     const isSocketLeader = () => {
@@ -85,10 +85,10 @@ export class ActionsService {
   }
   async handleKickState(socket: Socket, data: KickStateType) {
     if (data) {
-      const roomId = socket.data.user.RoomId;
+      const roomId = socket.data.user.RoomId || '';
       const userId = data.userId;
 
-      await this.roomService.handleKickAction(socket, roomId, userId);
+      await this.roomService.handleKickAction(roomId, userId);
     }
   }
 }

+ 8 - 4
src/room/room.service.ts

@@ -72,7 +72,8 @@ export class RoomService {
       const { RoomId, UserId } = socket.data.user;
       if (RoomId && UserId) {
         //标记主动退出房间
-        if (socket.data.isRequestExit !== 1) {
+
+        if (socket.data.isRequestExit !== 1 && socket.data.isKick !== 1) {
           const roomUsers = await this.userService.getRoomUsers(RoomId);
           this.socketGateway.server.to(RoomId).emit('action', {
             type: 'user-leave',
@@ -209,7 +210,7 @@ export class RoomService {
    *  房间T人
    * @param message
    */
-  async handleKickAction(socket: Socket, RoomId: string, userId: string) {
+  async handleKickAction(RoomId: string, userId: string) {
     const delUser = await this.userService.getUsersBy(RoomId, userId);
     this.logger.warn(
       `RoomId: ${RoomId},userId:${userId} socketId:${delUser.id}`,
@@ -218,9 +219,13 @@ export class RoomService {
     const roomUsers = await this.userService.getRoomUsers(RoomId);
     const filterRoomUser = roomUsers.filter((i) => i.UserId !== userId);
     if (delUser) {
-      socket.data.isKick = true;
       this.socketGateway.server.sockets.sockets.forEach((soc) => {
         if (soc.id === delUser.id) {
+          soc.data.isKick = 1;
+          soc.emit('action', {
+            type: 'user-be-kicked',
+            user: delUser,
+          });
           soc.disconnect(true);
         }
       });
@@ -292,5 +297,4 @@ export class RoomService {
       .emit('action', { type: 'leader-dismiss' });
     return Promise.resolve(true);
   }
-
 }

+ 8 - 1
src/room/users/users.service.ts

@@ -10,7 +10,7 @@ export class UsersService {
     @InjectRedis() private readonly redis: Redis,
     @Inject(forwardRef(() => RoomService))
     private roomService: RoomService,
-  ) { }
+  ) {}
 
   async isUserInRooms(RoomId: string, UserId: string): Promise<boolean> {
     const res = await this.redis.hexists(
@@ -20,6 +20,13 @@ export class UsersService {
 
     return res > 0 ? true : false;
   }
+  async isValidRoom(RoomId: string): Promise<boolean> {
+    const isRoomHasConfig = await this.redis.hexists(
+      `kankan:socket:roomConfig`,
+      RoomId,
+    );
+    return Promise.resolve(Boolean(isRoomHasConfig));
+  }
   async insertUser(useInfo: UserInfoType): Promise<boolean> {
     const insert = await this.redis.hset(
       `kankan:socket:rooms:${useInfo.RoomId}`,