gemercheung 2 سال پیش
والد
کامیت
757d06dc0f
7فایلهای تغییر یافته به همراه81 افزوده شده و 51 حذف شده
  1. 1 1
      .env
  2. 3 2
      .env.development
  3. 45 45
      package.json
  4. 3 1
      src/controller/connectController.js
  5. 3 1
      src/controller/room/assistant.js
  6. 2 1
      src/service/msgService.js
  7. 24 0
      src/service/publishService.js

+ 1 - 1
.env

@@ -7,7 +7,7 @@ VOLUME_DIR=./data
 REDIS_HOST=redis
 REDIS_PORT=6379
 REDIS_PASSWORD=s1mpl3
-REDIS_DB=10
+REDIS_DB=0
 REDIS_PREFIX=chat
 WATCH_USER=4dage
 WATCH_PASSWORD=4dage168.

+ 3 - 2
.env.development

@@ -1,10 +1,11 @@
 PORT=12001
-SOCKET_PORT=12001
+SOCKET_PORT=12000
 CONTAINER_NAME='socket-chat-IM'
+SOCKET_PATH="/new-zfb"
 VOLUME_DIR=./data
 REDIS_HOST=127.0.0.1
 REDIS_PORT=6379
-REDIS_DB=10
+REDIS_DB=0
 REDIS_PASSWORD=redis9394
 WATCH_USER=4dage
 WATCH_PASSWORD=4dage168.

+ 45 - 45
package.json

@@ -1,45 +1,45 @@
-{
-  "name": "chatim",
-  "version": "1.0.0",
-  "description": "",
-  "main": "index.js",
-  "scripts": {
-    "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",
-  "author": "",
-  "license": "ISC",
-  "dependencies": {
-    "@elastic/elasticsearch": "^8.1.0",
-    "@socket.io/admin-ui": "^0.2.0",
-    "@socket.io/cluster-adapter": "^0.1.0",
-    "@socket.io/redis-adapter": "^7.1.0",
-    "@socket.io/sticky": "^1.0.1",
-    "axios": "^0.27.2",
-    "bcrypt": "^5.0.1",
-    "dotenv-flow": "^3.2.0",
-    "eiows": "^3.7.3",
-    "elastic-apm-node": "^3.31.0",
-    "engine.io": "^6.1.2",
-    "fastify": "^3.25.3",
-    "fastify-socket.io": "^3.0.0",
-    "recluster": "^1.0.0",
-    "redis": "^4.0.2",
-    "socket.io": "^4.4.1",
-    "socket.io-msgpack-parser": "^3.0.1",
-    "socket.io-prometheus-v3": "^1.0.3",
-    "tls-sig-api-v2": "^1.0.2",
-    "winston": "^3.4.0",
-    "winston-elasticsearch": "^0.16.1"
-  },
-  "optionalDependencies": {
-    "bufferutil": "^4.0.6",
-    "utf-8-validate": "^5.0.8"
-  },
-  "devDependencies": {
-    "cross-env": "^7.0.3",
-    "socket.io-client": "^4.4.1"
-  }
-}
+{
+  "name": "chatim",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "start": "cross-env NODE_ENV=development silent=true node 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",
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "@elastic/elasticsearch": "^8.1.0",
+    "@socket.io/admin-ui": "^0.2.0",
+    "@socket.io/cluster-adapter": "^0.1.0",
+    "@socket.io/redis-adapter": "^7.1.0",
+    "@socket.io/sticky": "^1.0.1",
+    "axios": "^0.27.2",
+    "bcrypt": "^5.0.1",
+    "dotenv-flow": "^3.2.0",
+    "eiows": "^3.7.3",
+    "elastic-apm-node": "^3.31.0",
+    "engine.io": "^6.1.2",
+    "fastify": "^3.25.3",
+    "fastify-socket.io": "^3.0.0",
+    "recluster": "^1.0.0",
+    "redis": "^4.0.2",
+    "socket.io": "^4.4.1",
+    "socket.io-msgpack-parser": "^3.0.1",
+    "socket.io-prometheus-v3": "^1.0.3",
+    "tls-sig-api-v2": "^1.0.2",
+    "winston": "^3.4.0",
+    "winston-elasticsearch": "^0.16.1"
+  },
+  "optionalDependencies": {
+    "bufferutil": "^4.0.6",
+    "utf-8-validate": "^5.0.8"
+  },
+  "devDependencies": {
+    "cross-env": "^7.0.3",
+    "socket.io-client": "^4.4.1"
+  }
+}

