socket.js 30 KB

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