broadcast-operator.d.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import type { BroadcastFlags, Room, SocketId } from "socket.io-adapter";
  2. import { Handshake } from "./socket";
  3. import type { Adapter } from "socket.io-adapter";
  4. import type { EventParams, EventNames, EventsMap, TypedEventBroadcaster } from "./typed-events";
  5. export declare class BroadcastOperator<EmitEvents extends EventsMap, SocketData> implements TypedEventBroadcaster<EmitEvents> {
  6. private readonly adapter;
  7. private readonly rooms;
  8. private readonly exceptRooms;
  9. private readonly flags;
  10. constructor(adapter: Adapter, rooms?: Set<Room>, exceptRooms?: Set<Room>, flags?: BroadcastFlags);
  11. /**
  12. * Targets a room when emitting.
  13. *
  14. * @param room
  15. * @return a new BroadcastOperator instance
  16. * @public
  17. */
  18. to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  19. /**
  20. * Targets a room when emitting.
  21. *
  22. * @param room
  23. * @return a new BroadcastOperator instance
  24. * @public
  25. */
  26. in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  27. /**
  28. * Excludes a room when emitting.
  29. *
  30. * @param room
  31. * @return a new BroadcastOperator instance
  32. * @public
  33. */
  34. except(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  35. /**
  36. * Sets the compress flag.
  37. *
  38. * @param compress - if `true`, compresses the sending data
  39. * @return a new BroadcastOperator instance
  40. * @public
  41. */
  42. compress(compress: boolean): BroadcastOperator<EmitEvents, SocketData>;
  43. /**
  44. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  45. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  46. * and is in the middle of a request-response cycle).
  47. *
  48. * @return a new BroadcastOperator instance
  49. * @public
  50. */
  51. get volatile(): BroadcastOperator<EmitEvents, SocketData>;
  52. /**
  53. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  54. *
  55. * @return a new BroadcastOperator instance
  56. * @public
  57. */
  58. get local(): BroadcastOperator<EmitEvents, SocketData>;
  59. /**
  60. * Adds a timeout in milliseconds for the next operation
  61. *
  62. * <pre><code>
  63. *
  64. * io.timeout(1000).emit("some-event", (err, responses) => {
  65. * // ...
  66. * });
  67. *
  68. * </pre></code>
  69. *
  70. * @param timeout
  71. */
  72. timeout(timeout: number): BroadcastOperator<EventsMap, unknown>;
  73. /**
  74. * Emits to all clients.
  75. *
  76. * @return Always true
  77. * @public
  78. */
  79. emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
  80. /**
  81. * Gets a list of clients.
  82. *
  83. * @public
  84. */
  85. allSockets(): Promise<Set<SocketId>>;
  86. /**
  87. * Returns the matching socket instances
  88. *
  89. * @public
  90. */
  91. fetchSockets<SocketData = any>(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
  92. /**
  93. * Makes the matching socket instances join the specified rooms
  94. *
  95. * @param room
  96. * @public
  97. */
  98. socketsJoin(room: Room | Room[]): void;
  99. /**
  100. * Makes the matching socket instances leave the specified rooms
  101. *
  102. * @param room
  103. * @public
  104. */
  105. socketsLeave(room: Room | Room[]): void;
  106. /**
  107. * Makes the matching socket instances disconnect
  108. *
  109. * @param close - whether to close the underlying connection
  110. * @public
  111. */
  112. disconnectSockets(close?: boolean): void;
  113. }
  114. /**
  115. * Format of the data when the Socket instance exists on another Socket.IO server
  116. */
  117. interface SocketDetails<SocketData> {
  118. id: SocketId;
  119. handshake: Handshake;
  120. rooms: Room[];
  121. data: SocketData;
  122. }
  123. /**
  124. * Expose of subset of the attributes and methods of the Socket class
  125. */
  126. export declare class RemoteSocket<EmitEvents extends EventsMap, SocketData> implements TypedEventBroadcaster<EmitEvents> {
  127. readonly id: SocketId;
  128. readonly handshake: Handshake;
  129. readonly rooms: Set<Room>;
  130. readonly data: SocketData;
  131. private readonly operator;
  132. constructor(adapter: Adapter, details: SocketDetails<SocketData>);
  133. emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
  134. /**
  135. * Joins a room.
  136. *
  137. * @param {String|Array} room - room or array of rooms
  138. * @public
  139. */
  140. join(room: Room | Room[]): void;
  141. /**
  142. * Leaves a room.
  143. *
  144. * @param {String} room
  145. * @public
  146. */
  147. leave(room: Room): void;
  148. /**
  149. * Disconnects this client.
  150. *
  151. * @param {Boolean} close - if `true`, closes the underlying connection
  152. * @return {Socket} self
  153. *
  154. * @public
  155. */
  156. disconnect(close?: boolean): this;
  157. }
  158. export {};