Node-Red configuration
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.Socket = exports.RESERVED_EVENTS = void 0;
  7. const socket_io_parser_1 = require("socket.io-parser");
  8. const debug_1 = __importDefault(require("debug"));
  9. const typed_events_1 = require("./typed-events");
  10. const base64id_1 = __importDefault(require("base64id"));
  11. const broadcast_operator_1 = require("./broadcast-operator");
  12. const debug = (0, debug_1.default)("socket.io:socket");
  13. exports.RESERVED_EVENTS = new Set([
  14. "connect",
  15. "connect_error",
  16. "disconnect",
  17. "disconnecting",
  18. "newListener",
  19. "removeListener",
  20. ]);
  21. function noop() { }
  22. /**
  23. * This is the main object for interacting with a client.
  24. *
  25. * A Socket belongs to a given {@link Namespace} and uses an underlying {@link Client} to communicate.
  26. *
  27. * Within each {@link Namespace}, you can also define arbitrary channels (called "rooms") that the {@link Socket} can
  28. * join and leave. That provides a convenient way to broadcast to a group of socket instances.
  29. *
  30. * @example
  31. * io.on("connection", (socket) => {
  32. * console.log(`socket ${socket.id} connected`);
  33. *
  34. * // send an event to the client
  35. * socket.emit("foo", "bar");
  36. *
  37. * socket.on("foobar", () => {
  38. * // an event was received from the client
  39. * });
  40. *
  41. * // join the room named "room1"
  42. * socket.join("room1");
  43. *
  44. * // broadcast to everyone in the room named "room1"
  45. * io.to("room1").emit("hello");
  46. *
  47. * // upon disconnection
  48. * socket.on("disconnect", (reason) => {
  49. * console.log(`socket ${socket.id} disconnected due to ${reason}`);
  50. * });
  51. * });
  52. */
  53. class Socket extends typed_events_1.StrictEventEmitter {
  54. /**
  55. * Interface to a `Client` for a given `Namespace`.
  56. *
  57. * @param {Namespace} nsp
  58. * @param {Client} client
  59. * @param {Object} auth
  60. * @package
  61. */
  62. constructor(nsp, client, auth) {
  63. super();
  64. this.nsp = nsp;
  65. this.client = client;
  66. /**
  67. * Additional information that can be attached to the Socket instance and which will be used in the
  68. * {@link Server.fetchSockets()} method.
  69. */
  70. this.data = {};
  71. /**
  72. * Whether the socket is currently connected or not.
  73. *
  74. * @example
  75. * io.use((socket, next) => {
  76. * console.log(socket.connected); // false
  77. * next();
  78. * });
  79. *
  80. * io.on("connection", (socket) => {
  81. * console.log(socket.connected); // true
  82. * });
  83. */
  84. this.connected = false;
  85. this.acks = new Map();
  86. this.fns = [];
  87. this.flags = {};
  88. this.server = nsp.server;
  89. this.adapter = this.nsp.adapter;
  90. if (client.conn.protocol === 3) {
  91. // @ts-ignore
  92. this.id = nsp.name !== "/" ? nsp.name + "#" + client.id : client.id;
  93. }
  94. else {
  95. this.id = base64id_1.default.generateId(); // don't reuse the Engine.IO id because it's sensitive information
  96. }
  97. this.handshake = this.buildHandshake(auth);
  98. }
  99. /**
  100. * Builds the `handshake` BC object
  101. *
  102. * @private
  103. */
  104. buildHandshake(auth) {
  105. return {
  106. headers: this.request.headers,
  107. time: new Date() + "",
  108. address: this.conn.remoteAddress,
  109. xdomain: !!this.request.headers.origin,
  110. // @ts-ignore
  111. secure: !!this.request.connection.encrypted,
  112. issued: +new Date(),
  113. url: this.request.url,
  114. // @ts-ignore
  115. query: this.request._query,
  116. auth,
  117. };
  118. }
  119. /**
  120. * Emits to this client.
  121. *
  122. * @example
  123. * io.on("connection", (socket) => {
  124. * socket.emit("hello", "world");
  125. *
  126. * // all serializable datastructures are supported (no need to call JSON.stringify)
  127. * socket.emit("hello", 1, "2", { 3: ["4"], 5: Buffer.from([6]) });
  128. *
  129. * // with an acknowledgement from the client
  130. * socket.emit("hello", "world", (val) => {
  131. * // ...
  132. * });
  133. * });
  134. *
  135. * @return Always returns `true`.
  136. */
  137. emit(ev, ...args) {
  138. if (exports.RESERVED_EVENTS.has(ev)) {
  139. throw new Error(`"${String(ev)}" is a reserved event name`);
  140. }
  141. const data = [ev, ...args];
  142. const packet = {
  143. type: socket_io_parser_1.PacketType.EVENT,
  144. data: data,
  145. };
  146. // access last argument to see if it's an ACK callback
  147. if (typeof data[data.length - 1] === "function") {
  148. const id = this.nsp._ids++;
  149. debug("emitting packet with ack id %d", id);
  150. this.registerAckCallback(id, data.pop());
  151. packet.id = id;
  152. }
  153. const flags = Object.assign({}, this.flags);
  154. this.flags = {};
  155. this.notifyOutgoingListeners(packet);
  156. this.packet(packet, flags);
  157. return true;
  158. }
  159. /**
  160. * @private
  161. */
  162. registerAckCallback(id, ack) {
  163. const timeout = this.flags.timeout;
  164. if (timeout === undefined) {
  165. this.acks.set(id, ack);
  166. return;
  167. }
  168. const timer = setTimeout(() => {
  169. debug("event with ack id %d has timed out after %d ms", id, timeout);
  170. this.acks.delete(id);
  171. ack.call(this, new Error("operation has timed out"));
  172. }, timeout);
  173. this.acks.set(id, (...args) => {
  174. clearTimeout(timer);
  175. ack.apply(this, [null, ...args]);
  176. });
  177. }
  178. /**
  179. * Targets a room when broadcasting.
  180. *
  181. * @example
  182. * io.on("connection", (socket) => {
  183. * // the “foo” event will be broadcast to all connected clients in the “room-101” room, except this socket
  184. * socket.to("room-101").emit("foo", "bar");
  185. *
  186. * // the code above is equivalent to:
  187. * io.to("room-101").except(socket.id).emit("foo", "bar");
  188. *
  189. * // with an array of rooms (a client will be notified at most once)
  190. * socket.to(["room-101", "room-102"]).emit("foo", "bar");
  191. *
  192. * // with multiple chained calls
  193. * socket.to("room-101").to("room-102").emit("foo", "bar");
  194. * });
  195. *
  196. * @param room - a room, or an array of rooms
  197. * @return a new {@link BroadcastOperator} instance for chaining
  198. */
  199. to(room) {
  200. return this.newBroadcastOperator().to(room);
  201. }
  202. /**
  203. * Targets a room when broadcasting. Similar to `to()`, but might feel clearer in some cases:
  204. *
  205. * @example
  206. * io.on("connection", (socket) => {
  207. * // disconnect all clients in the "room-101" room, except this socket
  208. * socket.in("room-101").disconnectSockets();
  209. * });
  210. *
  211. * @param room - a room, or an array of rooms
  212. * @return a new {@link BroadcastOperator} instance for chaining
  213. */
  214. in(room) {
  215. return this.newBroadcastOperator().in(room);
  216. }
  217. /**
  218. * Excludes a room when broadcasting.
  219. *
  220. * @example
  221. * io.on("connection", (socket) => {
  222. * // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
  223. * // and this socket
  224. * socket.except("room-101").emit("foo", "bar");
  225. *
  226. * // with an array of rooms
  227. * socket.except(["room-101", "room-102"]).emit("foo", "bar");
  228. *
  229. * // with multiple chained calls
  230. * socket.except("room-101").except("room-102").emit("foo", "bar");
  231. * });
  232. *
  233. * @param room - a room, or an array of rooms
  234. * @return a new {@link BroadcastOperator} instance for chaining
  235. */
  236. except(room) {
  237. return this.newBroadcastOperator().except(room);
  238. }
  239. /**
  240. * Sends a `message` event.
  241. *
  242. * This method mimics the WebSocket.send() method.
  243. *
  244. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
  245. *
  246. * @example
  247. * io.on("connection", (socket) => {
  248. * socket.send("hello");
  249. *
  250. * // this is equivalent to
  251. * socket.emit("message", "hello");
  252. * });
  253. *
  254. * @return self
  255. */
  256. send(...args) {
  257. this.emit("message", ...args);
  258. return this;
  259. }
  260. /**
  261. * Sends a `message` event. Alias of {@link send}.
  262. *
  263. * @return self
  264. */
  265. write(...args) {
  266. this.emit("message", ...args);
  267. return this;
  268. }
  269. /**
  270. * Writes a packet.
  271. *
  272. * @param {Object} packet - packet object
  273. * @param {Object} opts - options
  274. * @private
  275. */
  276. packet(packet, opts = {}) {
  277. packet.nsp = this.nsp.name;
  278. opts.compress = false !== opts.compress;
  279. this.client._packet(packet, opts);
  280. }
  281. /**
  282. * Joins a room.
  283. *
  284. * @example
  285. * io.on("connection", (socket) => {
  286. * // join a single room
  287. * socket.join("room1");
  288. *
  289. * // join multiple rooms
  290. * socket.join(["room1", "room2"]);
  291. * });
  292. *
  293. * @param {String|Array} rooms - room or array of rooms
  294. * @return a Promise or nothing, depending on the adapter
  295. */
  296. join(rooms) {
  297. debug("join room %s", rooms);
  298. return this.adapter.addAll(this.id, new Set(Array.isArray(rooms) ? rooms : [rooms]));
  299. }
  300. /**
  301. * Leaves a room.
  302. *
  303. * @example
  304. * io.on("connection", (socket) => {
  305. * // leave a single room
  306. * socket.leave("room1");
  307. *
  308. * // leave multiple rooms
  309. * socket.leave("room1").leave("room2");
  310. * });
  311. *
  312. * @param {String} room
  313. * @return a Promise or nothing, depending on the adapter
  314. */
  315. leave(room) {
  316. debug("leave room %s", room);
  317. return this.adapter.del(this.id, room);
  318. }
  319. /**
  320. * Leave all rooms.
  321. *
  322. * @private
  323. */
  324. leaveAll() {
  325. this.adapter.delAll(this.id);
  326. }
  327. /**
  328. * Called by `Namespace` upon successful
  329. * middleware execution (ie: authorization).
  330. * Socket is added to namespace array before
  331. * call to join, so adapters can access it.
  332. *
  333. * @private
  334. */
  335. _onconnect() {
  336. debug("socket connected - writing packet");
  337. this.connected = true;
  338. this.join(this.id);
  339. if (this.conn.protocol === 3) {
  340. this.packet({ type: socket_io_parser_1.PacketType.CONNECT });
  341. }
  342. else {
  343. this.packet({ type: socket_io_parser_1.PacketType.CONNECT, data: { sid: this.id } });
  344. }
  345. }
  346. /**
  347. * Called with each packet. Called by `Client`.
  348. *
  349. * @param {Object} packet
  350. * @private
  351. */
  352. _onpacket(packet) {
  353. debug("got packet %j", packet);
  354. switch (packet.type) {
  355. case socket_io_parser_1.PacketType.EVENT:
  356. this.onevent(packet);
  357. break;
  358. case socket_io_parser_1.PacketType.BINARY_EVENT:
  359. this.onevent(packet);
  360. break;
  361. case socket_io_parser_1.PacketType.ACK:
  362. this.onack(packet);
  363. break;
  364. case socket_io_parser_1.PacketType.BINARY_ACK:
  365. this.onack(packet);
  366. break;
  367. case socket_io_parser_1.PacketType.DISCONNECT:
  368. this.ondisconnect();
  369. break;
  370. }
  371. }
  372. /**
  373. * Called upon event packet.
  374. *
  375. * @param {Packet} packet - packet object
  376. * @private
  377. */
  378. onevent(packet) {
  379. const args = packet.data || [];
  380. debug("emitting event %j", args);
  381. if (null != packet.id) {
  382. debug("attaching ack callback to event");
  383. args.push(this.ack(packet.id));
  384. }
  385. if (this._anyListeners && this._anyListeners.length) {
  386. const listeners = this._anyListeners.slice();
  387. for (const listener of listeners) {
  388. listener.apply(this, args);
  389. }
  390. }
  391. this.dispatch(args);
  392. }
  393. /**
  394. * Produces an ack callback to emit with an event.
  395. *
  396. * @param {Number} id - packet id
  397. * @private
  398. */
  399. ack(id) {
  400. const self = this;
  401. let sent = false;
  402. return function () {
  403. // prevent double callbacks
  404. if (sent)
  405. return;
  406. const args = Array.prototype.slice.call(arguments);
  407. debug("sending ack %j", args);
  408. self.packet({
  409. id: id,
  410. type: socket_io_parser_1.PacketType.ACK,
  411. data: args,
  412. });
  413. sent = true;
  414. };
  415. }
  416. /**
  417. * Called upon ack packet.
  418. *
  419. * @private
  420. */
  421. onack(packet) {
  422. const ack = this.acks.get(packet.id);
  423. if ("function" == typeof ack) {
  424. debug("calling ack %s with %j", packet.id, packet.data);
  425. ack.apply(this, packet.data);
  426. this.acks.delete(packet.id);
  427. }
  428. else {
  429. debug("bad ack %s", packet.id);
  430. }
  431. }
  432. /**
  433. * Called upon client disconnect packet.
  434. *
  435. * @private
  436. */
  437. ondisconnect() {
  438. debug("got disconnect packet");
  439. this._onclose("client namespace disconnect");
  440. }
  441. /**
  442. * Handles a client error.
  443. *
  444. * @private
  445. */
  446. _onerror(err) {
  447. if (this.listeners("error").length) {
  448. this.emitReserved("error", err);
  449. }
  450. else {
  451. console.error("Missing error handler on `socket`.");
  452. console.error(err.stack);
  453. }
  454. }
  455. /**
  456. * Called upon closing. Called by `Client`.
  457. *
  458. * @param {String} reason
  459. * @throw {Error} optional error object
  460. *
  461. * @private
  462. */
  463. _onclose(reason) {
  464. if (!this.connected)
  465. return this;
  466. debug("closing socket - reason %s", reason);
  467. this.emitReserved("disconnecting", reason);
  468. this._cleanup();
  469. this.nsp._remove(this);
  470. this.client._remove(this);
  471. this.connected = false;
  472. this.emitReserved("disconnect", reason);
  473. return;
  474. }
  475. /**
  476. * Makes the socket leave all the rooms it was part of and prevents it from joining any other room
  477. *
  478. * @private
  479. */
  480. _cleanup() {
  481. this.leaveAll();
  482. this.join = noop;
  483. }
  484. /**
  485. * Produces an `error` packet.
  486. *
  487. * @param {Object} err - error object
  488. *
  489. * @private
  490. */
  491. _error(err) {
  492. this.packet({ type: socket_io_parser_1.PacketType.CONNECT_ERROR, data: err });
  493. }
  494. /**
  495. * Disconnects this client.
  496. *
  497. * @example
  498. * io.on("connection", (socket) => {
  499. * // disconnect this socket (the connection might be kept alive for other namespaces)
  500. * socket.disconnect();
  501. *
  502. * // disconnect this socket and close the underlying connection
  503. * socket.disconnect(true);
  504. * })
  505. *
  506. * @param {Boolean} close - if `true`, closes the underlying connection
  507. * @return self
  508. */
  509. disconnect(close = false) {
  510. if (!this.connected)
  511. return this;
  512. if (close) {
  513. this.client._disconnect();
  514. }
  515. else {
  516. this.packet({ type: socket_io_parser_1.PacketType.DISCONNECT });
  517. this._onclose("server namespace disconnect");
  518. }
  519. return this;
  520. }
  521. /**
  522. * Sets the compress flag.
  523. *
  524. * @example
  525. * io.on("connection", (socket) => {
  526. * socket.compress(false).emit("hello");
  527. * });
  528. *
  529. * @param {Boolean} compress - if `true`, compresses the sending data
  530. * @return {Socket} self
  531. */
  532. compress(compress) {
  533. this.flags.compress = compress;
  534. return this;
  535. }
  536. /**
  537. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  538. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  539. * and is in the middle of a request-response cycle).
  540. *
  541. * @example
  542. * io.on("connection", (socket) => {
  543. * socket.volatile.emit("hello"); // the client may or may not receive it
  544. * });
  545. *
  546. * @return {Socket} self
  547. */
  548. get volatile() {
  549. this.flags.volatile = true;
  550. return this;
  551. }
  552. /**
  553. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to every sockets but the
  554. * sender.
  555. *
  556. * @example
  557. * io.on("connection", (socket) => {
  558. * // the “foo” event will be broadcast to all connected clients, except this socket
  559. * socket.broadcast.emit("foo", "bar");
  560. * });
  561. *
  562. * @return a new {@link BroadcastOperator} instance for chaining
  563. */
  564. get broadcast() {
  565. return this.newBroadcastOperator();
  566. }
  567. /**
  568. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  569. *
  570. * @example
  571. * io.on("connection", (socket) => {
  572. * // the “foo” event will be broadcast to all connected clients on this node, except this socket
  573. * socket.local.emit("foo", "bar");
  574. * });
  575. *
  576. * @return a new {@link BroadcastOperator} instance for chaining
  577. */
  578. get local() {
  579. return this.newBroadcastOperator().local;
  580. }
  581. /**
  582. * Sets a modifier for a subsequent event emission that the callback will be called with an error when the
  583. * given number of milliseconds have elapsed without an acknowledgement from the client:
  584. *
  585. * @example
  586. * io.on("connection", (socket) => {
  587. * socket.timeout(5000).emit("my-event", (err) => {
  588. * if (err) {
  589. * // the client did not acknowledge the event in the given delay
  590. * }
  591. * });
  592. * });
  593. *
  594. * @returns self
  595. */
  596. timeout(timeout) {
  597. this.flags.timeout = timeout;
  598. return this;
  599. }
  600. /**
  601. * Dispatch incoming event to socket listeners.
  602. *
  603. * @param {Array} event - event that will get emitted
  604. * @private
  605. */
  606. dispatch(event) {
  607. debug("dispatching an event %j", event);
  608. this.run(event, (err) => {
  609. process.nextTick(() => {
  610. if (err) {
  611. return this._onerror(err);
  612. }
  613. if (this.connected) {
  614. super.emitUntyped.apply(this, event);
  615. }
  616. else {
  617. debug("ignore packet received after disconnection");
  618. }
  619. });
  620. });
  621. }
  622. /**
  623. * Sets up socket middleware.
  624. *
  625. * @example
  626. * io.on("connection", (socket) => {
  627. * socket.use(([event, ...args], next) => {
  628. * if (isUnauthorized(event)) {
  629. * return next(new Error("unauthorized event"));
  630. * }
  631. * // do not forget to call next
  632. * next();
  633. * });
  634. *
  635. * socket.on("error", (err) => {
  636. * if (err && err.message === "unauthorized event") {
  637. * socket.disconnect();
  638. * }
  639. * });
  640. * });
  641. *
  642. * @param {Function} fn - middleware function (event, next)
  643. * @return {Socket} self
  644. */
  645. use(fn) {
  646. this.fns.push(fn);
  647. return this;
  648. }
  649. /**
  650. * Executes the middleware for an incoming event.
  651. *
  652. * @param {Array} event - event that will get emitted
  653. * @param {Function} fn - last fn call in the middleware
  654. * @private
  655. */
  656. run(event, fn) {
  657. const fns = this.fns.slice(0);
  658. if (!fns.length)
  659. return fn(null);
  660. function run(i) {
  661. fns[i](event, function (err) {
  662. // upon error, short-circuit
  663. if (err)
  664. return fn(err);
  665. // if no middleware left, summon callback
  666. if (!fns[i + 1])
  667. return fn(null);
  668. // go on to next
  669. run(i + 1);
  670. });
  671. }
  672. run(0);
  673. }
  674. /**
  675. * Whether the socket is currently disconnected
  676. */
  677. get disconnected() {
  678. return !this.connected;
  679. }
  680. /**
  681. * A reference to the request that originated the underlying Engine.IO Socket.
  682. */
  683. get request() {
  684. return this.client.request;
  685. }
  686. /**
  687. * A reference to the underlying Client transport connection (Engine.IO Socket object).
  688. *
  689. * @example
  690. * io.on("connection", (socket) => {
  691. * console.log(socket.conn.transport.name); // prints "polling" or "websocket"
  692. *
  693. * socket.conn.once("upgrade", () => {
  694. * console.log(socket.conn.transport.name); // prints "websocket"
  695. * });
  696. * });
  697. */
  698. get conn() {
  699. return this.client.conn;
  700. }
  701. /**
  702. * Returns the rooms the socket is currently in.
  703. *
  704. * @example
  705. * io.on("connection", (socket) => {
  706. * console.log(socket.rooms); // Set { <socket.id> }
  707. *
  708. * socket.join("room1");
  709. *
  710. * console.log(socket.rooms); // Set { <socket.id>, "room1" }
  711. * });
  712. */
  713. get rooms() {
  714. return this.adapter.socketRooms(this.id) || new Set();
  715. }
  716. /**
  717. * Adds a listener that will be fired when any event is received. The event name is passed as the first argument to
  718. * the callback.
  719. *
  720. * @example
  721. * io.on("connection", (socket) => {
  722. * socket.onAny((event, ...args) => {
  723. * console.log(`got event ${event}`);
  724. * });
  725. * });
  726. *
  727. * @param listener
  728. */
  729. onAny(listener) {
  730. this._anyListeners = this._anyListeners || [];
  731. this._anyListeners.push(listener);
  732. return this;
  733. }
  734. /**
  735. * Adds a listener that will be fired when any event is received. The event name is passed as the first argument to
  736. * the callback. The listener is added to the beginning of the listeners array.
  737. *
  738. * @param listener
  739. */
  740. prependAny(listener) {
  741. this._anyListeners = this._anyListeners || [];
  742. this._anyListeners.unshift(listener);
  743. return this;
  744. }
  745. /**
  746. * Removes the listener that will be fired when any event is received.
  747. *
  748. * @example
  749. * io.on("connection", (socket) => {
  750. * const catchAllListener = (event, ...args) => {
  751. * console.log(`got event ${event}`);
  752. * }
  753. *
  754. * socket.onAny(catchAllListener);
  755. *
  756. * // remove a specific listener
  757. * socket.offAny(catchAllListener);
  758. *
  759. * // or remove all listeners
  760. * socket.offAny();
  761. * });
  762. *
  763. * @param listener
  764. */
  765. offAny(listener) {
  766. if (!this._anyListeners) {
  767. return this;
  768. }
  769. if (listener) {
  770. const listeners = this._anyListeners;
  771. for (let i = 0; i < listeners.length; i++) {
  772. if (listener === listeners[i]) {
  773. listeners.splice(i, 1);
  774. return this;
  775. }
  776. }
  777. }
  778. else {
  779. this._anyListeners = [];
  780. }
  781. return this;
  782. }
  783. /**
  784. * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
  785. * e.g. to remove listeners.
  786. */
  787. listenersAny() {
  788. return this._anyListeners || [];
  789. }
  790. /**
  791. * Adds a listener that will be fired when any event is sent. The event name is passed as the first argument to
  792. * the callback.
  793. *
  794. * Note: acknowledgements sent to the client are not included.
  795. *
  796. * @example
  797. * io.on("connection", (socket) => {
  798. * socket.onAnyOutgoing((event, ...args) => {
  799. * console.log(`sent event ${event}`);
  800. * });
  801. * });
  802. *
  803. * @param listener
  804. */
  805. onAnyOutgoing(listener) {
  806. this._anyOutgoingListeners = this._anyOutgoingListeners || [];
  807. this._anyOutgoingListeners.push(listener);
  808. return this;
  809. }
  810. /**
  811. * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
  812. * callback. The listener is added to the beginning of the listeners array.
  813. *
  814. * @example
  815. * io.on("connection", (socket) => {
  816. * socket.prependAnyOutgoing((event, ...args) => {
  817. * console.log(`sent event ${event}`);
  818. * });
  819. * });
  820. *
  821. * @param listener
  822. */
  823. prependAnyOutgoing(listener) {
  824. this._anyOutgoingListeners = this._anyOutgoingListeners || [];
  825. this._anyOutgoingListeners.unshift(listener);
  826. return this;
  827. }
  828. /**
  829. * Removes the listener that will be fired when any event is sent.
  830. *
  831. * @example
  832. * io.on("connection", (socket) => {
  833. * const catchAllListener = (event, ...args) => {
  834. * console.log(`sent event ${event}`);
  835. * }
  836. *
  837. * socket.onAnyOutgoing(catchAllListener);
  838. *
  839. * // remove a specific listener
  840. * socket.offAnyOutgoing(catchAllListener);
  841. *
  842. * // or remove all listeners
  843. * socket.offAnyOutgoing();
  844. * });
  845. *
  846. * @param listener - the catch-all listener
  847. */
  848. offAnyOutgoing(listener) {
  849. if (!this._anyOutgoingListeners) {
  850. return this;
  851. }
  852. if (listener) {
  853. const listeners = this._anyOutgoingListeners;
  854. for (let i = 0; i < listeners.length; i++) {
  855. if (listener === listeners[i]) {
  856. listeners.splice(i, 1);
  857. return this;
  858. }
  859. }
  860. }
  861. else {
  862. this._anyOutgoingListeners = [];
  863. }
  864. return this;
  865. }
  866. /**
  867. * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
  868. * e.g. to remove listeners.
  869. */
  870. listenersAnyOutgoing() {
  871. return this._anyOutgoingListeners || [];
  872. }
  873. /**
  874. * Notify the listeners for each packet sent (emit or broadcast)
  875. *
  876. * @param packet
  877. *
  878. * @private
  879. */
  880. notifyOutgoingListeners(packet) {
  881. if (this._anyOutgoingListeners && this._anyOutgoingListeners.length) {
  882. const listeners = this._anyOutgoingListeners.slice();
  883. for (const listener of listeners) {
  884. listener.apply(this, packet.data);
  885. }
  886. }
  887. }
  888. newBroadcastOperator() {
  889. const flags = Object.assign({}, this.flags);
  890. this.flags = {};
  891. return new broadcast_operator_1.BroadcastOperator(this.adapter, new Set(), new Set([this.id]), flags);
  892. }
  893. }
  894. exports.Socket = Socket;