| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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, updateRoomConfigByKey } from "../../service/roomConfigService.js";
- import { RoomAssistant } from "./assistant.js";
- import { BasicController } from "../basicController.js";
- export class RoomController extends BasicController {
- constructor(...args) {
- super(...args);
- this.roomAssistant = new RoomAssistant(this.socket, this.redisCli, this);
- this.roomId = null;
- this.sessionId = null;
- this.userId = null;
- this.roomConfigId = null;
- this.debugger = true;
- this.user = {
- sig: null,
- roomId: null,
- userId: null,
- sceneNum: null,
- isClient: null,
- role: null,
- userLimitNum: null,
- sceneNumber: null,
- roomType: null,
- from: null,
- enableTalk: null,
- };
- }
- async run() {
- this.logger.info("socket conetcted has start!");
- try {
- await this.init();
- this.initBaseAction();
- this.roomMasterAutoRejoin();
- this.roomAssistant.watchRoomExpired();
- } catch (error) {
- this.logger.error("roomController::run::error", error);
- }
- }
- async init() {
- let user = this.socket.handshake.query;
- if (user) {
- this.user = Object.assign({}, user, {
- roomType: user.roomType || "",
- });
- this.user.sig = this.getSig(this.user.userId);
- const oneSceneNum = this.user.sceneNumber || this.user.sceneNum;
- 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
- if (this.isHoster(this.user.role)) {
- if ([FROMTYPE.MiniAPP].includes(Number(this.user.from))) {
- await setRoomConfig(this.roomId, {
- userLimitNum: this.user.userLimitNum,
- enableTalk: this.user.enableTalk === "true" ? true : false,
- });
- }
- }
- // 加入
- this.socket.join(this.roomId);
- } else {
- this.logger.info("user-query-不存在 :", this.socket.handshake.query);
- this.socket.disconnect();
- }
- }
- async initParams(userId, roomId, oneSceneNum) {
- this.userId = `user:${userId}`;
- this.syncId = `sync:${oneSceneNum}:${userId}`;
- this.sessionId = `session:${oneSceneNum}:${userId}`;
- const uRoomId = await this.roomAssistant.prepearRoom(this.sessionId, roomId);
- // this.roomId = `room:${uRoomId}_${oneSceneNum}`;
- this.roomId = `room:${oneSceneNum}:${uRoomId}`;
- this.user.roomId = uRoomId;
- this.roomConfigId = `config:${this.roomId}`;
- return Promise.resolve(true);
- }
- initBaseAction() {
- // 通知 baseView 减少大量通知
- this.socket.on(EVENT.webSyncAction, (data) => {
- // socket.broadcast.to(roomUniqueId).emit(EVENT.webSyncAction, data);
- try {
- if ([FROMTYPE.base].includes(Number(this.user.from))) {
- this.socket.broadcast.to(this.roomId).emit(EVENT.webSyncAction, data);
- }
- } catch (error) {
- this.logger.error("roomController::EVENT.webSyncAction", error);
- }
- });
- // 转发action
- this.socket.on(EVENT.action, (data) => {
- try {
- this.logger.debug("room-action", this.roomId, this.socket.rooms.has(this.roomId), JSON.stringify(data));
- if (this.socket.rooms.has(this.roomId)) {
- this.socket.broadcast.to(this.roomId).emit(EVENT.action, data);
- } else {
- this.logger.error("action 事件不在房间内", this.user);
- }
- } catch (error) {
- this.logger.error("roomController::EVENT.action", error);
- }
- });
- 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) {
- this.socket.onAny((event, data) => {
- if (event !== "webSyncAction") {
- console.log(`onAny:get ${event}, data:${JSON.stringify(data)}`);
- }
- });
- }
- }
- 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);
- }
- }
- }
|