Node-Red configuration
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. import { Packet } from "socket.io-parser";
  4. import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
  5. import type { Client } from "./client";
  6. import type { Namespace } from "./namespace";
  7. import type { IncomingMessage, IncomingHttpHeaders } from "http";
  8. import type { Room, SocketId } from "socket.io-adapter";
  9. import type { ParsedUrlQuery } from "querystring";
  10. import { BroadcastOperator } from "./broadcast-operator";
  11. export declare type DisconnectReason = "transport error" | "transport close" | "forced close" | "ping timeout" | "parse error" | "server shutting down" | "forced server close" | "client namespace disconnect" | "server namespace disconnect";
  12. export interface SocketReservedEventsMap {
  13. disconnect: (reason: DisconnectReason) => void;
  14. disconnecting: (reason: DisconnectReason) => void;
  15. error: (err: Error) => void;
  16. }
  17. export interface EventEmitterReservedEventsMap {
  18. newListener: (eventName: string | Symbol, listener: (...args: any[]) => void) => void;
  19. removeListener: (eventName: string | Symbol, listener: (...args: any[]) => void) => void;
  20. }
  21. export declare const RESERVED_EVENTS: ReadonlySet<string | Symbol>;
  22. /**
  23. * The handshake details
  24. */
  25. export interface Handshake {
  26. /**
  27. * The headers sent as part of the handshake
  28. */
  29. headers: IncomingHttpHeaders;
  30. /**
  31. * The date of creation (as string)
  32. */
  33. time: string;
  34. /**
  35. * The ip of the client
  36. */
  37. address: string;
  38. /**
  39. * Whether the connection is cross-domain
  40. */
  41. xdomain: boolean;
  42. /**
  43. * Whether the connection is secure
  44. */
  45. secure: boolean;
  46. /**
  47. * The date of creation (as unix timestamp)
  48. */
  49. issued: number;
  50. /**
  51. * The request URL string
  52. */
  53. url: string;
  54. /**
  55. * The query object
  56. */
  57. query: ParsedUrlQuery;
  58. /**
  59. * The auth object
  60. */
  61. auth: {
  62. [key: string]: any;
  63. };
  64. }
  65. /**
  66. * `[eventName, ...args]`
  67. */
  68. export declare type Event = [string, ...any[]];
  69. /**
  70. * This is the main object for interacting with a client.
  71. *
  72. * A Socket belongs to a given {@link Namespace} and uses an underlying {@link Client} to communicate.
  73. *
  74. * Within each {@link Namespace}, you can also define arbitrary channels (called "rooms") that the {@link Socket} can
  75. * join and leave. That provides a convenient way to broadcast to a group of socket instances.
  76. *
  77. * @example
  78. * io.on("connection", (socket) => {
  79. * console.log(`socket ${socket.id} connected`);
  80. *
  81. * // send an event to the client
  82. * socket.emit("foo", "bar");
  83. *
  84. * socket.on("foobar", () => {
  85. * // an event was received from the client
  86. * });
  87. *
  88. * // join the room named "room1"
  89. * socket.join("room1");
  90. *
  91. * // broadcast to everyone in the room named "room1"
  92. * io.to("room1").emit("hello");
  93. *
  94. * // upon disconnection
  95. * socket.on("disconnect", (reason) => {
  96. * console.log(`socket ${socket.id} disconnected due to ${reason}`);
  97. * });
  98. * });
  99. */
  100. export declare class Socket<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, SocketData = any> extends StrictEventEmitter<ListenEvents, EmitEvents, SocketReservedEventsMap> {
  101. readonly nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
  102. readonly client: Client<ListenEvents, EmitEvents, ServerSideEvents>;
  103. /**
  104. * An unique identifier for the session.
  105. */
  106. readonly id: SocketId;
  107. /**
  108. * The handshake details.
  109. */
  110. readonly handshake: Handshake;
  111. /**
  112. * Additional information that can be attached to the Socket instance and which will be used in the
  113. * {@link Server.fetchSockets()} method.
  114. */
  115. data: Partial<SocketData>;
  116. /**
  117. * Whether the socket is currently connected or not.
  118. *
  119. * @example
  120. * io.use((socket, next) => {
  121. * console.log(socket.connected); // false
  122. * next();
  123. * });
  124. *
  125. * io.on("connection", (socket) => {
  126. * console.log(socket.connected); // true
  127. * });
  128. */
  129. connected: boolean;
  130. private readonly server;
  131. private readonly adapter;
  132. private acks;
  133. private fns;
  134. private flags;
  135. private _anyListeners?;
  136. private _anyOutgoingListeners?;
  137. /**
  138. * Interface to a `Client` for a given `Namespace`.
  139. *
  140. * @param {Namespace} nsp
  141. * @param {Client} client
  142. * @param {Object} auth
  143. * @package
  144. */
  145. constructor(nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>, client: Client<ListenEvents, EmitEvents, ServerSideEvents>, auth: object);
  146. /**
  147. * Builds the `handshake` BC object
  148. *
  149. * @private
  150. */
  151. private buildHandshake;
  152. /**
  153. * Emits to this client.
  154. *
  155. * @example
  156. * io.on("connection", (socket) => {
  157. * socket.emit("hello", "world");
  158. *
  159. * // all serializable datastructures are supported (no need to call JSON.stringify)
  160. * socket.emit("hello", 1, "2", { 3: ["4"], 5: Buffer.from([6]) });
  161. *
  162. * // with an acknowledgement from the client
  163. * socket.emit("hello", "world", (val) => {
  164. * // ...
  165. * });
  166. * });
  167. *
  168. * @return Always returns `true`.
  169. */
  170. emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
  171. /**
  172. * @private
  173. */
  174. private registerAckCallback;
  175. /**
  176. * Targets a room when broadcasting.
  177. *
  178. * @example
  179. * io.on("connection", (socket) => {
  180. * // the “foo” event will be broadcast to all connected clients in the “room-101” room, except this socket
  181. * socket.to("room-101").emit("foo", "bar");
  182. *
  183. * // the code above is equivalent to:
  184. * io.to("room-101").except(socket.id).emit("foo", "bar");
  185. *
  186. * // with an array of rooms (a client will be notified at most once)
  187. * socket.to(["room-101", "room-102"]).emit("foo", "bar");
  188. *
  189. * // with multiple chained calls
  190. * socket.to("room-101").to("room-102").emit("foo", "bar");
  191. * });
  192. *
  193. * @param room - a room, or an array of rooms
  194. * @return a new {@link BroadcastOperator} instance for chaining
  195. */
  196. to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  197. /**
  198. * Targets a room when broadcasting. Similar to `to()`, but might feel clearer in some cases:
  199. *
  200. * @example
  201. * io.on("connection", (socket) => {
  202. * // disconnect all clients in the "room-101" room, except this socket
  203. * socket.in("room-101").disconnectSockets();
  204. * });
  205. *
  206. * @param room - a room, or an array of rooms
  207. * @return a new {@link BroadcastOperator} instance for chaining
  208. */
  209. in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  210. /**
  211. * Excludes a room when broadcasting.
  212. *
  213. * @example
  214. * io.on("connection", (socket) => {
  215. * // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
  216. * // and this socket
  217. * socket.except("room-101").emit("foo", "bar");
  218. *
  219. * // with an array of rooms
  220. * socket.except(["room-101", "room-102"]).emit("foo", "bar");
  221. *
  222. * // with multiple chained calls
  223. * socket.except("room-101").except("room-102").emit("foo", "bar");
  224. * });
  225. *
  226. * @param room - a room, or an array of rooms
  227. * @return a new {@link BroadcastOperator} instance for chaining
  228. */
  229. except(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData>;
  230. /**
  231. * Sends a `message` event.
  232. *
  233. * This method mimics the WebSocket.send() method.
  234. *
  235. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
  236. *
  237. * @example
  238. * io.on("connection", (socket) => {
  239. * socket.send("hello");
  240. *
  241. * // this is equivalent to
  242. * socket.emit("message", "hello");
  243. * });
  244. *
  245. * @return self
  246. */
  247. send(...args: EventParams<EmitEvents, "message">): this;
  248. /**
  249. * Sends a `message` event. Alias of {@link send}.
  250. *
  251. * @return self
  252. */
  253. write(...args: EventParams<EmitEvents, "message">): this;
  254. /**
  255. * Writes a packet.
  256. *
  257. * @param {Object} packet - packet object
  258. * @param {Object} opts - options
  259. * @private
  260. */
  261. private packet;
  262. /**
  263. * Joins a room.
  264. *
  265. * @example
  266. * io.on("connection", (socket) => {
  267. * // join a single room
  268. * socket.join("room1");
  269. *
  270. * // join multiple rooms
  271. * socket.join(["room1", "room2"]);
  272. * });
  273. *
  274. * @param {String|Array} rooms - room or array of rooms
  275. * @return a Promise or nothing, depending on the adapter
  276. */
  277. join(rooms: Room | Array<Room>): Promise<void> | void;
  278. /**
  279. * Leaves a room.
  280. *
  281. * @example
  282. * io.on("connection", (socket) => {
  283. * // leave a single room
  284. * socket.leave("room1");
  285. *
  286. * // leave multiple rooms
  287. * socket.leave("room1").leave("room2");
  288. * });
  289. *
  290. * @param {String} room
  291. * @return a Promise or nothing, depending on the adapter
  292. */
  293. leave(room: string): Promise<void> | void;
  294. /**
  295. * Leave all rooms.
  296. *
  297. * @private
  298. */
  299. private leaveAll;
  300. /**
  301. * Called by `Namespace` upon successful
  302. * middleware execution (ie: authorization).
  303. * Socket is added to namespace array before
  304. * call to join, so adapters can access it.
  305. *
  306. * @private
  307. */
  308. _onconnect(): void;
  309. /**
  310. * Called with each packet. Called by `Client`.
  311. *
  312. * @param {Object} packet
  313. * @private
  314. */
  315. _onpacket(packet: Packet): void;
  316. /**
  317. * Called upon event packet.
  318. *
  319. * @param {Packet} packet - packet object
  320. * @private
  321. */
  322. private onevent;
  323. /**
  324. * Produces an ack callback to emit with an event.
  325. *
  326. * @param {Number} id - packet id
  327. * @private
  328. */
  329. private ack;
  330. /**
  331. * Called upon ack packet.
  332. *
  333. * @private
  334. */
  335. private onack;
  336. /**
  337. * Called upon client disconnect packet.
  338. *
  339. * @private
  340. */
  341. private ondisconnect;
  342. /**
  343. * Handles a client error.
  344. *
  345. * @private
  346. */
  347. _onerror(err: Error): void;
  348. /**
  349. * Called upon closing. Called by `Client`.
  350. *
  351. * @param {String} reason
  352. * @throw {Error} optional error object
  353. *
  354. * @private
  355. */
  356. _onclose(reason: DisconnectReason): this | undefined;
  357. /**
  358. * Makes the socket leave all the rooms it was part of and prevents it from joining any other room
  359. *
  360. * @private
  361. */
  362. _cleanup(): void;
  363. /**
  364. * Produces an `error` packet.
  365. *
  366. * @param {Object} err - error object
  367. *
  368. * @private
  369. */
  370. _error(err: any): void;
  371. /**
  372. * Disconnects this client.
  373. *
  374. * @example
  375. * io.on("connection", (socket) => {
  376. * // disconnect this socket (the connection might be kept alive for other namespaces)
  377. * socket.disconnect();
  378. *
  379. * // disconnect this socket and close the underlying connection
  380. * socket.disconnect(true);
  381. * })
  382. *
  383. * @param {Boolean} close - if `true`, closes the underlying connection
  384. * @return self
  385. */
  386. disconnect(close?: boolean): this;
  387. /**
  388. * Sets the compress flag.
  389. *
  390. * @example
  391. * io.on("connection", (socket) => {
  392. * socket.compress(false).emit("hello");
  393. * });
  394. *
  395. * @param {Boolean} compress - if `true`, compresses the sending data
  396. * @return {Socket} self
  397. */
  398. compress(compress: boolean): this;
  399. /**
  400. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  401. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  402. * and is in the middle of a request-response cycle).
  403. *
  404. * @example
  405. * io.on("connection", (socket) => {
  406. * socket.volatile.emit("hello"); // the client may or may not receive it
  407. * });
  408. *
  409. * @return {Socket} self
  410. */
  411. get volatile(): this;
  412. /**
  413. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the
  414. * sender.
  415. *
  416. * @example
  417. * io.on("connection", (socket) => {
  418. * // the “foo” event will be broadcast to all connected clients, except this socket
  419. * socket.broadcast.emit("foo", "bar");
  420. * });
  421. *
  422. * @return a new {@link BroadcastOperator} instance for chaining
  423. */
  424. get broadcast(): BroadcastOperator<EmitEvents, SocketData>;
  425. /**
  426. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  427. *
  428. * @example
  429. * io.on("connection", (socket) => {
  430. * // the “foo” event will be broadcast to all connected clients on this node, except this socket
  431. * socket.local.emit("foo", "bar");
  432. * });
  433. *
  434. * @return a new {@link BroadcastOperator} instance for chaining
  435. */
  436. get local(): BroadcastOperator<EmitEvents, SocketData>;
  437. /**
  438. * Sets a modifier for a subsequent event emission that the callback will be called with an error when the
  439. * given number of milliseconds have elapsed without an acknowledgement from the client:
  440. *
  441. * @example
  442. * io.on("connection", (socket) => {
  443. * socket.timeout(5000).emit("my-event", (err) => {
  444. * if (err) {
  445. * // the client did not acknowledge the event in the given delay
  446. * }
  447. * });
  448. * });
  449. *
  450. * @returns self
  451. */
  452. timeout(timeout: number): this;
  453. /**
  454. * Dispatch incoming event to socket listeners.
  455. *
  456. * @param {Array} event - event that will get emitted
  457. * @private
  458. */
  459. private dispatch;
  460. /**
  461. * Sets up socket middleware.
  462. *
  463. * @example
  464. * io.on("connection", (socket) => {
  465. * socket.use(([event, ...args], next) => {
  466. * if (isUnauthorized(event)) {
  467. * return next(new Error("unauthorized event"));
  468. * }
  469. * // do not forget to call next
  470. * next();
  471. * });
  472. *
  473. * socket.on("error", (err) => {
  474. * if (err && err.message === "unauthorized event") {
  475. * socket.disconnect();
  476. * }
  477. * });
  478. * });
  479. *
  480. * @param {Function} fn - middleware function (event, next)
  481. * @return {Socket} self
  482. */
  483. use(fn: (event: Event, next: (err?: Error) => void) => void): this;
  484. /**
  485. * Executes the middleware for an incoming event.
  486. *
  487. * @param {Array} event - event that will get emitted
  488. * @param {Function} fn - last fn call in the middleware
  489. * @private
  490. */
  491. private run;
  492. /**
  493. * Whether the socket is currently disconnected
  494. */
  495. get disconnected(): boolean;
  496. /**
  497. * A reference to the request that originated the underlying Engine.IO Socket.
  498. */
  499. get request(): IncomingMessage;
  500. /**
  501. * A reference to the underlying Client transport connection (Engine.IO Socket object).
  502. *
  503. * @example
  504. * io.on("connection", (socket) => {
  505. * console.log(socket.conn.transport.name); // prints "polling" or "websocket"
  506. *
  507. * socket.conn.once("upgrade", () => {
  508. * console.log(socket.conn.transport.name); // prints "websocket"
  509. * });
  510. * });
  511. */
  512. get conn(): import("engine.io").Socket;
  513. /**
  514. * Returns the rooms the socket is currently in.
  515. *
  516. * @example
  517. * io.on("connection", (socket) => {
  518. * console.log(socket.rooms); // Set { <socket.id> }
  519. *
  520. * socket.join("room1");
  521. *
  522. * console.log(socket.rooms); // Set { <socket.id>, "room1" }
  523. * });
  524. */
  525. get rooms(): Set<Room>;
  526. /**
  527. * Adds a listener that will be fired when any event is received. The event name is passed as the first argument to
  528. * the callback.
  529. *
  530. * @example
  531. * io.on("connection", (socket) => {
  532. * socket.onAny((event, ...args) => {
  533. * console.log(`got event ${event}`);
  534. * });
  535. * });
  536. *
  537. * @param listener
  538. */
  539. onAny(listener: (...args: any[]) => void): this;
  540. /**
  541. * Adds a listener that will be fired when any event is received. The event name is passed as the first argument to
  542. * the callback. The listener is added to the beginning of the listeners array.
  543. *
  544. * @param listener
  545. */
  546. prependAny(listener: (...args: any[]) => void): this;
  547. /**
  548. * Removes the listener that will be fired when any event is received.
  549. *
  550. * @example
  551. * io.on("connection", (socket) => {
  552. * const catchAllListener = (event, ...args) => {
  553. * console.log(`got event ${event}`);
  554. * }
  555. *
  556. * socket.onAny(catchAllListener);
  557. *
  558. * // remove a specific listener
  559. * socket.offAny(catchAllListener);
  560. *
  561. * // or remove all listeners
  562. * socket.offAny();
  563. * });
  564. *
  565. * @param listener
  566. */
  567. offAny(listener?: (...args: any[]) => void): this;
  568. /**
  569. * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
  570. * e.g. to remove listeners.
  571. */
  572. listenersAny(): ((...args: any[]) => void)[];
  573. /**
  574. * Adds a listener that will be fired when any event is sent. The event name is passed as the first argument to
  575. * the callback.
  576. *
  577. * Note: acknowledgements sent to the client are not included.
  578. *
  579. * @example
  580. * io.on("connection", (socket) => {
  581. * socket.onAnyOutgoing((event, ...args) => {
  582. * console.log(`sent event ${event}`);
  583. * });
  584. * });
  585. *
  586. * @param listener
  587. */
  588. onAnyOutgoing(listener: (...args: any[]) => void): this;
  589. /**
  590. * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
  591. * callback. The listener is added to the beginning of the listeners array.
  592. *
  593. * @example
  594. * io.on("connection", (socket) => {
  595. * socket.prependAnyOutgoing((event, ...args) => {
  596. * console.log(`sent event ${event}`);
  597. * });
  598. * });
  599. *
  600. * @param listener
  601. */
  602. prependAnyOutgoing(listener: (...args: any[]) => void): this;
  603. /**
  604. * Removes the listener that will be fired when any event is sent.
  605. *
  606. * @example
  607. * io.on("connection", (socket) => {
  608. * const catchAllListener = (event, ...args) => {
  609. * console.log(`sent event ${event}`);
  610. * }
  611. *
  612. * socket.onAnyOutgoing(catchAllListener);
  613. *
  614. * // remove a specific listener
  615. * socket.offAnyOutgoing(catchAllListener);
  616. *
  617. * // or remove all listeners
  618. * socket.offAnyOutgoing();
  619. * });
  620. *
  621. * @param listener - the catch-all listener
  622. */
  623. offAnyOutgoing(listener?: (...args: any[]) => void): this;
  624. /**
  625. * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
  626. * e.g. to remove listeners.
  627. */
  628. listenersAnyOutgoing(): ((...args: any[]) => void)[];
  629. /**
  630. * Notify the listeners for each packet sent (emit or broadcast)
  631. *
  632. * @param packet
  633. *
  634. * @private
  635. */
  636. private notifyOutgoingListeners;
  637. private newBroadcastOperator;
  638. }