Browse Source

强制上线

gemercheung 3 years ago
parent
commit
a6da9d35f4
2 changed files with 77 additions and 58 deletions
  1. 12 3
      src/controller/room/index.js
  2. 65 55
      src/service/userService.js

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

@@ -1,5 +1,5 @@
 import { ROLES, CODEMEG, EVENT, FROMTYPE } from "../../enum/index.js";
-import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser, removeRoomUser } from "../../service/userService.js";
+import { getCurrentUser, updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser, removeRoomUser, isUserInRooms } from "../../service/userService.js";
 // import { watchRoomService } from "../../service/watchRoomService.js";
 import { setRoomConfig, getRoomConfig, updateRoomConfigByKey, isRoomMaster } from "../../service/roomConfigService.js";
 
@@ -130,14 +130,23 @@ export class RoomController extends BasicController {
       // 加入
       console.log("roomId", this.roomId);
       this.socket.join(this.roomId);
+
+      const isInRoom = await isUserInRooms(this.roomId, this.userId);
+      if (isInRoom) {
+        if (Number(isInRoom.onlineStatus) === 0) {
+          this.setOnlineStatus(this.roomId, this.userId, isInRoom);
+        }
+      }
+      console.log("isInRoom", isInRoom);
     } else {
       this.logger.info("user-query-不存在 :", this.socket.handshake.query);
       this.socket.disconnect();
     }
   }
   // 下线用户强制上线
-  async setOnlineStatus(roomId, userId) {
-
+  async setOnlineStatus(roomId, userId, user) {
+    user.onlineStatus = 1;
+    await updateRoomUser(this.roomId, this.userId, user);
   }
 
   async initParams(userId, roomId, oneSceneNum) {

+ 65 - 55
src/service/userService.js

@@ -1,55 +1,65 @@
-import { pubClient } from "../connection/redis.js";
-
-const prefix = process.env.REDIS_PREFIX || "chat";
-
-const getInKey = (realKey) => {
-  return `${prefix}:${realKey}`;
-};
-const getCurrentUser = async (userId, key) => {
-  try {
-    if (userId) {
-      console.log("getCurrentUser", userId, key);
-      const user = await pubClient.hGet(getInKey(userId), String(key));
-      return Promise.resolve(user);
-    } else {
-      return Promise.resolve(false);
-    }
-  } catch (error) {
-    return Promise.resolve(false);
-  }
-};
-
-const updateUser = async (userId, userObj) => {
-  await pubClient.hSet(getInKey(userId), userObj.from, JSON.stringify(userObj));
-  // await pubClient.expire(userId, 60 * 60 * 24);
-};
-
-const updateRoomUser = async (roomId, userId, userObj) => {
-  try {
-    await pubClient.hSet(getInKey(userId), userObj.from, JSON.stringify(userObj));
-    await pubClient.hSet(getInKey(roomId), userId, JSON.stringify(userObj));
-    return Promise.resolve(true);
-  } catch (error) {
-    return Promise.resolve(false);
-  }
-};
-
-const removeRoomAllUsers = (roomId) => {
-  return pubClient.del(getInKey(roomId));
-};
-
-const removeRoomUser = (roomId, useId) => {
-  return pubClient.hDel(getInKey(roomId), useId);
-};
-
-const getAllRoomUsers = async (roomId) => {
-  return new Promise(async (resolve) => {
-    const AllRoomUsers = await pubClient.hVals(getInKey(roomId));
-    const allUsers = Array.from(AllRoomUsers)
-      .map((i) => JSON.parse(i))
-      .sort((a, b) => parseInt(a.order) - parseInt(b.order));
-    return resolve(allUsers);
-  });
-};
-
-export { getCurrentUser, updateUser, updateRoomUser, removeRoomAllUsers, removeRoomUser, getAllRoomUsers };
+import { pubClient } from "../connection/redis.js";
+
+const prefix = process.env.REDIS_PREFIX || "chat";
+
+const getInKey = (realKey) => {
+  return `${prefix}:${realKey}`;
+};
+const getCurrentUser = async (userId, key) => {
+  try {
+    if (userId) {
+      console.log("getCurrentUser", userId, key);
+      const user = await pubClient.hGet(getInKey(userId), String(key));
+      return Promise.resolve(user);
+    } else {
+      return Promise.resolve(false);
+    }
+  } catch (error) {
+    return Promise.resolve(false);
+  }
+};
+
+const updateUser = async (userId, userObj) => {
+  await pubClient.hSet(getInKey(userId), userObj.from, JSON.stringify(userObj));
+  // await pubClient.expire(userId, 60 * 60 * 24);
+};
+
+const updateRoomUser = async (roomId, userId, userObj) => {
+  try {
+    await pubClient.hSet(getInKey(userId), userObj.from, JSON.stringify(userObj));
+    await pubClient.hSet(getInKey(roomId), userId, JSON.stringify(userObj));
+    return Promise.resolve(true);
+  } catch (error) {
+    return Promise.resolve(false);
+  }
+};
+
+const removeRoomAllUsers = (roomId) => {
+  return pubClient.del(getInKey(roomId));
+};
+
+const removeRoomUser = (roomId, useId) => {
+  return pubClient.hDel(getInKey(roomId), useId);
+};
+
+const getAllRoomUsers = async (roomId) => {
+  return new Promise(async (resolve) => {
+    const AllRoomUsers = await pubClient.hVals(getInKey(roomId));
+    const allUsers = Array.from(AllRoomUsers)
+      .map((i) => JSON.parse(i))
+      .sort((a, b) => parseInt(a.order) - parseInt(b.order));
+    return resolve(allUsers);
+  });
+};
+
+const isUserInRooms = async (roomId, userId) => {
+  return new Promise(async (resolve) => {
+    const AllRoomUsers = await pubClient.hVals(getInKey(roomId));
+    const hasUser = Array.from(AllRoomUsers)
+      .map((i) => JSON.parse(i))
+      .find((u) => u.userId === userId);
+    return resolve(hasUser || false);
+  });
+};
+
+export { getCurrentUser, updateUser, updateRoomUser, removeRoomAllUsers, removeRoomUser, getAllRoomUsers, isUserInRooms };