gemercheung 3 роки тому
батько
коміт
105d244bf8

+ 47 - 5
src/controller/room/assistant.js

@@ -1,6 +1,6 @@
 // 房间行为助手
 import { EVENT, CODEMEG, FROMTYPE } from "../../enum/index.js";
-import { updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser } from "../../service/userService.js";
+import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser } from "../../service/userService.js";
 import { setRoomConfig, getRoomConfig } from "../../service/roomConfigService.js";
 import { subClient } from "../../connection/redis.js";
 export class RoomAssistant {
@@ -32,9 +32,17 @@ export class RoomAssistant {
    * kickPersion LEADER or assistant 房主或助手
    */
   async kickPersion(roomId, userId) {
-    const hasJoin = await this.redis.HVALS(roomId, userId);
-    if (hasJoin.length > 0) {
-      await this.redis.hDel(roomId, userId);
+    console.log("kickPersion", roomId, userId);
+    try {
+      const hasJoin = await this.redis.HVALS(roomId, userId);
+      // const blackListId = ""
+      if (hasJoin.length > 0) {
+        await this.redis.hDel(roomId, userId);
+        // await this
+      }
+      return Promise.resolve(true);
+    } catch (error) {
+      return Promise.resolve(false);
     }
   }
 
@@ -175,7 +183,8 @@ export class RoomAssistant {
           this.room.logger.info("房主主动创建房间 :", { roomId, userId });
           await this.buildRoom(roomId, userId, user);
         } else {
-          this.room.logger.info("房主已存在房间 :", { roomId, userId });
+          this.room.logger.info("房主已存在房间 :", { roomId, userId, from: user.from });
+          this.notifyUserJitter(roomId);
         }
       }
       user.isInRoom = true;
@@ -199,6 +208,39 @@ export class RoomAssistant {
     }
   }
 
+  async notifyUserJitter(roomId, userId) {
+    const AllRoomUsers = await getAllRoomUsers(roomId);
+    const roomConfig = await getRoomConfig(roomId);
+    const currentUser = await getCurrentUser(userId, FROMTYPE.MiniAPP);
+    const user = JSON.parse(currentUser);
+    await updateRoomUser(roomId, userId, user);
+    this.room.logger.info("notifyUserJitter", roomId, AllRoomUsers.length);
+    this.socket.emit(EVENT.roomIn, {
+      user,
+      roomsPerson: AllRoomUsers,
+      roomsConfig: roomConfig,
+    });
+    this.socket.broadcast.to(roomId).emit(EVENT.someOneInRoom, {
+      user,
+      roomsPerson: AllRoomUsers,
+    });
+  }
+  /**
+   * 通知房间人员变动
+   */
+  async notifyUsersChange(roomId, user) {
+    const AllRoomUsers = await getAllRoomUsers(roomId);
+    const roomConfig = await getRoomConfig(roomId);
+    this.room.logger.info("notifyUsersChange", roomId, AllRoomUsers.length);
+    this.socket.broadcast.to(roomId).emit(EVENT.roomPersonChange, {
+      user: user,
+      actionName: "outRoom",
+      roomsPerson: AllRoomUsers,
+    });
+    // this.socket.broadcast.to(roomId).emit(EVENT.roomStatus, {
+    //   roomsPerson: AllRoomUsers,
+    // });
+  }
   /**
    * 关闭呼叫房间
    * @param {*} roomId

+ 24 - 11
src/controller/room/index.js

@@ -15,7 +15,6 @@ export class RoomController extends BasicController {
     this.userId = null;
     this.roomConfigId = null;
     this.debugger = true;
-    this.sysUsers = [];
     this.user = {
       sig: null,
       roomId: null,
@@ -34,8 +33,10 @@ export class RoomController extends BasicController {
     };
   }
   // 以小程序user信息作为主要信息
-  get currentUser() {
-    return this.sysUsers.find((item) => Number(item.from) === 2);
+  async currentUser() {
+    const data = await getCurrentUser(this.userId, FROMTYPE.MiniAPP);
+    const user = data ? JSON.parse(data) : this.user;
+    return user;
   }
 
   async run() {
@@ -52,7 +53,7 @@ export class RoomController extends BasicController {
 
   async init() {
     let user = this.socket.handshake.query;
-
+    this.logger.info("init-user-query:", this.socket.handshake.query);
     if (user) {
       this.user = Object.assign({}, user, {
         roomType: user.roomType || "",
@@ -64,7 +65,7 @@ export class RoomController extends BasicController {
       const userObj = { ...this.user, isConnected: true };
 
       updateUser(this.userId, userObj);
-      this.sysUsers.push(this.user);
+      // this.sysUsers.push(this.user);
 
       // 只有来源于小程序用户信息才记录到redis
       if (this.isHoster(this.user.role)) {
@@ -123,9 +124,9 @@ export class RoomController extends BasicController {
     });
 
     this.socket.on(EVENT.startCall, async () => {
-      const currentUser = await getCurrentUser(this.userId, FROMTYPE.MiniAPP);
-      const user = JSON.parse(currentUser) || this.user;
+      const user = await this.currentUser();
       this.roomAssistant.startCall(this.roomId, this.userId, user);
+      // console.log("startCall-from", this.user.from);
       if (this.isHoster(this.user.role)) {
         // 以startCall做为真正的进入房间
         await updateRoomConfigByKey(this.roomId, "isStart", true);
@@ -152,21 +153,33 @@ export class RoomController extends BasicController {
 
     this.socket.on(EVENT.changeOnlineStatus, async (data) => {
       try {
-        this.user.onlineStatus = data.status;
+        const user = await this.currentUser();
+        user.onlineStatus = data.status;
         const AllRoomUsers = await getAllRoomUsers(this.roomId);
-        await updateRoomUser(this.roomId, this.userId, this.user);
+        await updateRoomUser(this.roomId, this.userId, user);
         let actionName = this.user.onlineStatus ? "inRoom" : "leaveRoom";
-        this.logger.info("changeOnlineStatus", JSON.stringify(this.user));
+        this.logger.info("changeOnlineStatus", JSON.stringify(user));
         this.socket.broadcast.to(this.roomId).emit(EVENT.roomPersonChange, {
           roomsPerson: AllRoomUsers,
           actionName,
-          user: this.user,
+          user: user,
         });
       } catch (error) {
         this.logger.error("event:changeOnlineStatus", error);
       }
     });
 
+    this.socket.on(EVENT.kickUser, async ({ data, from }) => {
+      const userId = `user:${data.userId}`;
+      const roomId = `room:${data.sceneNumber}:${data.roomId}`;
+      this.logger.info("kickUser", data.userId, data.roomId);
+      this.roomAssistant.kickPersion(roomId, userId);
+      const currentUser = await getCurrentUser(userId, FROMTYPE.MiniAPP);
+      this.roomAssistant.notifyUsersChange(roomId, currentUser);
+      //TODO 如果踢人后 如何通知?
+      this.socket.broadcast.to(roomId).emit(EVENT.beKicked, data);
+    });
+
     if (this.debugger) {
       this.socket.onAny((event, data) => {
         if (event !== "webSyncAction") {

+ 5 - 0
src/enum/event.js

@@ -14,6 +14,11 @@ const EVENT = {
   changeOnlineStatus: "changeOnlineStatus", // 通知某设备的在线状态
   roomPersonChange: "roomPersonChange", // 房间人数变化事件
   changeRoomEnableTalk: "changeRoomEnableTalk", // 改变房间是否可通话的配置
+  setAssistant: "setAssistant", // 设置助手
+  setUserhasMic: "setUserhasMic", //
+  kickUser: "kickUser", // 踢人
+  roomStatus: "roomStatus", // 房间状态
+  beKicked:"beKicked" // 通知被踢者
 };
 
 export { EVENT };

+ 9 - 4
src/service/userService.js

@@ -2,9 +2,13 @@ import { pubClient } from "../connection/redis.js";
 
 const getCurrentUser = async (userId, key) => {
   try {
-    console.log("getCurrentUser", userId, key);
-    const user = await pubClient.hGet(userId, String(key));
-    return Promise.resolve(user);
+    if (userId) {
+      console.log("getCurrentUser", userId, key);
+      const user = await pubClient.hGet(userId, String(key));
+      return Promise.resolve(user);
+    } else {
+      return Promise.resolve(false);
+    }
   } catch (error) {
     return Promise.resolve(false);
   }
@@ -12,8 +16,9 @@ const getCurrentUser = async (userId, key) => {
 
 const updateUser = async (userId, userObj) => {
   await pubClient.hSet(userId, userObj.from, JSON.stringify(userObj));
-  await pubClient.expire(userId, 60 * 60 * 24);
+  // await pubClient.expire(userId, 60 * 60 * 24);
 };
+
 const updateRoomUser = async (roomId, userId, userObj) => {
   try {
     await pubClient.hSet(userId, userObj.from, JSON.stringify(userObj));