|
@@ -1,7 +1,7 @@
|
|
|
import { ROLES, CODEMEG, EVENT, FROMTYPE } from "../../enum/index.js";
|
|
|
import { updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser } from "../../service/userService.js";
|
|
|
// import { watchRoomService } from "../../service/watchRoomService.js";
|
|
|
-import { setRoomConfig, getRoomConfig } from "../../service/roomConfigService.js";
|
|
|
+import { setRoomConfig, getRoomConfig, updateRoomConfigByKey } from "../../service/roomConfigService.js";
|
|
|
|
|
|
import { RoomAssistant } from "./assistant.js";
|
|
|
import { BasicController } from "../basicController.js";
|
|
@@ -13,6 +13,7 @@ export class RoomController extends BasicController {
|
|
|
this.roomId = null;
|
|
|
this.sessionId = null;
|
|
|
this.userId = null;
|
|
|
+ this.roomConfigId = null;
|
|
|
this.debugger = true;
|
|
|
this.user = {
|
|
|
sig: null,
|
|
@@ -34,6 +35,7 @@ export class RoomController extends BasicController {
|
|
|
try {
|
|
|
await this.init();
|
|
|
this.initBaseAction();
|
|
|
+ this.roomMasterAutoRejoin();
|
|
|
} catch (error) {
|
|
|
this.logger.error("roomController::run::error", error);
|
|
|
}
|
|
@@ -50,6 +52,7 @@ export class RoomController extends BasicController {
|
|
|
const { userId, roomId } = this.user;
|
|
|
await this.initParams(userId, roomId, oneSceneNum);
|
|
|
const userObj = { ...this.user, isConnected: true };
|
|
|
+ // console.log('userObj',userObj)
|
|
|
updateUser(this.userId, userObj);
|
|
|
|
|
|
// 只有来源于小程序用户信息才记录到redis
|
|
@@ -76,6 +79,9 @@ export class RoomController extends BasicController {
|
|
|
this.sessionId = `${oneSceneNum}-${userId}`;
|
|
|
const uRoomId = await this.roomAssistant.prepearRoom(this.sessionId, roomId);
|
|
|
this.roomId = `room-${uRoomId}_${oneSceneNum}_web`;
|
|
|
+ this.user.roomId = uRoomId;
|
|
|
+ this.roomConfigId = `config-${this.roomId}`;
|
|
|
+ return Promise.resolve(true)
|
|
|
}
|
|
|
|
|
|
initBaseAction() {
|
|
@@ -104,11 +110,47 @@ export class RoomController extends BasicController {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- this.socket.on(EVENT.startCall, () => {
|
|
|
+ this.socket.on(EVENT.startCall, async () => {
|
|
|
this.roomAssistant.startCall(this.roomId, this.userId, this.user);
|
|
|
+ if (this.isHoster(this.user.role)) {
|
|
|
+ // 以startCall做为真正的进入房间
|
|
|
+ await updateRoomConfigByKey(this.roomId, "isStart", true);
|
|
|
+ }
|
|
|
});
|
|
|
this.socket.on(EVENT.stopCall, () => {
|
|
|
this.roomAssistant.stopCall(this.roomId, this.userId, this.user);
|
|
|
+ if (this.isHoster(this.user.role)) {
|
|
|
+ // 以stopCall断开做为真正的退出房间
|
|
|
+ this.roomAssistant.destoryRoom(this.sessionId, this.roomConfigId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ this.socket.on(EVENT.changeRoomEnableTalk, async (data) => {
|
|
|
+ // this._roomsConfig[roomId].enableTalk = data;
|
|
|
+ try {
|
|
|
+ await setRoomConfig(this.roomId, data);
|
|
|
+ const roomConfig = await getRoomConfig(this.roomId);
|
|
|
+ this.socket.broadcast.to(this.roomId).emit(EVENT.changeRoomEnableTalk, roomConfig);
|
|
|
+ } catch (error) {
|
|
|
+ this.logger.error("event:changeRoomEnableTalk", error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ this.socket.on(EVENT.changeOnlineStatus, async (data) => {
|
|
|
+ try {
|
|
|
+ this.user.onlineStatus = data.status;
|
|
|
+ const AllRoomUsers = await getAllRoomUsers(this.roomId);
|
|
|
+ await updateRoomUser(this.roomId, this.userId, this.user);
|
|
|
+ let actionName = this.user.onlineStatus ? "inRoom" : "leaveRoom";
|
|
|
+ this.logger.info("changeOnlineStatus", JSON.stringify(this.user));
|
|
|
+ this.socket.broadcast.to(this.roomId).emit(EVENT.roomPersonChange, {
|
|
|
+ roomsPerson: AllRoomUsers,
|
|
|
+ actionName,
|
|
|
+ user: this.user,
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ this.logger.error("event:changeOnlineStatus", error);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
if (this.debugger) {
|
|
@@ -120,7 +162,33 @@ export class RoomController extends BasicController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- roomMasterAutoRejoin() {
|
|
|
- // const
|
|
|
+ async roomMasterAutoRejoin() {
|
|
|
+ try {
|
|
|
+ const sessionExist = await this.redisCli.exists(this.sessionId);
|
|
|
+ const roomConfig = await getRoomConfig(this.roomId);
|
|
|
+ if (sessionExist > 0 && roomConfig.isStart && roomConfig.isStart === "true") {
|
|
|
+
|
|
|
+ const AllRoomUsers = await getAllRoomUsers(this.roomId);
|
|
|
+ // 房主
|
|
|
+ if (this.isHoster(this.user.role)) {
|
|
|
+ console.log("自动接连", this.roomId);
|
|
|
+ this.socket.join(this.roomId);
|
|
|
+
|
|
|
+ this.socket.emit("autoReJoin", {
|
|
|
+ user: this.user,
|
|
|
+ roomsPerson: AllRoomUsers,
|
|
|
+ roomId:this.user.roomId
|
|
|
+ });
|
|
|
+ }
|
|
|
+ setTimeout(() => {
|
|
|
+ this.socket.emit("someOneInRoom", {
|
|
|
+ user: this.user,
|
|
|
+ roomsPerson: AllRoomUsers,
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ this.logger.error("room::roomMasterAutoRejoin", error);
|
|
|
+ }
|
|
|
}
|
|
|
}
|