Pārlūkot izejas kodu

大改,增加redis prefix

gemercheung 3 gadi atpakaļ
vecāks
revīzija
f13c100445

+ 1 - 0
.env

@@ -9,6 +9,7 @@ REDIS_PORT=6379
 REDIS_USER=redis
 REDIS_USER=redis
 REDIS_PASSWORD=s1mpl3
 REDIS_PASSWORD=s1mpl3
 REDIS_DB=2
 REDIS_DB=2
+REDIS_PREFIX=chat
 WATCH_USER=4dage
 WATCH_USER=4dage
 WATCH_PASSWORD=4dage168.
 WATCH_PASSWORD=4dage168.
 LOGGER_PATH=
 LOGGER_PATH=

+ 18 - 12
src/controller/room/assistant.js

@@ -3,6 +3,11 @@ import { EVENT, CODEMEG, FROMTYPE } from "../../enum/index.js";
 import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser } from "../../service/userService.js";
 import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser } from "../../service/userService.js";
 import { setRoomConfig, getRoomConfig, isRoomMaster } from "../../service/roomConfigService.js";
 import { setRoomConfig, getRoomConfig, isRoomMaster } from "../../service/roomConfigService.js";
 import { subClient } from "../../connection/redis.js";
 import { subClient } from "../../connection/redis.js";
