assistant.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // 房间行为助手
  2. import { EVENT, CODEMEG } from "../../enum/index.js";
  3. import { updateUser, removeRoomAllUsers, getAllRoomUsers, updateRoomUser } from "../../service/userService.js";
  4. import { setRoomConfig, getRoomConfig } from "../../service/roomConfigService.js";
  5. import { subClient } from "../../connection/redis.js";
  6. export class RoomAssistant {
  7. constructor(socket, redis, room) {
  8. this.socket = socket;
  9. this.redis = redis;
  10. this.roomId = null;
  11. this.room = room;
  12. }
  13. async prepearRoom(roomSessionId, roomId) {
  14. const uRoomId = await this.redis.get(roomSessionId);
  15. const mergeRoomId = uRoomId || roomId;
  16. this.roomId = mergeRoomId;
  17. console.log("prepearRoom", roomSessionId, this.roomId);
  18. await this.redis.set(roomSessionId, mergeRoomId);
  19. return Promise.resolve(this.roomId);
  20. }
  21. async destoryRoom(roomSessionId, roomConfigId) {
  22. console.log("destoryRoom", roomSessionId, roomConfigId);
  23. await this.redis.del(roomSessionId);
  24. await this.redis.del(roomConfigId);
  25. this.disconnect();
  26. return Promise.resolve(true);
  27. }
  28. /**
  29. * kickPersion LEADER or assistant 房主或助手
  30. */
  31. async kickPersion(roomId, userId) {
  32. const hasJoin = await this.redis.HVALS(roomId, userId);
  33. if (hasJoin.length > 0) {
  34. await this.redis.hDel(roomId, userId);
  35. }
  36. }
  37. /**
  38. * 创建房间 LEADER or assistant 房主或助手
  39. * @param {*string} roomId
  40. * @param {*string} userId
  41. * @param {*Object} user
  42. */
  43. async buildRoom(roomId, userId, user) {
  44. const hasJoin = await this.redis.HVALS(roomId, userId);
  45. if (hasJoin.length === 0) {
  46. this.room.logger.info("buildRoom", { roomId });
  47. await this.redis.hSet(roomId, userId, JSON.stringify(user));
  48. }
  49. }
  50. /**
  51. * 关闭房间
  52. * @param {*} roomId
  53. */
  54. async removeRoom(roomId) {
  55. this.room.logger.info("removeRoom", { roomId });
  56. await this.redis.del(roomId);
  57. }
  58. // /**
  59. // * 加入房间
  60. // * @param {*} roomId
  61. // * @param {*} userId
  62. // * @param {*} user
  63. // */
  64. // async joinRoom(roomId, userId, user) {
  65. // this.room.logger.info("joinRoom", { roomId });
  66. // try {
  67. // const hasRoom = await this.redis.exists(roomId);
  68. // if (hasRoom) {
  69. // await this.redis.hSet(roomId, userId, JSON.stringify(user));
  70. // } else {
  71. // this.room.logger.info("no room join");
  72. // }
  73. // return Promise.resolve();
  74. // } catch (error) {
  75. // this.room.logger.error(error);
  76. // return Promise.reject(error);
  77. // }
  78. // }
  79. /**
  80. * 加入房间
  81. * @param {*} roomId
  82. * @param {*} userId
  83. * @param {*} user
  84. */
  85. async joinRoom(roomId, userId, user) {
  86. const hasRoom = await this.redis.exists(roomId);
  87. const isJoinRoom = await this.redis.hExists(roomId, userId);
  88. if (hasRoom) {
  89. await this.redis.hSet(roomId, userId, JSON.stringify(user));
  90. } else {
  91. this.room.logger.error("不存在房间", roomId);
  92. }
  93. if (!isJoinRoom) {
  94. this.room.logger.info("加入房间 :", { userId, roomId, user });
  95. this.socket.join(roomId);
  96. const AllRoomUsers = await getAllRoomUsers(roomId);
  97. const roomConfig = await getRoomConfig(roomId);
  98. this.socket.broadcast.emit(EVENT.roomIn, {
  99. user,
  100. roomsPerson: AllRoomUsers,
  101. roomsConfig: roomConfig,
  102. });
  103. } else {
  104. this.room.logger.info(`已加入房间 :`, { userId });
  105. }
  106. }
  107. /**
  108. * 离开房间
  109. * @param {*} roomId
  110. * @param {*} userId
  111. * @param {*} user
  112. */
  113. async leaveRoom(roomId, userId, user) {
  114. try {
  115. await this.redis.hDel(roomId, userId);
  116. this.room.logger.info("离开房间", userId, AllRoomUsers);
  117. const AllRoomUsers = await getAllRoomUsers(roomId);
  118. const roomConfig = await getRoomConfig(roomId);
  119. this.socket.broadcast.to(roomId).emit(EVENT.roomOut, {
  120. user,
  121. roomsPerson: AllRoomUsers,
  122. roomsConfig: roomConfig,
  123. });
  124. this.socket.broadcast.to(roomId).emit(EVENT.someOneLeaveRoom, {
  125. user,
  126. roomsPerson: AllRoomUsers,
  127. });
  128. await this.socket.leave(roomId);
  129. } catch (error) {
  130. console.log("leaveRoom::error", error);
  131. }
  132. }
  133. /**
  134. * 房主关闭房间
  135. * @param {*} clientRoom
  136. * @param {*} userUniqueId
  137. * @param {*} roomUniqueId
  138. */
  139. async closeRoom(roomId, userId, user) {
  140. try {
  141. this.room.logger.info("房主关闭房间", userId);
  142. console.log("isInRoom", this.socket.rooms.has(roomId));
  143. this.socket.broadcast.to(roomId).emit(EVENT.roomClose, { code: 3002, msg: CODEMEG[3002] });
  144. await removeRoomAllUsers(roomId);
  145. this.socket.leave(roomId);
  146. } catch (error) {
  147. this.room.logger.error("RoomAssistant::closeRoom", error);
  148. }
  149. }
  150. /**
  151. * 呼叫房间
  152. * @param {*} roomId
  153. * @param {*} userId
  154. * @param {*} user
  155. */
  156. async startCall(roomId, userId, user) {
  157. try {
  158. if (!this.room.isHoster(user.role)) {
  159. this.room.logger.info("不是房主", JSON.stringify(user));
  160. await this.joinRoom(roomId, userId, user);
  161. } else {
  162. const hasRoom = await this.redis.hVals(roomId);
  163. if (hasRoom.length === 0) {
  164. this.room.logger.info("房主主动创建房间 :", { roomId, userId });
  165. await this.buildRoom(roomId, userId, user);
  166. } else {
  167. this.room.logger.info("房主已存在房间 :", { roomId, userId });
  168. }
  169. }
  170. user.isInRoom = true;
  171. const AllRoomUsers = await getAllRoomUsers(roomId);
  172. const roomConfig = await getRoomConfig(roomId);
  173. await updateRoomUser(roomId, userId, user);
  174. this.room.logger.info("roomId", roomId);
  175. this.room.logger.info("AllRoomUsers", AllRoomUsers.length);
  176. this.socket.emit(EVENT.roomIn, {
  177. user,
  178. roomsPerson: AllRoomUsers,
  179. roomsConfig: roomConfig,
  180. });
  181. this.socket.broadcast.to(roomId).emit(EVENT.someOneInRoom, {
  182. user,
  183. roomsPerson: AllRoomUsers,
  184. });
  185. } catch (error) {
  186. this.room.logger.error("assistant::startCall:", error);
  187. }
  188. }
  189. /**
  190. * 关闭呼叫房间
  191. * @param {*} roomId
  192. * @param {*} userId
  193. * @param {*} user
  194. */
  195. stopCall(roomId, userId, user) {
  196. if (!this.room.isHoster(user.role)) {
  197. this.leaveRoom(roomId, userId, user);
  198. } else {
  199. this.closeRoom(roomId, userId, user);
  200. }
  201. }
  202. // 主动断开
  203. async disconnect() {
  204. const syncId = this.room.syncId;
  205. const roomId = this.room.roomId;
  206. const userId = this.room.userId;
  207. this.socket.leave(syncId);
  208. this.socket.leave(roomId);
  209. await this.redis.del(syncId);
  210. await this.redis.del(userId);
  211. }
  212. // RoomSessionId 房间有效时间
  213. setRoomUnlimit(roomSessionId) {
  214. return this.redis.expire(roomSessionId, -1);
  215. }
  216. setRoomAvailableBySeconds(roomSessionId, seconds) {
  217. return this.redis.expire(roomSessionId, seconds);
  218. }
  219. setRoomAvailableByHours(roomSessionId, hours) {
  220. return this.redis.expire(roomSessionId, 60 * 60 * hours);
  221. }
  222. watchRoomExpired(callback) {
  223. subClient.subscribe("__keyevent@0__:expired", this.watchRoomExpiredFn);
  224. }
  225. async watchRoomExpiredFn(key) {
  226. console.log("key=> ", key);
  227. }
  228. unWatchRoomExpired() {
  229. subClient.unsubscribe("__keyevent@0__:expired", this.watchRoomExpiredFn);
  230. }
  231. }