assistant.js 7.2 KB

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