+
+const prefix = process.env.REDIS_PREFIX || "chat";
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
 export class RoomAssistant {
 export class RoomAssistant {
   constructor(socket, redis, room) {
   constructor(socket, redis, room) {
     this.socket = socket;
     this.socket = socket;
@@ -12,18 +17,18 @@ export class RoomAssistant {
   }
   }
 
 
   async prepearRoom(roomSessionId, roomId) {
   async prepearRoom(roomSessionId, roomId) {
-    const uRoomId = await this.redis.get(roomSessionId);
+    const uRoomId = await this.redis.get(getInKey(roomSessionId));
     const mergeRoomId = uRoomId || roomId;
     const mergeRoomId = uRoomId || roomId;
     this.roomId = mergeRoomId;
     this.roomId = mergeRoomId;
     this.room.logger.info("prepearRoom", roomSessionId, this.roomId);
     this.room.logger.info("prepearRoom", roomSessionId, this.roomId);
-    await this.redis.set(roomSessionId, mergeRoomId);
+    await this.redis.set(getInKey(roomSessionId), mergeRoomId);
     return Promise.resolve(this.roomId);
     return Promise.resolve(this.roomId);
   }
   }
 
 
   async destoryRoom(roomSessionId, roomConfigId) {
   async destoryRoom(roomSessionId, roomConfigId) {
     this.room.logger.info("destoryRoom", roomSessionId, roomConfigId);
     this.room.logger.info("destoryRoom", roomSessionId, roomConfigId);
-    await this.redis.del(roomSessionId);
-    await this.redis.del(roomConfigId);
+    await this.redis.del(getInKey(roomSessionId));
+    await this.redis.del(getInKey(roomConfigId));
     this.disconnect();
     this.disconnect();
     return Promise.resolve(true);
     return Promise.resolve(true);
   }
   }
@@ -33,11 +38,12 @@ export class RoomAssistant {
    */
    */
   async kickPersion(roomId, userId) {
   async kickPersion(roomId, userId) {
     console.log("kickPersion", roomId, userId);
     console.log("kickPersion", roomId, userId);
+    getInKey(roomId)
     try {
     try {
-      const hasJoin = await this.redis.HVALS(roomId, userId);
+      const hasJoin = await this.redis.HVALS( getInKey(roomId), userId);
       // const blackListId = ""
       // const blackListId = ""
       if (hasJoin.length > 0) {
       if (hasJoin.length > 0) {
-        await this.redis.hDel(roomId, userId);
+        await this.redis.hDel(getInKey(roomId), userId);
         return Promise.resolve(true);
         return Promise.resolve(true);
       } else {
       } else {
         return Promise.resolve(false);
         return Promise.resolve(false);
@@ -141,9 +147,9 @@ export class RoomAssistant {
    */
    */
 
 
   async buildRoom(roomId, userId, user) {
   async buildRoom(roomId, userId, user) {
-    const hasJoin = await this.redis.HVALS(roomId, userId);
+    const hasJoin = await this.redis.HVALS(getInKey(roomId), userId);
     if (hasJoin.length === 0) {
     if (hasJoin.length === 0) {
-      await this.redis.hSet(roomId, userId, JSON.stringify(user));
+      await this.redis.hSet(getInKey(roomId), userId, JSON.stringify(user));
     }
     }
   }
   }
 
 
@@ -164,10 +170,10 @@ export class RoomAssistant {
    * @param {*} user
    * @param {*} user
    */
    */
   async joinRoom(roomId, userId, user) {
   async joinRoom(roomId, userId, user) {
-    const hasRoom = await this.redis.exists(roomId);
-    const isJoinRoom = await this.redis.hExists(roomId, userId);
+    const hasRoom = await this.redis.exists(getInKey(roomId));
+    const isJoinRoom = await this.redis.hExists(getInKey(roomId), userId);
     if (hasRoom) {
     if (hasRoom) {
-      await this.redis.hSet(roomId, userId, JSON.stringify(user));
+      await this.redis.hSet(getInKey(roomId), userId, JSON.stringify(user));
     } else {
     } else {
       this.room.logger.error("不存在房间", roomId);
       this.room.logger.error("不存在房间", roomId);
     }
     }
@@ -194,7 +200,7 @@ export class RoomAssistant {
    */
    */
   async leaveRoom(roomId, userId, user) {
   async leaveRoom(roomId, userId, user) {
     try {
     try {
-      await this.redis.hDel(roomId, userId);
+      await this.redis.hDel(getInKey(roomId), userId);
       this.room.logger.info("离开房间", userId, AllRoomUsers);
       this.room.logger.info("离开房间", userId, AllRoomUsers);
       const AllRoomUsers = await getAllRoomUsers(roomId);
       const AllRoomUsers = await getAllRoomUsers(roomId);
       const roomConfig = await getRoomConfig(roomId);
       const roomConfig = await getRoomConfig(roomId);

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

@@ -90,7 +90,7 @@ export class RoomController extends BasicController {
           break;
           break;
       }
       }
 
 
-      const userObj = { ...this.user, isConnected: true, order };
+      const userObj = { ...this.user, onlineStatus: 1, isConnected: true, order };
 
 
       updateUser(this.userId, userObj);
       updateUser(this.userId, userObj);
       // this.sysUsers.push(this.user);
       // this.sysUsers.push(this.user);
@@ -196,10 +196,10 @@ export class RoomController extends BasicController {
       try {
       try {
         const user = await this.currentUser();
         const user = await this.currentUser();
         user.onlineStatus = data.status;
         user.onlineStatus = data.status;
-        const AllRoomUsers = await getAllRoomUsers(this.roomId);
         await updateRoomUser(this.roomId, this.userId, user);
         await updateRoomUser(this.roomId, this.userId, user);
         let actionName = this.user.onlineStatus ? "inRoom" : "leaveRoom";
         let actionName = this.user.onlineStatus ? "inRoom" : "leaveRoom";
         this.logger.info("changeOnlineStatus", JSON.stringify(user));
         this.logger.info("changeOnlineStatus", JSON.stringify(user));
+        const AllRoomUsers = await getAllRoomUsers(this.roomId);
         this.socket.broadcast.to(this.roomId).emit(EVENT.roomPersonChange, {
         this.socket.broadcast.to(this.roomId).emit(EVENT.roomPersonChange, {
           roomsPerson: AllRoomUsers,
           roomsPerson: AllRoomUsers,
           actionName,
           actionName,

+ 11 - 6
src/controller/syncDeviceController.js

@@ -3,6 +3,11 @@ import { BasicController } from "./basicController.js";
 import { getAllRoomUsers, updateRoomUser } from "../service/userService.js";
 import { getAllRoomUsers, updateRoomUser } from "../service/userService.js";
 import { FROMTYPE, EVENT } from "../enum/index.js";
 import { FROMTYPE, EVENT } from "../enum/index.js";
 
 
+const prefix = process.env.REDIS_PREFIX || "chat";
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
+
 export class SyncDeviceController extends BasicController {
 export class SyncDeviceController extends BasicController {
   constructor(...args) {
   constructor(...args) {
     super(...args);
     super(...args);
@@ -22,7 +27,7 @@ export class SyncDeviceController extends BasicController {
   async joinSyncClient(syncId, userId, from) {
   async joinSyncClient(syncId, userId, from) {
     this.logger.info("syncId", { syncId, userId, from });
     this.logger.info("syncId", { syncId, userId, from });
     try {
     try {
-      await this.redisCli.hSet(syncId, from, userId);
+      await this.redisCli.hSet(getInKey(syncId), from, userId);
       this.socket.join(syncId);
       this.socket.join(syncId);
     } catch (error) {
     } catch (error) {
       this.logger.error("joinSyncClient", error);
       this.logger.error("joinSyncClient", error);
@@ -49,11 +54,11 @@ export class SyncDeviceController extends BasicController {
           user: this.room.user,
           user: this.room.user,
           roomsPerson: AllRoomUsers,
           roomsPerson: AllRoomUsers,
         });
         });
-        // this.socket.broadcast.to(this.room.syncId).emit(EVENT.changeVoiceStatus, {
-        //   ...data,
-        //   user: this.room.user,
-        //   roomsPerson: AllRoomUsers,
-        // });
+        this.socket.broadcast.to(this.room.syncId).emit(EVENT.changeVoiceStatus, {
+          ...updateUser,
+          user: this.room.user,
+          roomsPerson: AllRoomUsers,
+        });
       } catch (error) {
       } catch (error) {
         this.logger.error("event::changeVoiceStatus", error);
         this.logger.error("event::changeVoiceStatus", error);
       }
       }

+ 10 - 5
src/service/roomConfigService.js

@@ -1,5 +1,10 @@
 import { pubClient } from "../connection/redis.js";
 import { pubClient } from "../connection/redis.js";
 
 
+const prefix = process.env.REDIS_PREFIX || "chat";
+
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
 const setRoomConfig = (roomId, config) => {
 const setRoomConfig = (roomId, config) => {
   const roomConfigKey = `config:${roomId}`;
   const roomConfigKey = `config:${roomId}`;
   // console.log("roomConfigKey", roomConfigKey);
   // console.log("roomConfigKey", roomConfigKey);
@@ -7,22 +12,22 @@ const setRoomConfig = (roomId, config) => {
   //   console.log("item", item, config[item]);
   //   console.log("item", item, config[item]);
   //   // pubClient.set(roomConfigKey, item, config[item]);// 旧版
   //   // pubClient.set(roomConfigKey, item, config[item]);// 旧版
   // });
   // });
-  pubClient.hSet(roomConfigKey, config); // 5.0 可以用hash set
+  pubClient.hSet(getInKey(roomConfigKey), config); // 5.0 可以用hash set
 };
 };
 const removeRoomConfig = (roomId, config) => {
 const removeRoomConfig = (roomId, config) => {
   const roomConfigKey = `config:${roomId}`;
   const roomConfigKey = `config:${roomId}`;
-  return pubClient.del(roomConfigKey);
+  return pubClient.del(getInKey(roomConfigKey));
 };
 };
 const getRoomConfig = (roomId, config) => {
 const getRoomConfig = (roomId, config) => {
   const roomConfigKey = `config:${roomId}`;
   const roomConfigKey = `config:${roomId}`;
-  return pubClient.hGetAll(roomConfigKey);
+  return pubClient.hGetAll(getInKey(roomConfigKey));
 };
 };
 const updateRoomConfigByKey = async (roomId, keyName, value) => {
 const updateRoomConfigByKey = async (roomId, keyName, value) => {
   const roomConfigKey = `config:${roomId}`;
   const roomConfigKey = `config:${roomId}`;
-  const res = await pubClient.hGetAll(roomConfigKey);
+  const res = await pubClient.hGetAll(getInKey(roomConfigKey));
   const newKey = { [keyName]: value };
   const newKey = { [keyName]: value };
   const updateObj = Object.assign({}, res, newKey);
   const updateObj = Object.assign({}, res, newKey);
-  return pubClient.hSet(roomConfigKey, updateObj);
+  return pubClient.hSet(getInKey(roomConfigKey), updateObj);
 };
 };
 const isRoomMaster = async (roomId, userId) => {
 const isRoomMaster = async (roomId, userId) => {
   // const roomConfigKey = `config:${roomId}`;
   // const roomConfigKey = `config:${roomId}`;

+ 12 - 7
src/service/userService.js

@@ -1,10 +1,15 @@
 import { pubClient } from "../connection/redis.js";
 import { pubClient } from "../connection/redis.js";
 
 
+const prefix = process.env.REDIS_PREFIX || "chat";
+
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
 const getCurrentUser = async (userId, key) => {
 const getCurrentUser = async (userId, key) => {
   try {
   try {
     if (userId) {
     if (userId) {
       console.log("getCurrentUser", userId, key);
       console.log("getCurrentUser", userId, key);
-      const user = await pubClient.hGet(userId, String(key));
+      const user = await pubClient.hGet(getInKey(userId), String(key));
       return Promise.resolve(user);
       return Promise.resolve(user);
     } else {
     } else {
       return Promise.resolve(false);
       return Promise.resolve(false);
@@ -15,14 +20,14 @@ const getCurrentUser = async (userId, key) => {
 };
 };
 
 
 const updateUser = async (userId, userObj) => {
 const updateUser = async (userId, userObj) => {
-  await pubClient.hSet(userId, userObj.from, JSON.stringify(userObj));
+  await pubClient.hSet(getInKey(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) => {
 const updateRoomUser = async (roomId, userId, userObj) => {
   try {
   try {
-    await pubClient.hSet(userId, userObj.from || 2, JSON.stringify(userObj));
-    await pubClient.hSet(roomId, userId, JSON.stringify(userObj));
+    await pubClient.hSet(getInKey(userId), userObj.from || 2, JSON.stringify(userObj));
+    await pubClient.hSet(getInKey(roomId), userId, JSON.stringify(userObj));
     return Promise.resolve(true);
     return Promise.resolve(true);
   } catch (error) {
   } catch (error) {
     return Promise.resolve(false);
     return Promise.resolve(false);
@@ -30,16 +35,16 @@ const updateRoomUser = async (roomId, userId, userObj) => {
 };
 };
 
 
 const removeRoomAllUsers = (roomId) => {
 const removeRoomAllUsers = (roomId) => {
-  return pubClient.del(roomId);
+  return pubClient.del(getInKey(roomId));
 };
 };
 
 
 const removeRoomUser = (roomId, useId) => {
 const removeRoomUser = (roomId, useId) => {
-  return pubClient.hDel(roomId, useId);
+  return pubClient.hDel(getInKey(roomId), useId);
 };
 };
 
 
 const getAllRoomUsers = async (roomId) => {
 const getAllRoomUsers = async (roomId) => {
   return new Promise(async (resolve) => {
   return new Promise(async (resolve) => {
-    const AllRoomUsers = await pubClient.hVals(roomId);
+    const AllRoomUsers = await pubClient.hVals(getInKey(roomId));
     const allUsers = Array.from(AllRoomUsers)
     const allUsers = Array.from(AllRoomUsers)
       .map((i) => JSON.parse(i))
       .map((i) => JSON.parse(i))
       .sort((a, b) => parseInt(a.order) - parseInt(b.order));
       .sort((a, b) => parseInt(a.order) - parseInt(b.order));

+ 7 - 2
src/service/watchRoomService.js

@@ -4,12 +4,17 @@ import { io } from "../core/io.js";
 import { FROMTYPE } from "../enum/index.js";
 import { FROMTYPE } from "../enum/index.js";
 import { leaveRoomAction } from "../controller/roomActionController.js";
 import { leaveRoomAction } from "../controller/roomActionController.js";
 
 
+const prefix = process.env.REDIS_PREFIX || "chat";
+
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
 const passiveLeave = async (roomId, userId, socket) => {
 const passiveLeave = async (roomId, userId, socket) => {
   try {
   try {
     setTimeout(async () => {
     setTimeout(async () => {
-      const isExistUser = await pubClient.exists(userId);
+      const isExistUser = await pubClient.exists(getInKey(userId));
       if (isExistUser === 1) {
       if (isExistUser === 1) {
-        const baseUser = await pubClient.hGetAll(userId);
+        const baseUser = await pubClient.hGetAll(getInKey(userId));
         const user = JSON.parse(baseUser[FROMTYPE.MiniAPP]);
         const user = JSON.parse(baseUser[FROMTYPE.MiniAPP]);
         console.log("user-isConnected", user.isConnected);
         console.log("user-isConnected", user.isConnected);
         if (user && !user.isConnected) {
         if (user && !user.isConnected) {