Node-Red configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

namespace.d.ts 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 extends EventsMap, EmitEvents extends EventsMap, ServerSideEvents extends EventsMap, 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. /**
  19. * A Namespace is a communication channel that allows you to split the logic of your application over a single shared
  20. * connection.
  21. *
  22. * Each namespace has its own:
  23. *
  24. * - event handlers
  25. *
  26. * ```
  27. * io.of("/orders").on("connection", (socket) => {
  28. * socket.on("order:list", () => {});
  29. * socket.on("order:create", () => {});
  30. * });
  31. *
  32. * io.of("/users").on("connection", (socket) => {
  33. * socket.on("user:list", () => {});
  34. * });
  35. * ```
  36. *
  37. * - rooms
  38. *
  39. * ```
  40. * const orderNamespace = io.of("/orders");
  41. *
  42. * orderNamespace.on("connection", (socket) => {
  43. * socket.join("room1");
  44. * orderNamespace.to("room1").emit("hello");
  45. * });
  46. *
  47. * const userNamespace = io.of("/users");
  48. *
  49. * userNamespace.on("connection", (socket) => {
  50. * socket.join("room1"); // distinct from the room in the "orders" namespace
  51. * userNamespace.to("room1").emit("holà");
  52. * });
  53. * ```
  54. *
  55. * - middlewares
  56. *
  57. * ```
  58. * const orderNamespace = io.of("/orders");
  59. *
  60. * orderNamespace.use((socket, next) => {
  61. * // ensure the socket has access to the "orders" namespace
  62. * });
  63. *
  64. * const userNamespace = io.of("/users");
  65. *
  66. * userNamespace.use((socket, next) => {
  67. * // ensure the socket has access to the "users" namespace
  68. * });
  69. * ```
  70. */
  71. 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>> {
  72. readonly name: string;
  73. readonly sockets: Map<SocketId, Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>>;
  74. adapter: Adapter;
  75. /** @private */
  76. readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
  77. /** @private */
  78. _fns: Array<(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void>;
  79. /** @private */
  80. _ids: number;
  81. /**
  82. * Namespace constructor.
  83. *
  84. * @param server instance
  85. * @param name
  86. */
  87. constructor(server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, name: string);
  88. /**
  89. * Initializes the `Adapter` for this nsp.
  90. * Run upon changing adapter by `Server#adapter`
  91. * in addition to the constructor.
  92. *
  93. * @private
  94. */
  95. _initAdapter(): void;
  96. /**
  97. * Registers a middleware, which is a function that gets executed for every incoming {@link Socket}.
  98. *
  99. * @example
  100. * const myNamespace = io.of("/my-namespace");
  101. *
  102. * myNamespace.use((socket, next) => {
  103. * // ...
  104. * next();
  105. * });
  106. *
  107. * @param fn - the middleware function
  108. */
  109. use(fn: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, next: (err?: ExtendedError) => void) => void): this;
  110. /**
  111. * Executes the middleware for an incoming client.
  112. *
  113. * @param socket - the socket that will get added
  114. * @param fn - last fn call in the middleware
  115. * @private
  116. */
  117. private run;
  118. /**
  119. * Targets a room when emitting.
  120. *
  121. * @example
  122. * const myNamespace = io.of("/my-namespace");
  123. *
  124. * // the “foo” event will be broadcast to all connected clients in the “room-101” room
  125. * myNamespace.to("room-101").emit("foo", "bar");
  126. *
  127. * // with an array of rooms (a client will be notified at most once)
  128. * myNamespace.to(["room-101", "room-102"]).emit("foo", "bar");
  129. *
  130. * // with multiple chained calls
  131. * myNamespace.to("room-101").to("room-102").emit("foo", "bar");
  132. *
  133. * @param room - a room, or an array of rooms
  134. * @return a new {@link BroadcastOperator} instance for chaining
  135. */
  136. to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  137. /**
  138. * Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
  139. *
  140. * @example
  141. * const myNamespace = io.of("/my-namespace");
  142. *
  143. * // disconnect all clients in the "room-101" room
  144. * myNamespace.in("room-101").disconnectSockets();
  145. *
  146. * @param room - a room, or an array of rooms
  147. * @return a new {@link BroadcastOperator} instance for chaining
  148. */
  149. in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  150. /**
  151. * Excludes a room when emitting.
  152. *
  153. * @example
  154. * const myNamespace = io.of("/my-namespace");
  155. *
  156. * // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
  157. * myNamespace.except("room-101").emit("foo", "bar");
  158. *
  159. * // with an array of rooms
  160. * myNamespace.except(["room-101", "room-102"]).emit("foo", "bar");
  161. *
  162. * // with multiple chained calls
  163. * myNamespace.except("room-101").except("room-102").emit("foo", "bar");
  164. *
  165. * @param room - a room, or an array of rooms
  166. * @return a new {@link BroadcastOperator} instance for chaining
  167. */
  168. except(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  169. /**
  170. * Adds a new client.
  171. *
  172. * @return {Socket}
  173. * @private
  174. */
  175. _add(client: Client<ListenEvents, EmitEvents, ServerSideEvents>, query: any, fn?: () => void): Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;
  176. /**
  177. * Removes a client. Called by each `Socket`.
  178. *
  179. * @private
  180. */
  181. _remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>): void;
  182. /**
  183. * Emits to all connected clients.
  184. *
  185. * @example
  186. * const myNamespace = io.of("/my-namespace");
  187. *
  188. * myNamespace.emit("hello", "world");
  189. *
  190. * // all serializable datastructures are supported (no need to call JSON.stringify)
  191. * myNamespace.emit("hello", 1, "2", { 3: ["4"], 5: Uint8Array.from([6]) });
  192. *
  193. * // with an acknowledgement from the clients
  194. * myNamespace.timeout(1000).emit("some-event", (err, responses) => {
  195. * if (err) {
  196. * // some clients did not acknowledge the event in the given delay
  197. * } else {
  198. * console.log(responses); // one response per client
  199. * }
  200. * });
  201. *
  202. * @return Always true
  203. */
  204. emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
  205. /**
  206. * Sends a `message` event to all clients.
  207. *
  208. * This method mimics the WebSocket.send() method.
  209. *
  210. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
  211. *
  212. * @example
  213. * const myNamespace = io.of("/my-namespace");
  214. *
  215. * myNamespace.send("hello");
  216. *
  217. * // this is equivalent to
  218. * myNamespace.emit("message", "hello");
  219. *
  220. * @return self
  221. */
  222. send(...args: EventParams<EmitEvents, "message">): this;
  223. /**
  224. * Sends a `message` event to all clients. Sends a `message` event. Alias of {@link send}.
  225. *
  226. * @return self
  227. */
  228. write(...args: EventParams<EmitEvents, "message">): this;
  229. /**
  230. * Sends a message to the other Socket.IO servers of the cluster.
  231. *
  232. * @example
  233. * const myNamespace = io.of("/my-namespace");
  234. *
  235. * myNamespace.serverSideEmit("hello", "world");
  236. *
  237. * myNamespace.on("hello", (arg1) => {
  238. * console.log(arg1); // prints "world"
  239. * });
  240. *
  241. * // acknowledgements (without binary content) are supported too:
  242. * myNamespace.serverSideEmit("ping", (err, responses) => {
  243. * if (err) {
  244. * // some clients did not acknowledge the event in the given delay
  245. * } else {
  246. * console.log(responses); // one response per client
  247. * }
  248. * });
  249. *
  250. * myNamespace.on("ping", (cb) => {
  251. * cb("pong");
  252. * });
  253. *
  254. * @param ev - the event name
  255. * @param args - an array of arguments, which may include an acknowledgement callback at the end
  256. */
  257. serverSideEmit<Ev extends EventNames<ServerSideEvents>>(ev: Ev, ...args: EventParams<ServerSideEvents, Ev>): boolean;
  258. /**
  259. * Called when a packet is received from another Socket.IO server
  260. *
  261. * @param args - an array of arguments, which may include an acknowledgement callback at the end
  262. *
  263. * @private
  264. */
  265. _onServerSideEmit(args: [string, ...any[]]): void;
  266. /**
  267. * Gets a list of clients.
  268. *
  269. * @deprecated this method will be removed in the next major release, please use {@link Namespace#serverSideEmit} or
  270. * {@link Namespace#fetchSockets} instead.
  271. */
  272. allSockets(): Promise<Set<SocketId>>;
  273. /**
  274. * Sets the compress flag.
  275. *
  276. * @example
  277. * const myNamespace = io.of("/my-namespace");
  278. *
  279. * myNamespace.compress(false).emit("hello");
  280. *
  281. * @param compress - if `true`, compresses the sending data
  282. * @return self
  283. */
  284. compress(compress: boolean): BroadcastOperator<EmitEvents, SocketData>;
  285. /**
  286. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  287. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  288. * and is in the middle of a request-response cycle).
  289. *
  290. * @example
  291. * const myNamespace = io.of("/my-namespace");
  292. *
  293. * myNamespace.volatile.emit("hello"); // the clients may or may not receive it
  294. *
  295. * @return self
  296. */
  297. get volatile(): BroadcastOperator<EmitEvents, SocketData>;
  298. /**
  299. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  300. *
  301. * @example
  302. * const myNamespace = io.of("/my-namespace");
  303. *
  304. * // the “foo” event will be broadcast to all connected clients on this node
  305. * myNamespace.local.emit("foo", "bar");
  306. *
  307. * @return a new {@link BroadcastOperator} instance for chaining
  308. */
  309. get local(): BroadcastOperator<EmitEvents, SocketData>;
  310. /**
  311. * Adds a timeout in milliseconds for the next operation.
  312. *
  313. * @example
  314. * const myNamespace = io.of("/my-namespace");
  315. *
  316. * myNamespace.timeout(1000).emit("some-event", (err, responses) => {
  317. * if (err) {
  318. * // some clients did not acknowledge the event in the given delay
  319. * } else {
  320. * console.log(responses); // one response per client
  321. * }
  322. * });
  323. *
  324. * @param timeout
  325. */
  326. timeout(timeout: number): BroadcastOperator<EmitEvents, SocketData>;
  327. /**
  328. * Returns the matching socket instances.
  329. *
  330. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  331. *
  332. * @example
  333. * const myNamespace = io.of("/my-namespace");
  334. *
  335. * // return all Socket instances
  336. * const sockets = await myNamespace.fetchSockets();
  337. *
  338. * // return all Socket instances in the "room1" room
  339. * const sockets = await myNamespace.in("room1").fetchSockets();
  340. *
  341. * for (const socket of sockets) {
  342. * console.log(socket.id);
  343. * console.log(socket.handshake);
  344. * console.log(socket.rooms);
  345. * console.log(socket.data);
  346. *
  347. * socket.emit("hello");
  348. * socket.join("room1");
  349. * socket.leave("room2");
  350. * socket.disconnect();
  351. * }
  352. */
  353. fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]>;
  354. /**
  355. * Makes the matching socket instances join the specified rooms.
  356. *
  357. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  358. *
  359. * @example
  360. * const myNamespace = io.of("/my-namespace");
  361. *
  362. * // make all socket instances join the "room1" room
  363. * myNamespace.socketsJoin("room1");
  364. *
  365. * // make all socket instances in the "room1" room join the "room2" and "room3" rooms
  366. * myNamespace.in("room1").socketsJoin(["room2", "room3"]);
  367. *
  368. * @param room - a room, or an array of rooms
  369. */
  370. socketsJoin(room: Room | Room[]): void;
  371. /**
  372. * Makes the matching socket instances leave the specified rooms.
  373. *
  374. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  375. *
  376. * @example
  377. * const myNamespace = io.of("/my-namespace");
  378. *
  379. * // make all socket instances leave the "room1" room
  380. * myNamespace.socketsLeave("room1");
  381. *
  382. * // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
  383. * myNamespace.in("room1").socketsLeave(["room2", "room3"]);
  384. *
  385. * @param room - a room, or an array of rooms
  386. */
  387. socketsLeave(room: Room | Room[]): void;
  388. /**
  389. * Makes the matching socket instances disconnect.
  390. *
  391. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  392. *
  393. * @example
  394. * const myNamespace = io.of("/my-namespace");
  395. *
  396. * // make all socket instances disconnect (the connections might be kept alive for other namespaces)
  397. * myNamespace.disconnectSockets();
  398. *
  399. * // make all socket instances in the "room1" room disconnect and close the underlying connections
  400. * myNamespace.in("room1").disconnectSockets(true);
  401. *
  402. * @param close - whether to close the underlying connection
  403. */
  404. disconnectSockets(close?: boolean): void;
  405. }