in-memory-adapter.d.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { EventEmitter } from "events";
  2. /**
  3. * A public ID, sent by the server at the beginning of the Socket.IO session and which can be used for private messaging
  4. */
  5. export type SocketId = string;
  6. /**
  7. * A private ID, sent by the server at the beginning of the Socket.IO session and used for connection state recovery
  8. * upon reconnection
  9. */
  10. export type PrivateSessionId = string;
  11. export type Room = string;
  12. export interface BroadcastFlags {
  13. volatile?: boolean;
  14. compress?: boolean;
  15. local?: boolean;
  16. broadcast?: boolean;
  17. binary?: boolean;
  18. timeout?: number;
  19. }
  20. export interface BroadcastOptions {
  21. rooms: Set<Room>;
  22. except?: Set<Room>;
  23. flags?: BroadcastFlags;
  24. }
  25. interface SessionToPersist {
  26. sid: SocketId;
  27. pid: PrivateSessionId;
  28. rooms: Room[];
  29. data: unknown;
  30. }
  31. export type Session = SessionToPersist & {
  32. missedPackets: unknown[][];
  33. };
  34. export declare class Adapter extends EventEmitter {
  35. readonly nsp: any;
  36. rooms: Map<Room, Set<SocketId>>;
  37. sids: Map<SocketId, Set<Room>>;
  38. private readonly encoder;
  39. /**
  40. * In-memory adapter constructor.
  41. *
  42. * @param nsp
  43. */
  44. constructor(nsp: any);
  45. /**
  46. * To be overridden
  47. */
  48. init(): Promise<void> | void;
  49. /**
  50. * To be overridden
  51. */
  52. close(): Promise<void> | void;
  53. /**
  54. * Returns the number of Socket.IO servers in the cluster
  55. *
  56. * @public
  57. */
  58. serverCount(): Promise<number>;
  59. /**
  60. * Adds a socket to a list of room.
  61. *
  62. * @param {SocketId} id the socket id
  63. * @param {Set<Room>} rooms a set of rooms
  64. * @public
  65. */
  66. addAll(id: SocketId, rooms: Set<Room>): Promise<void> | void;
  67. /**
  68. * Removes a socket from a room.
  69. *
  70. * @param {SocketId} id the socket id
  71. * @param {Room} room the room name
  72. */
  73. del(id: SocketId, room: Room): Promise<void> | void;
  74. private _del;
  75. /**
  76. * Removes a socket from all rooms it's joined.
  77. *
  78. * @param {SocketId} id the socket id
  79. */
  80. delAll(id: SocketId): void;
  81. /**
  82. * Broadcasts a packet.
  83. *
  84. * Options:
  85. * - `flags` {Object} flags for this packet
  86. * - `except` {Array} sids that should be excluded
  87. * - `rooms` {Array} list of rooms to broadcast to
  88. *
  89. * @param {Object} packet the packet object
  90. * @param {Object} opts the options
  91. * @public
  92. */
  93. broadcast(packet: any, opts: BroadcastOptions): void;
  94. /**
  95. * Broadcasts a packet and expects multiple acknowledgements.
  96. *
  97. * Options:
  98. * - `flags` {Object} flags for this packet
  99. * - `except` {Array} sids that should be excluded
  100. * - `rooms` {Array} list of rooms to broadcast to
  101. *
  102. * @param {Object} packet the packet object
  103. * @param {Object} opts the options
  104. * @param clientCountCallback - the number of clients that received the packet
  105. * @param ack - the callback that will be called for each client response
  106. *
  107. * @public
  108. */
  109. broadcastWithAck(packet: any, opts: BroadcastOptions, clientCountCallback: (clientCount: number) => void, ack: (...args: any[]) => void): void;
  110. private _encode;
  111. /**
  112. * Gets a list of sockets by sid.
  113. *
  114. * @param {Set<Room>} rooms the explicit set of rooms to check.
  115. */
  116. sockets(rooms: Set<Room>): Promise<Set<SocketId>>;
  117. /**
  118. * Gets the list of rooms a given socket has joined.
  119. *
  120. * @param {SocketId} id the socket id
  121. */
  122. socketRooms(id: SocketId): Set<Room> | undefined;
  123. /**
  124. * Returns the matching socket instances
  125. *
  126. * @param opts - the filters to apply
  127. */
  128. fetchSockets(opts: BroadcastOptions): Promise<any[]>;
  129. /**
  130. * Makes the matching socket instances join the specified rooms
  131. *
  132. * @param opts - the filters to apply
  133. * @param rooms - the rooms to join
  134. */
  135. addSockets(opts: BroadcastOptions, rooms: Room[]): void;
  136. /**
  137. * Makes the matching socket instances leave the specified rooms
  138. *
  139. * @param opts - the filters to apply
  140. * @param rooms - the rooms to leave
  141. */
  142. delSockets(opts: BroadcastOptions, rooms: Room[]): void;
  143. /**
  144. * Makes the matching socket instances disconnect
  145. *
  146. * @param opts - the filters to apply
  147. * @param close - whether to close the underlying connection
  148. */
  149. disconnectSockets(opts: BroadcastOptions, close: boolean): void;
  150. private apply;
  151. private computeExceptSids;
  152. /**
  153. * Send a packet to the other Socket.IO servers in the cluster
  154. * @param packet - an array of arguments, which may include an acknowledgement callback at the end
  155. */
  156. serverSideEmit(packet: any[]): void;
  157. /**
  158. * Save the client session in order to restore it upon reconnection.
  159. */
  160. persistSession(session: SessionToPersist): void;
  161. /**
  162. * Restore the session and find the packets that were missed by the client.
  163. * @param pid
  164. * @param offset
  165. */
  166. restoreSession(pid: PrivateSessionId, offset: string): Promise<Session>;
  167. }
  168. export declare class SessionAwareAdapter extends Adapter {
  169. readonly nsp: any;
  170. private readonly maxDisconnectionDuration;
  171. private sessions;
  172. private packets;
  173. constructor(nsp: any);
  174. persistSession(session: SessionToPersist): void;
  175. restoreSession(pid: PrivateSessionId, offset: string): Promise<Session>;
  176. broadcast(packet: any, opts: BroadcastOptions): void;
  177. }
  178. export {};