|
@@ -2,8 +2,9 @@ import { pubClient } from "../connection/redis.js";
|
|
import { ROLES, CODEMEG, EVENT, FROMTYPE } from "../enum/index.js";
|
|
import { ROLES, CODEMEG, EVENT, FROMTYPE } from "../enum/index.js";
|
|
import { io } from "../core/io.js";
|
|
import { io } from "../core/io.js";
|
|
import { logger } from "../core/logger.js";
|
|
import { logger } from "../core/logger.js";
|
|
-import { updateUser, removeRoomAllUsers } from "../service/userService.js";
|
|
|
|
|
|
+import { updateUser, removeRoomAllUsers, getAllRoomUsers } from "../service/userService.js";
|
|
import { watchRoomService } from "../service/watchRoomService.js";
|
|
import { watchRoomService } from "../service/watchRoomService.js";
|
|
|
|
+import { setRoomConfig, getRoomConfig } from "../service/roomConfigService.js";
|
|
import { getSig } from "../core/getSig.js";
|
|
import { getSig } from "../core/getSig.js";
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -74,10 +75,11 @@ const joinRoomAction = async (roomId, userId, user, socket) => {
|
|
if (!isJoinRoom) {
|
|
if (!isJoinRoom) {
|
|
logger.info("加入房间 :", { userId, roomId, user });
|
|
logger.info("加入房间 :", { userId, roomId, user });
|
|
socket.join(roomId);
|
|
socket.join(roomId);
|
|
|
|
+ const AllRoomUsers = await getAllRoomUsers(roomId);
|
|
socket.broadcast.emit(EVENT.roomIn, {
|
|
socket.broadcast.emit(EVENT.roomIn, {
|
|
user,
|
|
user,
|
|
- // roomsPerson: sortRoomUser(roomsPerson),
|
|
|
|
- // roomsConfig: this._roomsConfig[roomId],
|
|
|
|
|
|
+ roomsPerson: AllRoomUsers,
|
|
|
|
+ roomsConfig: {},
|
|
});
|
|
});
|
|
} else {
|
|
} else {
|
|
logger.info(`已加入房间 :`, { userId });
|
|
logger.info(`已加入房间 :`, { userId });
|
|
@@ -117,7 +119,7 @@ const closeRoomAction = async (roomId, userId, user, socket) => {
|
|
};
|
|
};
|
|
export async function roomController(socket) {
|
|
export async function roomController(socket) {
|
|
let user = socket.handshake.query;
|
|
let user = socket.handshake.query;
|
|
- const { roomId, userId, sceneNum, isClient, role, userLimitNum, sceneNumber, roomType, from } = user;
|
|
|
|
|
|
+ const { roomId, userId, sceneNum, isClient, role, userLimitNum, sceneNumber, roomType, from, enableTalk } = user;
|
|
if (user) {
|
|
if (user) {
|
|
// const webRoomId = `${roomId}_${sceneNum}_web`;
|
|
// const webRoomId = `${roomId}_${sceneNum}_web`;
|
|
// const clientRoom = `${userId}${roomId}`;
|
|
// const clientRoom = `${userId}${roomId}`;
|
|
@@ -139,8 +141,11 @@ export async function roomController(socket) {
|
|
if ([FROMTYPE.MiniAPP].includes(Number(from))) {
|
|
if ([FROMTYPE.MiniAPP].includes(Number(from))) {
|
|
logger.debug("房主自动创建房间 :", { roomId: roomUniqueId, userId: userUniqueId, user });
|
|
logger.debug("房主自动创建房间 :", { roomId: roomUniqueId, userId: userUniqueId, user });
|
|
buildRoom(roomUniqueId, userUniqueId, user);
|
|
buildRoom(roomUniqueId, userUniqueId, user);
|
|
|
|
+ await setRoomConfig(roomUniqueId, {
|
|
|
|
+ userLimitNum,
|
|
|
|
+ enableTalk: enableTalk,
|
|
|
|
+ });
|
|
}
|
|
}
|
|
-
|
|
|
|
socket.join(roomUniqueId);
|
|
socket.join(roomUniqueId);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
@@ -151,22 +156,64 @@ export async function roomController(socket) {
|
|
|
|
|
|
socket.on(EVENT.startCall, startCallAction.bind(null, roomUniqueId, userUniqueId, user, socket));
|
|
socket.on(EVENT.startCall, startCallAction.bind(null, roomUniqueId, userUniqueId, user, socket));
|
|
socket.on(EVENT.stopCall, stopCallAction.bind(null, roomUniqueId, userUniqueId, user, socket));
|
|
socket.on(EVENT.stopCall, stopCallAction.bind(null, roomUniqueId, userUniqueId, user, socket));
|
|
|
|
+
|
|
socket.on(EVENT.action, (data) => {
|
|
socket.on(EVENT.action, (data) => {
|
|
- //console.log(data, 'clientSyncAction')
|
|
|
|
|
|
+ console.log("room -action");
|
|
socket.broadcast.to(roomUniqueId).emit(EVENT.action, data);
|
|
socket.broadcast.to(roomUniqueId).emit(EVENT.action, data);
|
|
});
|
|
});
|
|
- socket.onAny((event) => {
|
|
|
|
- console.log(`got ${event}`);
|
|
|
|
|
|
+
|
|
|
|
+ // 通知 baseView 减少大量通知
|
|
|
|
+ socket.on(EVENT.webSyncAction, (data) => {
|
|
|
|
+ if ([FROMTYPE.base].includes(Number(from))) {
|
|
|
|
+ socket.broadcast.to(roomUniqueId).emit(EVENT.webSyncAction, data);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ socket.onAny((event, data) => {
|
|
|
|
+ // console.log(`onAny:get ${event}`);
|
|
|
|
+ console.log(`onAny:get ${event}, data:${JSON.stringify(data)}`);
|
|
});
|
|
});
|
|
|
|
+
|
|
|
|
+ // test
|
|
|
|
+
|
|
|
|
+ socket.on(EVENT.changeOnlineStatus, async (data) => {
|
|
|
|
+ user.onlineStatus = data.status;
|
|
|
|
+ const AllRoomUsers = await getAllRoomUsers(roomUniqueId);
|
|
|
|
+ let actionName = user.onlineStatus ? "inRoom" : "leaveRoom";
|
|
|
|
+ socket.broadcast.to(roomUniqueId).emit(EVENT.roomPersonChange, {
|
|
|
|
+ roomsPerson: AllRoomUsers,
|
|
|
|
+ actionName,
|
|
|
|
+ user,
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+
|
|
socket.on("disconnect", async (reason) => {
|
|
socket.on("disconnect", async (reason) => {
|
|
// leaveRoom({ clientRoom, userUniqueId, roomUniqueId, socket });
|
|
// leaveRoom({ clientRoom, userUniqueId, roomUniqueId, socket });
|
|
// watchRoomWithMaster(roomUniqueId, socket);
|
|
// watchRoomWithMaster(roomUniqueId, socket);
|
|
console.log("reason", reason);
|
|
console.log("reason", reason);
|
|
- if (reason === "transport close") {
|
|
|
|
|
|
+ if (reason === "transport close" || reason === "client namespace disconnect") {
|
|
// 主动离开
|
|
// 主动离开
|
|
|
|
+
|
|
|
|
+ shotGunLeaveAction(
|
|
|
|
+ {
|
|
|
|
+ syncId,
|
|
|
|
+ roomId: roomUniqueId,
|
|
|
|
+ userId: userUniqueId,
|
|
|
|
+ },
|
|
|
|
+ socket,
|
|
|
|
+ user,
|
|
|
|
+ );
|
|
} else {
|
|
} else {
|
|
// 被动离开
|
|
// 被动离开
|
|
- disconnectAction(roomUniqueId, userUniqueId, socket, user);
|
|
|
|
|
|
+ disconnectAction(
|
|
|
|
+ {
|
|
|
|
+ syncId,
|
|
|
|
+ roomId: roomUniqueId,
|
|
|
|
+ userId: userUniqueId,
|
|
|
|
+ },
|
|
|
|
+ socket,
|
|
|
|
+ user,
|
|
|
|
+ );
|
|
}
|
|
}
|
|
});
|
|
});
|
|
socket.on("reconnect", () => {
|
|
socket.on("reconnect", () => {
|
|
@@ -174,7 +221,18 @@ export async function roomController(socket) {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-const disconnectAction = async (roomId, userId, socket, user) => {
|
|
|
|
|
|
+const shotGunLeaveAction = async (ids, socket, user) => {
|
|
|
|
+ console.log("主动离开");
|
|
|
|
+ const { syncId, roomId, userId } = ids;
|
|
|
|
+ // await pubClient.del(syncId);
|
|
|
|
+ // if (isHoster(user.role)) {
|
|
|
|
+ // await pubClient.del(roomId);
|
|
|
|
+ // }
|
|
|
|
+ // await pubClient.del(userId);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const disconnectAction = async (ids, socket, user) => {
|
|
|
|
+ const { roomId, userId } = ids;
|
|
const userObj = { ...user, isConnected: false };
|
|
const userObj = { ...user, isConnected: false };
|
|
updateUser(userId, userObj);
|
|
updateUser(userId, userObj);
|
|
watchRoomService(roomId, userId, socket);
|
|
watchRoomService(roomId, userId, socket);
|
|
@@ -182,8 +240,9 @@ const disconnectAction = async (roomId, userId, socket, user) => {
|
|
|
|
|
|
const startCallAction = async (roomId, userId, user, socket) => {
|
|
const startCallAction = async (roomId, userId, user, socket) => {
|
|
console.log("startCallAction");
|
|
console.log("startCallAction");
|
|
|
|
+ // try {
|
|
if (!isHoster(user.role)) {
|
|
if (!isHoster(user.role)) {
|
|
- joinRoomAction(roomId, userId, user, socket);
|
|
|
|
|
|
+ await joinRoomAction(roomId, userId, user, socket);
|
|
} else {
|
|
} else {
|
|
const hasRoom = await pubClient.hVals(roomId);
|
|
const hasRoom = await pubClient.hVals(roomId);
|
|
if (hasRoom.length === 0) {
|
|
if (hasRoom.length === 0) {
|
|
@@ -192,15 +251,23 @@ const startCallAction = async (roomId, userId, user, socket) => {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
user.isInRoom = true;
|
|
user.isInRoom = true;
|
|
|
|
+ const AllRoomUsers = await getAllRoomUsers(roomId);
|
|
|
|
+
|
|
|
|
+ logger.info("roomId", roomId);
|
|
|
|
+ logger.info("AllRoomUsers", AllRoomUsers.length, AllRoomUsers);
|
|
socket.broadcast.to(roomId).emit(EVENT.someOneInRoom, {
|
|
socket.broadcast.to(roomId).emit(EVENT.someOneInRoom, {
|
|
user,
|
|
user,
|
|
- roomsPerson: [user],
|
|
|
|
|
|
+ roomsPerson: AllRoomUsers,
|
|
});
|
|
});
|
|
|
|
+
|
|
socket.emit(EVENT.roomIn, {
|
|
socket.emit(EVENT.roomIn, {
|
|
user,
|
|
user,
|
|
- roomsPerson: [user],
|
|
|
|
|
|
+ roomsPerson: AllRoomUsers,
|
|
roomsConfig: {},
|
|
roomsConfig: {},
|
|
});
|
|
});
|
|
|
|
+ // } catch (error) {
|
|
|
|
+ // logger.error("startCallAction:", error);
|
|
|
|
+ // }
|
|
};
|
|
};
|
|
|
|
|
|
const stopCallAction = (roomId, userId, user, socket) => {
|
|
const stopCallAction = (roomId, userId, user, socket) => {
|