|
@@ -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);
|