浏览代码

redis优化

gemercheung 3 年之前
父节点
当前提交
95642b18b8
共有 6 个文件被更改,包括 235 次插入19 次删除
  1. 1 0
      .env
  2. 2 2
      package.json
  3. 2 8
      src/connection/redis.js
  4. 220 0
      src/controller/room/bocker.js
  5. 6 5
      src/controller/room/index.js
  6. 4 4
      src/service/roomConfigService.js

+ 1 - 0
.env

@@ -8,6 +8,7 @@ REDIS_HOST=redis
 REDIS_PORT=6379
 REDIS_USER=redis
 REDIS_PASSWORD=s1mpl3
+REDIS_DB=2
 WATCH_USER=4dage
 WATCH_PASSWORD=4dage168.
 USER_LIMIT=50

+ 2 - 2
package.json

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

+ 2 - 8
src/connection/redis.js

@@ -2,7 +2,7 @@ import { createClient } from "redis";
 import cluster from "cluster";
 import { logger } from "../core/logger.js";
 
-const url = `redis://:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`;
+const url = `redis://:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/${process.env.REDIS_DB}`;
 if (cluster.isPrimary) {
   logger.info("redis", { url });
 }
@@ -12,13 +12,7 @@ pubClient.on("ready", () => {
   // console.log("pubClient-ready");
   pubClient.configSet("notify-keyspace-events", "Ex");
 });
-// pubClient.configSet
-const subClient = pubClient.duplicate();
-// subClient.on('ready',()=>{
-//   subClient.subscribe("__keyevent@0__:expired", (key) => {
-//     console.log("key=> ", key);
-//   });
-// })
 
+const subClient = pubClient.duplicate();
 
 export { pubClient, subClient };

+ 220 - 0
src/controller/room/bocker.js

@@ -0,0 +1,220 @@
+// 房间拦截行为助手
+import { EVENT, CODEMEG } 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";
+export class RoomBlocker {
+  constructor(socket, redis, room) {
+    this.socket = socket;
+    this.redis = redis;
+    this.roomId = null;
+    this.room = room;
+  }
+
+  async prepearRoom(roomSessionId, roomId) {
+    const uRoomId = await this.redis.get(roomSessionId);
+    const mergeRoomId = uRoomId || roomId;
+    this.roomId = mergeRoomId;
+    console.log("prepearRoom", roomSessionId, this.roomId);
+    await this.redis.set(roomSessionId, mergeRoomId);
+    return Promise.resolve(this.roomId);
+  }
+
+  async destoryRoom(roomSessionId, roomConfigId) {
+    console.log("destoryRoom", roomSessionId, roomConfigId);
+    await this.redis.del(roomSessionId);
+    await this.redis.del(roomConfigId);
+    this.disconnect();
+    return Promise.resolve(true);
+  }
+
+  /**
+   *
+   */
+  kickPersion() {}
+
+  /**
+   * 创建房间  LEADER or assistant 房主或助手
+   * @param {*} roomId
+   * @param {*} userId
+   * @param {*} user
+   */
+
+  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));
+    }
+  }
+
+  /**
+   * 关闭房间
+   * @param {*} roomId
+   */
+
+  async removeRoom(roomId) {
+    this.room.logger.info("removeRoom", { roomId });
+    await this.redis.del(roomId);
+  }
+
+  /**
+   *  加入房间
+   * @param {*} roomId
+   * @param {*} userId
+   * @param {*} user
+   */
+  async joinRoom(roomId, userId, user) {
+    const hasRoom = await this.redis.exists(roomId);
+    const isJoinRoom = await this.redis.hExists(roomId, userId);
+    if (hasRoom) {
+      await this.redis.hSet(roomId, userId, JSON.stringify(user));
+    } else {
+      this.room.logger.error("不存在房间", roomId);
+    }
+    if (!isJoinRoom) {
+      this.room.logger.info("加入房间 :", { userId, roomId, user });
+      this.socket.join(roomId);
+      const AllRoomUsers = await getAllRoomUsers(roomId);
+      const roomConfig = await getRoomConfig(roomId);
+      this.socket.broadcast.emit(EVENT.roomIn, {
+        user,
+        roomsPerson: AllRoomUsers,
+        roomsConfig: roomConfig,
+      });
+    } else {
+      this.room.logger.info(`已加入房间 :`, { userId });
+    }
+  }
+
+  /**
+   * 离开房间
+   * @param {*} roomId
+   * @param {*} userId
+   * @param {*} user
+   */
+  async leaveRoom(roomId, userId, user) {
+    try {
+      await this.redis.hDel(roomId, userId);
+      this.room.logger.info("离开房间", userId, AllRoomUsers);
+      const AllRoomUsers = await getAllRoomUsers(roomId);
+      const roomConfig = await getRoomConfig(roomId);
+      this.socket.broadcast.to(roomId).emit(EVENT.roomOut, {
+        user,
+        roomsPerson: AllRoomUsers,
+        roomsConfig: roomConfig,
+      });
+
+      this.socket.broadcast.to(roomId).emit(EVENT.someOneLeaveRoom, {
+        user,
+        roomsPerson: AllRoomUsers,
+      });
+      await this.socket.leave(roomId);
+    } catch (error) {
+      console.log("leaveRoom::error", error);
+    }
+  }
+
+  /**
+   * 房主关闭房间
+   * @param {*} clientRoom
+   * @param {*} userUniqueId
+   * @param {*} roomUniqueId
+   */
+  async closeRoom(roomId, userId, user) {
+    try {
+      this.room.logger.info("房主关闭房间", userId);
+      console.log("isInRoom", this.socket.rooms.has(roomId));
+      this.socket.broadcast.to(roomId).emit(EVENT.roomClose, { code: 3002, msg: CODEMEG[3002] });
+      await removeRoomAllUsers(roomId);
+      this.socket.leave(roomId);
+    } catch (error) {
+      this.room.logger.error("RoomAssistant::closeRoom", error);
+    }
+  }
+
+  /**
+   * 呼叫房间
+   * @param {*} roomId
+   * @param {*} userId
+   * @param {*} user
+   */
+  async startCall(roomId, userId, user) {
+    try {
+      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(roomId);
+        if (hasRoom.length === 0) {
+          this.room.logger.info("房主主动创建房间 :", { roomId, userId });
+          await this.buildRoom(roomId, userId, user);
+        } else {
+          this.room.logger.info("房主已存在房间 :", { roomId, userId });
+        }
+      }
+      user.isInRoom = 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.broadcast.to(roomId).emit(EVENT.someOneInRoom, {
+        user,
+        roomsPerson: AllRoomUsers,
+      });
+    } catch (error) {
+      this.room.logger.error("assistant::startCall:", error);
+    }
+  }
+
+  /**
+   * 关闭呼叫房间
+   * @param {*} roomId
+   * @param {*} userId
+   * @param {*} user
+   */
+
+  stopCall(roomId, userId, user) {
+    if (!this.room.isHoster(user.role)) {
+      this.leaveRoom(roomId, userId, user);
+    } else {
+      this.closeRoom(roomId, userId, user);
+    }
+  }
+  // 主动断开
+  async disconnect() {
+    const syncId = this.room.syncId;
+    const roomId = this.room.roomId;
+    const userId = this.room.userId;
+    this.socket.leave(syncId);
+    this.socket.leave(roomId);
+    await this.redis.del(syncId);
+    await this.redis.del(userId);
+  }
+  // RoomSessionId 房间有效时间
+  setRoomUnlimit(roomSessionId) {
+    return this.redis.expire(roomSessionId, -1);
+  }
+  setRoomAvailableBySeconds(roomSessionId, seconds) {
+    return this.redis.expire(roomSessionId, seconds);
+  }
+  setRoomAvailableByHours(roomSessionId, hours) {
+    return this.redis.expire(roomSessionId, 60 * 60 * hours);
+  }
+  watchRoomExpired(callback) {
+    subClient.subscribe("__keyevent@0__:expired", this.watchRoomExpiredFn);
+  }
+  async watchRoomExpiredFn(key) {
+    console.log("key=> ", key);
+  }
+  unWatchRoomExpired() {
+    subClient.unsubscribe("__keyevent@0__:expired", this.watchRoomExpiredFn);
+  }
+}

