gemercheung 3 năm trước cách đây
mục cha
commit
6cb015a245
4 tập tin đã thay đổi với 31 bổ sung10 xóa
  1. 2 2
      package.json
  2. 3 4
      src/controller/room/assistant.js
  3. 15 3
      src/controller/room/index.js
  4. 11 1
      src/service/userService.js

+ 2 - 2
package.json

@@ -4,8 +4,8 @@
   "description": "",
   "main": "index.js",
   "scripts": {
-    "start": "cross-env NODE_ENV=development silent=true node index.js --debug 0.0.0.0:9229",
-    "start:debug": "cross-env NODE_ENV=development npx nodemon index.js --debug 0.0.0.0:9229",
+    "start": "cross-env NODE_ENV=development silent=true node --inspect index.js",
+    "start:debug": "cross-env NODE_ENV=development npx nodemon --inspect index.js",
     "production": "cross-env NODE_ENV=production node index.js"
   },
   "type": "module",

+ 3 - 4
src/controller/room/assistant.js

@@ -1,5 +1,5 @@
 // 房间行为助手
-import { EVENT, CODEMEG } from "../../enum/index.js";
+import { EVENT, CODEMEG, FROMTYPE } from "../../enum/index.js";
 import { updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser } from "../../service/userService.js";
 import { setRoomConfig, getRoomConfig } from "../../service/roomConfigService.js";
 import { subClient } from "../../connection/redis.js";
@@ -15,13 +15,13 @@ export class RoomAssistant {
     const uRoomId = await this.redis.get(roomSessionId);
     const mergeRoomId = uRoomId || roomId;
     this.roomId = mergeRoomId;
-    console.log("prepearRoom", roomSessionId, this.roomId);
+    this.room.logger.info("prepearRoom", roomSessionId, this.roomId);
     await this.redis.set(roomSessionId, mergeRoomId);
     return Promise.resolve(this.roomId);
   }
 
   async destoryRoom(roomSessionId, roomConfigId) {
-    console.log("destoryRoom", roomSessionId, roomConfigId);
+    this.room.logger.info("destoryRoom", roomSessionId, roomConfigId);
     await this.redis.del(roomSessionId);
     await this.redis.del(roomConfigId);
     this.disconnect();
@@ -48,7 +48,6 @@ export class RoomAssistant {
   async buildRoom(roomId, userId, user) {
     const hasJoin = await this.redis.HVALS(roomId, userId);
     if (hasJoin.length === 0) {
-      this.room.logger.info("buildRoom", { roomId });
       await this.redis.hSet(roomId, userId, JSON.stringify(user));
     }
   }

+ 15 - 3
src/controller/room/index.js

@@ -1,5 +1,5 @@
 import { ROLES, CODEMEG, EVENT, 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 { watchRoomService } from "../../service/watchRoomService.js";
 import { setRoomConfig, getRoomConfig, updateRoomConfigByKey } from "../../service/roomConfigService.js";
 
@@ -15,20 +15,28 @@ export class RoomController extends BasicController {
     this.userId = null;
     this.roomConfigId = null;
     this.debugger = true;
+    this.sysUsers = [];
     this.user = {
       sig: null,
       roomId: null,
       userId: null,
       sceneNum: null,
       isClient: null,
+      avatar: "",
       role: null,
       userLimitNum: null,
       sceneNumber: null,
       roomType: null,
       from: null,
+      onlineStatus: false,
+      voiceStatus: 0,
       enableTalk: null,
     };
   }
+  // 以小程序user信息作为主要信息
+  get currentUser() {
+    return this.sysUsers.find((item) => Number(item.from) === 2);
+  }
 
   async run() {
     this.logger.info("socket conetcted has start!");
@@ -44,6 +52,7 @@ export class RoomController extends BasicController {
 
   async init() {
     let user = this.socket.handshake.query;
+
     if (user) {
       this.user = Object.assign({}, user, {
         roomType: user.roomType || "",
@@ -53,8 +62,9 @@ export class RoomController extends BasicController {
       const { userId, roomId } = this.user;
       await this.initParams(userId, roomId, oneSceneNum);
       const userObj = { ...this.user, isConnected: true };
-      // console.log('userObj',userObj)
+
       updateUser(this.userId, userObj);
+      this.sysUsers.push(this.user);
 
       // 只有来源于小程序用户信息才记录到redis
       if (this.isHoster(this.user.role)) {
@@ -113,7 +123,9 @@ export class RoomController extends BasicController {
     });
 
     this.socket.on(EVENT.startCall, async () => {
-      this.roomAssistant.startCall(this.roomId, this.userId, this.user);
+      const currentUser = await getCurrentUser(this.userId, FROMTYPE.MiniAPP);
+      const user = JSON.parse(currentUser) || this.user;
+      this.roomAssistant.startCall(this.roomId, this.userId, user);
       if (this.isHoster(this.user.role)) {
         // 以startCall做为真正的进入房间
         await updateRoomConfigByKey(this.roomId, "isStart", true);

+ 11 - 1
src/service/userService.js

@@ -1,5 +1,15 @@
 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);
+  } catch (error) {
+    return Promise.resolve(false);
+  }
+};
+
 const updateUser = async (userId, userObj) => {
   await pubClient.hSet(userId, userObj.from, JSON.stringify(userObj));
   await pubClient.expire(userId, 60 * 60 * 24);
@@ -30,4 +40,4 @@ const getAllRoomUsers = async (roomId) => {
   });
 };
 
-export { updateUser, updateRoomUser, removeRoomAllUsers, removeRoomUser, getAllRoomUsers };
+export { getCurrentUser, updateUser, updateRoomUser, removeRoomAllUsers, removeRoomUser, getAllRoomUsers };