index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Adapter = void 0;
  4. const events_1 = require("events");
  5. class Adapter extends events_1.EventEmitter {
  6. /**
  7. * In-memory adapter constructor.
  8. *
  9. * @param {Namespace} nsp
  10. */
  11. constructor(nsp) {
  12. super();
  13. this.nsp = nsp;
  14. this.rooms = new Map();
  15. this.sids = new Map();
  16. this.encoder = nsp.server.encoder;
  17. }
  18. /**
  19. * To be overridden
  20. */
  21. init() { }
  22. /**
  23. * To be overridden
  24. */
  25. close() { }
  26. /**
  27. * Adds a socket to a list of room.
  28. *
  29. * @param {SocketId} id the socket id
  30. * @param {Set<Room>} rooms a set of rooms
  31. * @public
  32. */
  33. addAll(id, rooms) {
  34. for (const room of rooms) {
  35. if (!this.sids.has(id)) {
  36. this.sids.set(id, new Set());
  37. }
  38. this.sids.get(id).add(room);
  39. if (!this.rooms.has(room)) {
  40. this.rooms.set(room, new Set());
  41. }
  42. this.rooms.get(room).add(id);
  43. }
  44. }
  45. /**
  46. * Removes a socket from a room.
  47. *
  48. * @param {SocketId} id the socket id
  49. * @param {Room} room the room name
  50. */
  51. del(id, room) {
  52. if (this.sids.has(id)) {
  53. this.sids.get(id).delete(room);
  54. }
  55. if (this.rooms.has(room)) {
  56. this.rooms.get(room).delete(id);
  57. if (this.rooms.get(room).size === 0)
  58. this.rooms.delete(room);
  59. }
  60. }
  61. /**
  62. * Removes a socket from all rooms it's joined.
  63. *
  64. * @param {SocketId} id the socket id
  65. */
  66. delAll(id) {
  67. if (!this.sids.has(id)) {
  68. return;
  69. }
  70. for (const room of this.sids.get(id)) {
  71. if (this.rooms.has(room)) {
  72. this.rooms.get(room).delete(id);
  73. if (this.rooms.get(room).size === 0)
  74. this.rooms.delete(room);
  75. }
  76. }
  77. this.sids.delete(id);
  78. }
  79. /**
  80. * Broadcasts a packet.
  81. *
  82. * Options:
  83. * - `flags` {Object} flags for this packet
  84. * - `except` {Array} sids that should be excluded
  85. * - `rooms` {Array} list of rooms to broadcast to
  86. *
  87. * @param {Object} packet the packet object
  88. * @param {Object} opts the options
  89. * @public
  90. */
  91. broadcast(packet, opts) {
  92. const rooms = opts.rooms;
  93. const except = opts.except || new Set();
  94. const flags = opts.flags || {};
  95. const packetOpts = {
  96. preEncoded: true,
  97. volatile: flags.volatile,
  98. compress: flags.compress
  99. };
  100. const ids = new Set();
  101. packet.nsp = this.nsp.name;
  102. const encodedPackets = this.encoder.encode(packet);
  103. if (rooms.size) {
  104. for (const room of rooms) {
  105. if (!this.rooms.has(room))
  106. continue;
  107. for (const id of this.rooms.get(room)) {
  108. if (ids.has(id) || except.has(id))
  109. continue;
  110. const socket = this.nsp.sockets.get(id);
  111. if (socket) {
  112. socket.packet(encodedPackets, packetOpts);
  113. ids.add(id);
  114. }
  115. }
  116. }
  117. }
  118. else {
  119. for (const [id] of this.sids) {
  120. if (except.has(id))
  121. continue;
  122. const socket = this.nsp.sockets.get(id);
  123. if (socket)
  124. socket.packet(encodedPackets, packetOpts);
  125. }
  126. }
  127. }
  128. /**
  129. * Gets a list of sockets by sid.
  130. *
  131. * @param {Set<Room>} rooms the explicit set of rooms to check.
  132. */
  133. sockets(rooms) {
  134. const sids = new Set();
  135. if (rooms.size) {
  136. for (const room of rooms) {
  137. if (!this.rooms.has(room))
  138. continue;
  139. for (const id of this.rooms.get(room)) {
  140. if (this.nsp.sockets.has(id)) {
  141. sids.add(id);
  142. }
  143. }
  144. }
  145. }
  146. else {
  147. for (const [id] of this.sids) {
  148. if (this.nsp.sockets.has(id))
  149. sids.add(id);
  150. }
  151. }
  152. return Promise.resolve(sids);
  153. }
  154. /**
  155. * Gets the list of rooms a given socket has joined.
  156. *
  157. * @param {SocketId} id the socket id
  158. */
  159. socketRooms(id) {
  160. return this.sids.get(id);
  161. }
  162. }
  163. exports.Adapter = Adapter;