+ 3 - 1
src/controller/connectController.js

@@ -3,7 +3,7 @@ import { RoomController } from "./room/index.js";
 import { SyncDeviceController } from "./syncDeviceController.js";
 import { FROMTYPE } from "../enum/index.js";
 import { updateRoomUser, isUserInRoom, removeRoomUser } from "../service/userService.js";
-
+import { publishExitRoom } from "../service/publishService.js";
 export class ConnectController extends BasicController {
   constructor(...args) {
     super(...args);
@@ -63,6 +63,7 @@ export class ConnectController extends BasicController {
         });
         // if (this.roomController.isHoster(role)) {
         this.roomController.roomAssistant.hasCall = false;
+
         setTimeout(async () => {
           // 有可客户端主动断开时
           if (Number(user.onlineStatus) === 1) {
@@ -81,6 +82,7 @@ export class ConnectController extends BasicController {
                 console.log("已在线,被断开后", isInRoom);
                 await updateRoomUser(roomId, userId, isInRoom);
                 await this.roomController.roomAssistant.notifyUsersChange(roomId, user, false);
+                publishExitRoom(userId, user.roomId);
               }
             }
           }

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

@@ -4,6 +4,7 @@ import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, update
 import { setRoomConfig, getRoomConfig, isRoomMaster } from "../../service/roomConfigService.js";
 import { createMessage } from "../../service/msgService.js";
 import { subClient } from "../../connection/redis.js";
+import { publishEnterRoom } from "../../service/publishService.js";
 
 const prefix = process.env.REDIS_PREFIX || "chat";
 const getInKey = (realKey) => {
@@ -366,6 +367,7 @@ export class RoomAssistant {
           user,
           roomsPerson: AllRoomUsers,
         });
+        publishEnterRoom(userId, roomId);
       } else {
         this.room.logger.warn("超出房间上限");
         this.socket.emit(EVENT.roomMaximum, user);
@@ -479,7 +481,7 @@ export class RoomAssistant {
     try {
       await createMessage(roomId, user, msg);
     } catch (error) {
-      console.log("error", sendMessage);
+      console.log("error", error);
     }
   }
 

+ 2 - 1
src/service/msgService.js

@@ -15,7 +15,8 @@ const createMessage = (roomId, user, msg) => {
     msg: msg,
     createTime: Math.floor(Date.now() / 1000),
   };
-  return pubClient.hSet(getInKey(msgConfigKey), user.userId, JSON.stringify(msgObj));
+  pubClient.publish("getRoomMessage", JSON.stringify(msgObj));
+  return pubClient.hSet(getInKey(msgConfigKey), `${msgObj.createTime}`, JSON.stringify(msgObj));
 };
 
 export { createMessage };

+ 24 - 0
src/service/publishService.js

@@ -0,0 +1,24 @@
+import { pubClient } from "../connection/redis.js";
+
+const prefix = process.env.REDIS_PREFIX || "chat";
+
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
+
+const publishEnterRoom = (userId, roomId) => {
+  const payLoad = {
+    userId,
+    roomId,
+  };
+  pubClient.publish("enterRoom", JSON.stringify(payLoad));
+};
+const publishExitRoom = (userId, roomId) => {
+  const payLoad = {
+    userId,
+    roomId,
+  };
+  pubClient.publish("exitRoom", JSON.stringify(payLoad));
+};
+
+export { publishEnterRoom, publishExitRoom };