namespace.d.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { Socket } from "./socket";
  2. import type { Server } from "./index";
  3. import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
  4. import type { Client } from "./client";
  5. import type { Adapter, Room, SocketId } from "socket.io-adapter";
  6. import { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
  7. export interface ExtendedError extends Error {
  8. data?: any;
  9. }
  10. export interface NamespaceReservedEventsMap<ListenEvents extends EventsMap, EmitEvents extends EventsMap, ServerSideEvents extends EventsMap, SocketData> {
  11. connect: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
  12. connection: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
  13. }
  14. export interface ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData> extends NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
  15. new_namespace: (namespace: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>) => void;
  16. }
  17. export declare const RESERVED_EVENTS: ReadonlySet<string | Symbol>;
  18. export declare class Namespace<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, SocketData = any> extends StrictEventEmitter<ServerSideEvents, EmitEvents, NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents, SocketData>> {
  19. readonly name: string;
  20. readonly sockets: Map<SocketId, Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>;
  21. adapter: Adapter;
  22. /** @private */
  23. readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
  24. /** @private */
  25. _fns: Array<(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void>;
  26. /** @private */
  27. _ids: number;
  28. /**
  29. * Namespace constructor.
  30. *
  31. * @param server instance
  32. * @param name
  33. */
  34. constructor(server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, name: string);
  35. /**
  36. * Initializes the `Adapter` for this nsp.
  37. * Run upon changing adapter by `Server#adapter`
  38. * in addition to the constructor.
  39. *
  40. * @private
  41. */
  42. _initAdapter(): void;
  43. /**
  44. * Sets up namespace middleware.
  45. *
  46. * @return self
  47. * @public
  48. */
  49. use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void): this;
  50. /**
  51. * Executes the middleware for an incoming client.
  52. *
  53. * @param socket - the socket that will get added
  54. * @param fn - last fn call in the middleware
  55. * @private
  56. */
  57. private run;
  58. /**
  59. * Targets a room when emitting.
  60. *
  61. * @param room
  62. * @return self
  63. * @public
  64. */
  65. to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  66. /**
  67. * Targets a room when emitting.
  68. *
  69. * @param room
  70. * @return self
  71. * @public
  72. */
  73. in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  74. /**
  75. * Excludes a room when emitting.
  76. *
  77. * @param room
  78. * @return self
  79. * @public
  80. */
  81. except(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  82. /**
  83. * Adds a new client.
  84. *
  85. * @return {Socket}
  86. * @private
  87. */
  88. _add(client: Client<ListenEvents, EmitEvents, ServerSideEvents>, query: any, fn?: () => void): Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
  89. /**
  90. * Removes a client. Called by each `Socket`.
  91. *
  92. * @private
  93. */
  94. _remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>): void;
  95. /**
  96. * Emits to all clients.
  97. *
  98. * @return Always true
  99. * @public
  100. */
  101. emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
  102. /**
  103. * Sends a `message` event to all clients.
  104. *
  105. * @return self
  106. * @public
  107. */
  108. send(...args: EventParams<EmitEvents, "message">): this;
  109. /**
  110. * Sends a `message` event to all clients.
  111. *
  112. * @return self
  113. * @public
  114. */
  115. write(...args: EventParams<EmitEvents, "message">): this;
  116. /**
  117. * Emit a packet to other Socket.IO servers
  118. *
  119. * @param ev - the event name
  120. * @param args - an array of arguments, which may include an acknowledgement callback at the end
  121. * @public
  122. */
  123. serverSideEmit<Ev extends EventNames<ServerSideEvents>>(ev: Ev, ...args: EventParams<ServerSideEvents, Ev>): boolean;
  124. /**
  125. * Called when a packet is received from another Socket.IO server
  126. *
  127. * @param args - an array of arguments, which may include an acknowledgement callback at the end
  128. *
  129. * @private
  130. */
  131. _onServerSideEmit(args: [string, ...any[]]): void;
  132. /**
  133. * Gets a list of clients.
  134. *
  135. * @return self
  136. * @public
  137. */
  138. allSockets(): Promise<Set<SocketId>>;
  139. /**
  140. * Sets the compress flag.
  141. *
  142. * @param compress - if `true`, compresses the sending data
  143. * @return self
  144. * @public
  145. */
  146. compress(compress: boolean): BroadcastOperator<EmitEvents, SocketData>;
  147. /**
  148. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  149. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  150. * and is in the middle of a request-response cycle).
  151. *
  152. * @return self
  153. * @public
  154. */
  155. get volatile(): BroadcastOperator<EmitEvents, SocketData>;
  156. /**
  157. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  158. *
  159. * @return self
  160. * @public
  161. */
  162. get local(): BroadcastOperator<EmitEvents, SocketData>;
  163. /**
  164. * Adds a timeout in milliseconds for the next operation
  165. *
  166. * <pre><code>
  167. *
  168. * io.timeout(1000).emit("some-event", (err, responses) => {
  169. * // ...
  170. * });
  171. *
  172. * </pre></code>
  173. *
  174. * @param timeout
  175. */
  176. timeout(timeout: number): BroadcastOperator<EventsMap, unknown>;
  177. /**
  178. * Returns the matching socket instances
  179. *
  180. * @public
  181. */
  182. fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
  183. /**
  184. * Makes the matching socket instances join the specified rooms
  185. *
  186. * @param room
  187. * @public
  188. */
  189. socketsJoin(room: Room | Room[]): void;
  190. /**
  191. * Makes the matching socket instances leave the specified rooms
  192. *
  193. * @param room
  194. * @public
  195. */
  196. socketsLeave(room: Room | Room[]): void;
  197. /**
  198. * Makes the matching socket instances disconnect
  199. *
  200. * @param close - whether to close the underlying connection
  201. * @public
  202. */
  203. disconnectSockets(close?: boolean): void;
  204. }