gemercheung 2 年之前
父节点
当前提交
6bc827797c
共有 3 个文件被更改,包括 21 次插入5 次删除
  1. 8 4
      src/room/actions/actions.service.ts
  2. 3 1
      src/room/room.service.ts
  3. 10 0
      src/room/users/users.service.ts

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

@@ -18,7 +18,14 @@ export class ActionsService {
     const isSocketLeader = () => {
       return socket.data.user?.Role === 'leader';
     };
-    const roomId = socket.data.user.RoomId;
+    let roomId = socket.data.user?.RoomId;
+    if (!roomId) {
+      roomId = this.roomService._userInfo.RoomId;
+      this.roomService.logger.error(
+        `当前-socket丟失参数-action::${roomId}`,
+        'handleAllAction',
+      );
+    }
     this.roomService.logger.warn(
       `当前--broadcast:${roomId}`,
       'handleAllAction',
@@ -54,9 +61,6 @@ export class ActionsService {
 
     socket.broadcast.to(roomId).emit('action', data);
   }
-  // answer(data: any) {
-  //   this.roomService.socketGateway._socket.broadcast.emit('action', data);
-  // }
 
   async handleDanmaku(data: DanmakuDataType) {
     console.log('data', data);

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

@@ -17,7 +17,7 @@ export class RoomService {
     private readonly actionsService: ActionsService,
     private readonly delayService: DelayService,
     private readonly tempService: TempService,
-  ) {}
+  ) { }
   public readonly logger = new Logger('user');
   public _sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
   public _userInfo = {} as UserInfoType;
@@ -61,6 +61,7 @@ export class RoomService {
     if (socket.data?.user) {
       this.delayService.handleOffline(socket);
       const { RoomId, UserId, Role } = socket.data.user;
+      await this.userService.setRoomEntranceLog(RoomId, UserId, 1);
       if (RoomId && UserId) {
         //标记主动退出房间
         if (socket.data.isRequestExit !== 1 && socket.data.isKick !== 1) {
@@ -108,6 +109,7 @@ export class RoomService {
     await this.delayService.handleOnine(socket);
     let blockJoin = false;
     const { RoomId, UserId, Role, roomConfig } = socket.data.user;
+    await this.userService.setRoomEntranceLog(RoomId, UserId, 0);
     const _isLeader = Role === 'leader';
     if (RoomId?.length && UserId?.length) {
       //房主设置房间配置

+ 10 - 0
src/room/users/users.service.ts

@@ -258,4 +258,14 @@ export class UsersService {
       return Promise.resolve(false);
     }
   }
+  async setRoomEntranceLog(
+    RoomId: string,
+    userId: string,
+    type: number,
+  ): Promise<void> {
+    const logKey = `kankan:socket:entranceLog:${RoomId}:${userId}`;
+    const timestamp = Date.now();
+    const setData = { type: type, RoomId: RoomId, userId: userId };
+    await this.redis.hset(logKey, timestamp, JSON.stringify(setData));
+  }
 }