in-memory-adapter.d.ts 5.4 KB

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