socket.js 29 KB

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