namespace.d.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /// <reference types="node" />
  2. import { Socket } from "./socket";
  3. import { Server } from "./index";
  4. import { Client } from "./client";
  5. import { EventEmitter } from "events";
  6. import { Adapter, Room, SocketId } from "socket.io-adapter";
  7. export interface ExtendedError extends Error {
  8. data?: any;
  9. }
  10. export declare class Namespace extends EventEmitter {
  11. readonly name: string;
  12. readonly sockets: Map<SocketId, Socket>;
  13. adapter: Adapter;
  14. /** @private */
  15. readonly server: Server;
  16. /** @private */
  17. _fns: Array<(socket: Socket, next: (err: ExtendedError) => void) => void>;
  18. /** @private */
  19. _rooms: Set<Room>;
  20. /** @private */
  21. _flags: any;
  22. /** @private */
  23. _ids: number;
  24. /**
  25. * Namespace constructor.
  26. *
  27. * @param {Server} server instance
  28. * @param {string} name
  29. */
  30. constructor(server: Server, name: string);
  31. /**
  32. * Initializes the `Adapter` for this nsp.
  33. * Run upon changing adapter by `Server#adapter`
  34. * in addition to the constructor.
  35. *
  36. * @private
  37. */
  38. _initAdapter(): void;
  39. /**
  40. * Sets up namespace middleware.
  41. *
  42. * @return {Namespace} self
  43. * @public
  44. */
  45. use(fn: (socket: Socket, next: (err?: ExtendedError) => void) => void): Namespace;
  46. /**
  47. * Executes the middleware for an incoming client.
  48. *
  49. * @param {Socket} socket - the socket that will get added
  50. * @param {Function} fn - last fn call in the middleware
  51. * @private
  52. */
  53. private run;
  54. /**
  55. * Targets a room when emitting.
  56. *
  57. * @param {String} name
  58. * @return {Namespace} self
  59. * @public
  60. */
  61. to(name: Room): Namespace;
  62. /**
  63. * Targets a room when emitting.
  64. *
  65. * @param {String} name
  66. * @return {Namespace} self
  67. * @public
  68. */
  69. in(name: Room): Namespace;
  70. /**
  71. * Adds a new client.
  72. *
  73. * @return {Socket}
  74. * @private
  75. */
  76. _add(client: Client, query: any, fn?: () => void): Socket;
  77. /**
  78. * Removes a client. Called by each `Socket`.
  79. *
  80. * @private
  81. */
  82. _remove(socket: Socket): void;
  83. /**
  84. * Emits to all clients.
  85. *
  86. * @return {Boolean} Always true
  87. * @public
  88. */
  89. emit(ev: string, ...args: any[]): boolean;
  90. /**
  91. * Sends a `message` event to all clients.
  92. *
  93. * @return {Namespace} self
  94. * @public
  95. */
  96. send(...args: any[]): Namespace;
  97. /**
  98. * Sends a `message` event to all clients.
  99. *
  100. * @return {Namespace} self
  101. * @public
  102. */
  103. write(...args: any[]): Namespace;
  104. /**
  105. * Gets a list of clients.
  106. *
  107. * @return {Namespace} self
  108. * @public
  109. */
  110. allSockets(): Promise<Set<SocketId>>;
  111. /**
  112. * Sets the compress flag.
  113. *
  114. * @param {Boolean} compress - if `true`, compresses the sending data
  115. * @return {Namespace} self
  116. * @public
  117. */
  118. compress(compress: boolean): Namespace;
  119. /**
  120. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  121. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  122. * and is in the middle of a request-response cycle).
  123. *
  124. * @return {Namespace} self
  125. * @public
  126. */
  127. get volatile(): Namespace;
  128. /**
  129. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  130. *
  131. * @return {Namespace} self
  132. * @public
  133. */
  134. get local(): Namespace;
  135. }