소스 검색

超出上限

gemercheung 3 년 전
부모
커밋
72320293f7
2개의 변경된 파일79개의 추가작업 그리고 53개의 파일을 삭제
  1. 77 53
      src/controller/room/assistant.js
  2. 2 0
      src/controller/room/index.js

+ 77 - 53
src/controller/room/assistant.js

@@ -278,66 +278,72 @@ export class RoomAssistant {
    */
   async startCall(roomId, userId, user) {
     try {
-      if (user.oid) {
-        console.log("hasDuplicateUser-存在oid", user.oid);
-        const hasDuplicateUser = await this.getOpenidInRoom(roomId, user.oid);
-        if (hasDuplicateUser && hasDuplicateUser.length > 0) {
-          const removeAll = [];
-          Array.from(hasDuplicateUser).forEach((duplicateUser) => {
-            if (duplicateUser.userId !== user.userId) {
-              console.log("duplicateUser-去重用户", duplicateUser);
-              const deleteUserKey = `user:${duplicateUser.userId}`;
-              console.log("deleteUserKey", deleteUserKey);
-              removeAll.push(removeRoomUser(roomId, deleteUserKey));
-            }
-          });
-          const res = await Promise.all(removeAll);
-          console.log("去重完成", res);
+      const res = this.checkRoomMaximum(roomId);
+      if (!res.isMax) {
+        if (user.oid) {
+          console.log("hasDuplicateUser-存在oid", user.oid);
+          const hasDuplicateUser = await this.getOpenidInRoom(roomId, user.oid);
+          if (hasDuplicateUser && hasDuplicateUser.length > 0) {
+            const removeAll = [];
+            Array.from(hasDuplicateUser).forEach((duplicateUser) => {
+              if (duplicateUser.userId !== user.userId) {
+                console.log("duplicateUser-去重用户", duplicateUser);
+                const deleteUserKey = `user:${duplicateUser.userId}`;
+                console.log("deleteUserKey", deleteUserKey);
+                removeAll.push(removeRoomUser(roomId, deleteUserKey));
+              }
+            });
+            const res = await Promise.all(removeAll);
+            console.log("去重完成", res);
+          }
         }
-      }
 
-      if (!this.room.isHoster(user.role)) {
-        this.room.logger.info("不是房主", JSON.stringify(user));
-        await this.joinRoom(roomId, userId, user);
-      } else {
-        const hasRoom = await this.redis.hVals(getInKey(roomId));
-        if (hasRoom.length === 0) {
-          this.room.logger.info("房主主动创建房间 :", { roomId, userId });
-          await this.buildRoom(roomId, userId, user);
+        if (!this.room.isHoster(user.role)) {
+          this.room.logger.info("不是房主", JSON.stringify(user));
+          await this.joinRoom(roomId, userId, user);
         } else {
-          //TODO
-          const checkIsRoomMaster = await isRoomMaster(roomId, userId);
-          console.log("isRoomMaster", checkIsRoomMaster);
-          if (checkIsRoomMaster) {
-            this.room.logger.info("房主已存在房间 :", { roomId, userId, from: user.from });
-            await this.joinRoom(roomId, userId, user);
-            // this.notifyUserJitter(roomId);
+          const hasRoom = await this.redis.hVals(getInKey(roomId));
+          if (hasRoom.length === 0) {
+            this.room.logger.info("房主主动创建房间 :", { roomId, userId });
+            await this.buildRoom(roomId, userId, user);
           } else {
-            this.room.logger.error("存在非法房主", userId);
+            //TODO
+            const checkIsRoomMaster = await isRoomMaster(roomId, userId);
+            console.log("isRoomMaster", checkIsRoomMaster);
+            if (checkIsRoomMaster) {
+              this.room.logger.info("房主已存在房间 :", { roomId, userId, from: user.from });
+              await this.joinRoom(roomId, userId, user);
+              // this.notifyUserJitter(roomId);
+            } else {
+              this.room.logger.error("存在非法房主", userId);
+            }
           }
         }
-      }
-      user.isInRoom = true;
-      this.hasCall = true;
-      const AllRoomUsers = await getAllRoomUsers(roomId);
-      const roomConfig = await getRoomConfig(roomId);
-      await updateRoomUser(roomId, userId, user);
-      this.room.logger.info("roomId", roomId);
-      this.room.logger.info("AllRoomUsers", AllRoomUsers.length);
+        user.isInRoom = true;
+        this.hasCall = true;
+        const AllRoomUsers = await getAllRoomUsers(roomId);
+        const roomConfig = await getRoomConfig(roomId);
+        await updateRoomUser(roomId, userId, user);
+        this.room.logger.info("roomId", roomId);
+        this.room.logger.info("AllRoomUsers", AllRoomUsers.length);
 
-      this.socket.emit(EVENT.roomIn, {
-        user,
-        roomsPerson: AllRoomUsers,
-        roomsConfig: roomConfig,
-      });
-      this.socket.emit(EVENT.someOneInRoom, {
-        user,
-        roomsPerson: AllRoomUsers,
-      });
-      this.socket.broadcast.to(roomId).emit(EVENT.someOneInRoom, {
-        user,
-        roomsPerson: AllRoomUsers,
-      });
+        this.socket.emit(EVENT.roomIn, {
+          user,
+          roomsPerson: AllRoomUsers,
+          roomsConfig: roomConfig,
+        });
+        this.socket.emit(EVENT.someOneInRoom, {
+          user,
+          roomsPerson: AllRoomUsers,
+        });
+        this.socket.broadcast.to(roomId).emit(EVENT.someOneInRoom, {
+          user,
+          roomsPerson: AllRoomUsers,
+        });
+      } else {
+        this.room.logger.warn("超出房间上限", res.num);
+        
+      }
       // await this.notifyUsersChange(roomId, user, true);
     } catch (error) {
       this.room.logger.error("assistant::startCall:", error);
@@ -418,6 +424,24 @@ export class RoomAssistant {
       users: AllRoomUsers,
     });
   }
+
+  async checkRoomMaximum(roomId) {
+    const roomConfigRes = await getRoomConfig(roomId);
+    const userLimitNum = Number(roomConfigRes.userLimitNum) || 50;
+    const users = await getAllRoomUsers(roomId);
+    if (users && users.length > Number(userLimitNum)) {
+      return Promise.resolve({
+        isMax: true,
+        num: userLimitNum,
+      });
+    } else {
+      return Promise.resolve({
+        isMax: false,
+        num: userLimitNum,
+      });
+    }
+  }
+
   // 主动断开
   async disconnect() {
     try {

+ 2 - 0
src/controller/room/index.js

@@ -108,6 +108,7 @@ export class RoomController extends BasicController {
       this.user = userObj;
       // this.sysUsers.push(this.user);
       // 只有来源于小程序用户信息才记录到redis
+
       if (this.isHoster(this.user.role)) {
         if ([FROMTYPE.MiniAPP].includes(Number(this.user.from))) {
           const roomConfig = getRoomConfig(this.roomId);
@@ -133,6 +134,7 @@ export class RoomController extends BasicController {
       // 不带key的this.user.userId
       const isInRoom = await isUserInRoom(this.roomId, this.user.userId);
       if (isInRoom) {
+        console.log("isInRoom-hasCall", this.roomAssistant.hasCall);
         if (Number(isInRoom.onlineStatus) === 0 && this.roomAssistant.hasCall) {
           console.log("hasCall在房间人员掉线人员,强制上线!", isInRoom);
           // 带key的this.userId