+ 6 - 5
src/controller/room/index.js

@@ -75,13 +75,14 @@ export class RoomController extends BasicController {
   }
 
   async initParams(userId, roomId, oneSceneNum) {
-    this.userId = `user-${userId}`;
-    this.syncId = `sync-${oneSceneNum}-${userId}`;
-    this.sessionId = `${oneSceneNum}-${userId}`;
+    this.userId = `user:${userId}`;
+    this.syncId = `sync:${oneSceneNum}:${userId}`;
+    this.sessionId = `session:${oneSceneNum}:${userId}`;
     const uRoomId = await this.roomAssistant.prepearRoom(this.sessionId, roomId);
-    this.roomId = `room-${uRoomId}_${oneSceneNum}_web`;
+    // this.roomId = `room:${uRoomId}_${oneSceneNum}`;
+    this.roomId = `room:${oneSceneNum}:${uRoomId}`;
     this.user.roomId = uRoomId;
-    this.roomConfigId = `config-${this.roomId}`;
+    this.roomConfigId = `config:${this.roomId}`;
     return Promise.resolve(true);
   }
 

+ 4 - 4
src/service/roomConfigService.js

@@ -1,19 +1,19 @@
 import { pubClient } from "../connection/redis.js";
 
 const setRoomConfig = (roomId, config) => {
-  const roomConfigKey = `config-${roomId}`;
+  const roomConfigKey = `config:${roomId}`;
   return pubClient.hSet(roomConfigKey, config);
 };
 const removeRoomConfig = (roomId, config) => {
-  const roomConfigKey = `config-${roomId}`;
+  const roomConfigKey = `config:${roomId}`;
   return pubClient.del(roomConfigKey);
 };
 const getRoomConfig = (roomId, config) => {
-  const roomConfigKey = `config-${roomId}`;
+  const roomConfigKey = `config:${roomId}`;
   return pubClient.hGetAll(roomConfigKey);
 };
 const updateRoomConfigByKey = async (roomId, keyName, value) => {
-  const roomConfigKey = `config-${roomId}`;
+  const roomConfigKey = `config:${roomId}`;
   const res = await pubClient.hGetAll(roomConfigKey);
   const newKey = { [keyName]: value };
   const updateObj = Object.assign({}, res, newKey);