import { BasicController } from "./basicController.js"; // import { RoomAssistant } from "./assistant.js"; import { getAllRoomUsers, updateRoomUser } from "../service/userService.js"; import { FROMTYPE, EVENT } from "../enum/index.js"; const prefix = process.env.REDIS_PREFIX || "chat"; const getInKey = (realKey) => { return `${prefix}:${realKey}`; }; export class SyncDeviceController extends BasicController { constructor(...args) { super(...args); this.room = args[3]; } async run() { if (this.room.user) { const syncId = this.room.syncId; const userId = this.room.userId; const from = this.room.user.from; this.joinSyncClient(syncId, userId, from); this.initAction(); } else { this.logger.error("roomController:init: 初始化失败!"); } } async joinSyncClient(syncId, userId, from) { this.logger.info("syncId", { syncId, userId, from }); try { await this.redisCli.hSet(getInKey(syncId), from, userId); this.socket.join(syncId); } catch (error) { this.logger.error("joinSyncClient", error); } } initAction() { this.socket.on(EVENT.clientSyncAction, (data) => { this.socket.broadcast.to(this.room.syncId).emit(EVENT.clientSyncAction, data); }); this.socket.on(EVENT.changeVoiceStatus, async ({ status }) => { try { const current = await this.room.currentUser(); const updateUser = { ...current, // onlineStatus: 1, voiceStatus: status, }; const isUpdate = await updateRoomUser(this.room.roomId, this.room.userId, updateUser); const AllRoomUsers = await getAllRoomUsers(this.room.roomId); this.logger.info("changeVoiceStatus", { roomId: this.room.roomId, updateUser, AllRoomUsers: AllRoomUsers.length }); this.socket.broadcast.to(this.room.roomId).emit(EVENT.changeVoiceStatus, { user: updateUser, roomsPerson: AllRoomUsers, }); this.socket.broadcast.to(this.room.syncId).emit(EVENT.changeVoiceStatus, { user: updateUser, roomsPerson: AllRoomUsers, }); } catch (error) { this.logger.error("event::changeVoiceStatus", error); } }); } }