gemercheung 3 rokov pred
rodič
commit
d27e2a28f9

+ 10 - 1
src/controller/room/assistant.js

@@ -2,6 +2,7 @@
 import { EVENT, CODEMEG, FROMTYPE } from "../../enum/index.js";
 import { EVENT, CODEMEG, FROMTYPE } from "../../enum/index.js";
 import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser, removeRoomUser } from "../../service/userService.js";
 import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser, removeRoomUser } from "../../service/userService.js";
 import { setRoomConfig, getRoomConfig, isRoomMaster } from "../../service/roomConfigService.js";
 import { setRoomConfig, getRoomConfig, isRoomMaster } from "../../service/roomConfigService.js";
+import { createMessage } from "../../service/msgService.js";
 import { subClient } from "../../connection/redis.js";
 import { subClient } from "../../connection/redis.js";
 
 
 const prefix = process.env.REDIS_PREFIX || "chat";
 const prefix = process.env.REDIS_PREFIX || "chat";
@@ -335,7 +336,7 @@ export class RoomAssistant {
             if (checkIsRoomMaster) {
             if (checkIsRoomMaster) {
               this.room.logger.info("房主已存在房间 :", { roomId, userId, from: user.from });
               this.room.logger.info("房主已存在房间 :", { roomId, userId, from: user.from });
               await this.joinRoom(roomId, userId, user);
               await this.joinRoom(roomId, userId, user);
-              
+
               // this.notifyUserJitter(roomId);
               // this.notifyUserJitter(roomId);
             } else {
             } else {
               this.room.logger.error("存在非法房主", roomId, userId);
               this.room.logger.error("存在非法房主", roomId, userId);
@@ -470,6 +471,14 @@ export class RoomAssistant {
     }
     }
   }
   }
 
 
+  /**
+   * 记录房间内MSG
+   */
+
+  async sendMessage(roomId, user, msg) {
+    await createMessage(roomId, user, msg);
+  }
+
   // 主动断开
   // 主动断开
   async disconnect() {
   async disconnect() {
     try {
     try {

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

@@ -138,7 +138,7 @@ export class RoomController extends BasicController {
             setRoomMaster();
             setRoomMaster();
           } else {
           } else {
             if (checkoutMaster) {
             if (checkoutMaster) {
-              console.log("二次房主进入",JSON.stringify(roomConfig));
+              console.log("二次房主进入", JSON.stringify(roomConfig));
               setRoomMaster();
               setRoomMaster();
             } else {
             } else {
               this.roomAssistant.illegalMaster = true;
               this.roomAssistant.illegalMaster = true;
@@ -201,6 +201,11 @@ export class RoomController extends BasicController {
         this.logger.debug("room-action", this.roomId, this.socket.rooms.has(this.roomId), JSON.stringify(data));
         this.logger.debug("room-action", this.roomId, this.socket.rooms.has(this.roomId), JSON.stringify(data));
         if (this.socket.rooms.has(this.roomId)) {
         if (this.socket.rooms.has(this.roomId)) {
           this.socket.broadcast.to(this.roomId).emit(EVENT.action, data);
           this.socket.broadcast.to(this.roomId).emit(EVENT.action, data);
+          // 从action获取msg事件
+          if (data.type === "msg") {
+            const { user, msg } = data.data;
+            this.roomAssistant.sendMessage(this.roomId, user, msg);
+          }
         } else {
         } else {
           this.logger.error("action 事件不在房间内", this.user);
           this.logger.error("action 事件不在房间内", this.user);
         }
         }

+ 19 - 0
src/service/msgService.js

@@ -0,0 +1,19 @@
+import { pubClient } from "../connection/redis.js";
+
+const prefix = process.env.REDIS_PREFIX || "chat";
+
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
+
+const createMessage = async (roomId, user, msg) => {
+  const msgConfigKey = `msg:${roomId}`;
+  const msgObj = {
+    ...user,
+    msg: msg,
+    createTime: Math.floor(Date.now() / 1000),
+  };
+  return pubClient.hSet(getInKey(msgConfigKey), updateObj);
+};
+
+export { createMessage };