index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.Namespace = exports.Socket = exports.Server = void 0;
  30. const http = require("http");
  31. const fs_1 = require("fs");
  32. const zlib_1 = require("zlib");
  33. const accepts = require("accepts");
  34. const stream_1 = require("stream");
  35. const path = require("path");
  36. const engine_io_1 = require("engine.io");
  37. const client_1 = require("./client");
  38. const events_1 = require("events");
  39. const namespace_1 = require("./namespace");
  40. Object.defineProperty(exports, "Namespace", { enumerable: true, get: function () { return namespace_1.Namespace; } });
  41. const parent_namespace_1 = require("./parent-namespace");
  42. const socket_io_adapter_1 = require("socket.io-adapter");
  43. const parser = __importStar(require("socket.io-parser"));
  44. const debug_1 = __importDefault(require("debug"));
  45. const socket_1 = require("./socket");
  46. Object.defineProperty(exports, "Socket", { enumerable: true, get: function () { return socket_1.Socket; } });
  47. const typed_events_1 = require("./typed-events");
  48. const uws_1 = require("./uws");
  49. const debug = (0, debug_1.default)("socket.io:server");
  50. const clientVersion = require("../package.json").version;
  51. const dotMapRegex = /\.map/;
  52. /**
  53. * Represents a Socket.IO server.
  54. *
  55. * @example
  56. * import { Server } from "socket.io";
  57. *
  58. * const io = new Server();
  59. *
  60. * io.on("connection", (socket) => {
  61. * console.log(`socket ${socket.id} connected`);
  62. *
  63. * // send an event to the client
  64. * socket.emit("foo", "bar");
  65. *
  66. * socket.on("foobar", () => {
  67. * // an event was received from the client
  68. * });
  69. *
  70. * // upon disconnection
  71. * socket.on("disconnect", (reason) => {
  72. * console.log(`socket ${socket.id} disconnected due to ${reason}`);
  73. * });
  74. * });
  75. *
  76. * io.listen(3000);
  77. */
  78. class Server extends typed_events_1.StrictEventEmitter {
  79. constructor(srv, opts = {}) {
  80. super();
  81. /**
  82. * @private
  83. */
  84. this._nsps = new Map();
  85. this.parentNsps = new Map();
  86. /**
  87. * A subset of the {@link parentNsps} map, only containing {@link ParentNamespace} which are based on a regular
  88. * expression.
  89. *
  90. * @private
  91. */
  92. this.parentNamespacesFromRegExp = new Map();
  93. if ("object" === typeof srv &&
  94. srv instanceof Object &&
  95. !srv.listen) {
  96. opts = srv;
  97. srv = undefined;
  98. }
  99. this.path(opts.path || "/socket.io");
  100. this.connectTimeout(opts.connectTimeout || 45000);
  101. this.serveClient(false !== opts.serveClient);
  102. this._parser = opts.parser || parser;
  103. this.encoder = new this._parser.Encoder();
  104. this.opts = opts;
  105. if (opts.connectionStateRecovery) {
  106. opts.connectionStateRecovery = Object.assign({
  107. maxDisconnectionDuration: 2 * 60 * 1000,
  108. skipMiddlewares: true,
  109. }, opts.connectionStateRecovery);
  110. this.adapter(opts.adapter || socket_io_adapter_1.SessionAwareAdapter);
  111. }
  112. else {
  113. this.adapter(opts.adapter || socket_io_adapter_1.Adapter);
  114. }
  115. opts.cleanupEmptyChildNamespaces = !!opts.cleanupEmptyChildNamespaces;
  116. this.sockets = this.of("/");
  117. if (srv || typeof srv == "number")
  118. this.attach(srv);
  119. }
  120. get _opts() {
  121. return this.opts;
  122. }
  123. serveClient(v) {
  124. if (!arguments.length)
  125. return this._serveClient;
  126. this._serveClient = v;
  127. return this;
  128. }
  129. /**
  130. * Executes the middleware for an incoming namespace not already created on the server.
  131. *
  132. * @param name - name of incoming namespace
  133. * @param auth - the auth parameters
  134. * @param fn - callback
  135. *
  136. * @private
  137. */
  138. _checkNamespace(name, auth, fn) {
  139. if (this.parentNsps.size === 0)
  140. return fn(false);
  141. const keysIterator = this.parentNsps.keys();
  142. const run = () => {
  143. const nextFn = keysIterator.next();
  144. if (nextFn.done) {
  145. return fn(false);
  146. }
  147. nextFn.value(name, auth, (err, allow) => {
  148. if (err || !allow) {
  149. return run();
  150. }
  151. if (this._nsps.has(name)) {
  152. // the namespace was created in the meantime
  153. debug("dynamic namespace %s already exists", name);
  154. return fn(this._nsps.get(name));
  155. }
  156. const namespace = this.parentNsps.get(nextFn.value).createChild(name);
  157. debug("dynamic namespace %s was created", name);
  158. fn(namespace);
  159. });
  160. };
  161. run();
  162. }
  163. path(v) {
  164. if (!arguments.length)
  165. return this._path;
  166. this._path = v.replace(/\/$/, "");
  167. const escapedPath = this._path.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
  168. this.clientPathRegex = new RegExp("^" +
  169. escapedPath +
  170. "/socket\\.io(\\.msgpack|\\.esm)?(\\.min)?\\.js(\\.map)?(?:\\?|$)");
  171. return this;
  172. }
  173. connectTimeout(v) {
  174. if (v === undefined)
  175. return this._connectTimeout;
  176. this._connectTimeout = v;
  177. return this;
  178. }
  179. adapter(v) {
  180. if (!arguments.length)
  181. return this._adapter;
  182. this._adapter = v;
  183. for (const nsp of this._nsps.values()) {
  184. nsp._initAdapter();
  185. }
  186. return this;
  187. }
  188. /**
  189. * Attaches socket.io to a server or port.
  190. *
  191. * @param srv - server or port
  192. * @param opts - options passed to engine.io
  193. * @return self
  194. */
  195. listen(srv, opts = {}) {
  196. return this.attach(srv, opts);
  197. }
  198. /**
  199. * Attaches socket.io to a server or port.
  200. *
  201. * @param srv - server or port
  202. * @param opts - options passed to engine.io
  203. * @return self
  204. */
  205. attach(srv, opts = {}) {
  206. if ("function" == typeof srv) {
  207. const msg = "You are trying to attach socket.io to an express " +
  208. "request handler function. Please pass a http.Server instance.";
  209. throw new Error(msg);
  210. }
  211. // handle a port as a string
  212. if (Number(srv) == srv) {
  213. srv = Number(srv);
  214. }
  215. if ("number" == typeof srv) {
  216. debug("creating http server and binding to %d", srv);
  217. const port = srv;
  218. srv = http.createServer((req, res) => {
  219. res.writeHead(404);
  220. res.end();
  221. });
  222. srv.listen(port);
  223. }
  224. // merge the options passed to the Socket.IO server
  225. Object.assign(opts, this.opts);
  226. // set engine.io path to `/socket.io`
  227. opts.path = opts.path || this._path;
  228. this.initEngine(srv, opts);
  229. return this;
  230. }
  231. attachApp(app /*: TemplatedApp */, opts = {}) {
  232. // merge the options passed to the Socket.IO server
  233. Object.assign(opts, this.opts);
  234. // set engine.io path to `/socket.io`
  235. opts.path = opts.path || this._path;
  236. // initialize engine
  237. debug("creating uWebSockets.js-based engine with opts %j", opts);
  238. const engine = new engine_io_1.uServer(opts);
  239. engine.attach(app, opts);
  240. // bind to engine events
  241. this.bind(engine);
  242. if (this._serveClient) {
  243. // attach static file serving
  244. app.get(`${this._path}/*`, (res, req) => {
  245. if (!this.clientPathRegex.test(req.getUrl())) {
  246. req.setYield(true);
  247. return;
  248. }
  249. const filename = req
  250. .getUrl()
  251. .replace(this._path, "")
  252. .replace(/\?.*$/, "")
  253. .replace(/^\//, "");
  254. const isMap = dotMapRegex.test(filename);
  255. const type = isMap ? "map" : "source";
  256. // Per the standard, ETags must be quoted:
  257. // https://tools.ietf.org/html/rfc7232#section-2.3
  258. const expectedEtag = '"' + clientVersion + '"';
  259. const weakEtag = "W/" + expectedEtag;
  260. const etag = req.getHeader("if-none-match");
  261. if (etag) {
  262. if (expectedEtag === etag || weakEtag === etag) {
  263. debug("serve client %s 304", type);
  264. res.writeStatus("304 Not Modified");
  265. res.end();
  266. return;
  267. }
  268. }
  269. debug("serve client %s", type);
  270. res.writeHeader("cache-control", "public, max-age=0");
  271. res.writeHeader("content-type", "application/" + (isMap ? "json" : "javascript") + "; charset=utf-8");
  272. res.writeHeader("etag", expectedEtag);
  273. const filepath = path.join(__dirname, "../client-dist/", filename);
  274. (0, uws_1.serveFile)(res, filepath);
  275. });
  276. }
  277. (0, uws_1.patchAdapter)(app);
  278. }
  279. /**
  280. * Initialize engine
  281. *
  282. * @param srv - the server to attach to
  283. * @param opts - options passed to engine.io
  284. * @private
  285. */
  286. initEngine(srv, opts) {
  287. // initialize engine
  288. debug("creating engine.io instance with opts %j", opts);
  289. this.eio = (0, engine_io_1.attach)(srv, opts);
  290. // attach static file serving
  291. if (this._serveClient)
  292. this.attachServe(srv);
  293. // Export http server
  294. this.httpServer = srv;
  295. // bind to engine events
  296. this.bind(this.eio);
  297. }
  298. /**
  299. * Attaches the static file serving.
  300. *
  301. * @param srv http server
  302. * @private
  303. */
  304. attachServe(srv) {
  305. debug("attaching client serving req handler");
  306. const evs = srv.listeners("request").slice(0);
  307. srv.removeAllListeners("request");
  308. srv.on("request", (req, res) => {
  309. if (this.clientPathRegex.test(req.url)) {
  310. this.serve(req, res);
  311. }
  312. else {
  313. for (let i = 0; i < evs.length; i++) {
  314. evs[i].call(srv, req, res);
  315. }
  316. }
  317. });
  318. }
  319. /**
  320. * Handles a request serving of client source and map
  321. *
  322. * @param req
  323. * @param res
  324. * @private
  325. */
  326. serve(req, res) {
  327. const filename = req.url.replace(this._path, "").replace(/\?.*$/, "");
  328. const isMap = dotMapRegex.test(filename);
  329. const type = isMap ? "map" : "source";
  330. // Per the standard, ETags must be quoted:
  331. // https://tools.ietf.org/html/rfc7232#section-2.3
  332. const expectedEtag = '"' + clientVersion + '"';
  333. const weakEtag = "W/" + expectedEtag;
  334. const etag = req.headers["if-none-match"];
  335. if (etag) {
  336. if (expectedEtag === etag || weakEtag === etag) {
  337. debug("serve client %s 304", type);
  338. res.writeHead(304);
  339. res.end();
  340. return;
  341. }
  342. }
  343. debug("serve client %s", type);
  344. res.setHeader("Cache-Control", "public, max-age=0");
  345. res.setHeader("Content-Type", "application/" + (isMap ? "json" : "javascript") + "; charset=utf-8");
  346. res.setHeader("ETag", expectedEtag);
  347. Server.sendFile(filename, req, res);
  348. }
  349. /**
  350. * @param filename
  351. * @param req
  352. * @param res
  353. * @private
  354. */
  355. static sendFile(filename, req, res) {
  356. const readStream = (0, fs_1.createReadStream)(path.join(__dirname, "../client-dist/", filename));
  357. const encoding = accepts(req).encodings(["br", "gzip", "deflate"]);
  358. const onError = (err) => {
  359. if (err) {
  360. res.end();
  361. }
  362. };
  363. switch (encoding) {
  364. case "br":
  365. res.writeHead(200, { "content-encoding": "br" });
  366. readStream.pipe((0, zlib_1.createBrotliCompress)()).pipe(res);
  367. (0, stream_1.pipeline)(readStream, (0, zlib_1.createBrotliCompress)(), res, onError);
  368. break;
  369. case "gzip":
  370. res.writeHead(200, { "content-encoding": "gzip" });
  371. (0, stream_1.pipeline)(readStream, (0, zlib_1.createGzip)(), res, onError);
  372. break;
  373. case "deflate":
  374. res.writeHead(200, { "content-encoding": "deflate" });
  375. (0, stream_1.pipeline)(readStream, (0, zlib_1.createDeflate)(), res, onError);
  376. break;
  377. default:
  378. res.writeHead(200);
  379. (0, stream_1.pipeline)(readStream, res, onError);
  380. }
  381. }
  382. /**
  383. * Binds socket.io to an engine.io instance.
  384. *
  385. * @param engine engine.io (or compatible) server
  386. * @return self
  387. */
  388. bind(engine) {
  389. this.engine = engine;
  390. this.engine.on("connection", this.onconnection.bind(this));
  391. return this;
  392. }
  393. /**
  394. * Called with each incoming transport connection.
  395. *
  396. * @param {engine.Socket} conn
  397. * @return self
  398. * @private
  399. */
  400. onconnection(conn) {
  401. debug("incoming connection with id %s", conn.id);
  402. const client = new client_1.Client(this, conn);
  403. if (conn.protocol === 3) {
  404. // @ts-ignore
  405. client.connect("/");
  406. }
  407. return this;
  408. }
  409. /**
  410. * Looks up a namespace.
  411. *
  412. * @example
  413. * // with a simple string
  414. * const myNamespace = io.of("/my-namespace");
  415. *
  416. * // with a regex
  417. * const dynamicNsp = io.of(/^\/dynamic-\d+$/).on("connection", (socket) => {
  418. * const namespace = socket.nsp; // newNamespace.name === "/dynamic-101"
  419. *
  420. * // broadcast to all clients in the given sub-namespace
  421. * namespace.emit("hello");
  422. * });
  423. *
  424. * @param name - nsp name
  425. * @param fn optional, nsp `connection` ev handler
  426. */
  427. of(name, fn) {
  428. if (typeof name === "function" || name instanceof RegExp) {
  429. const parentNsp = new parent_namespace_1.ParentNamespace(this);
  430. debug("initializing parent namespace %s", parentNsp.name);
  431. if (typeof name === "function") {
  432. this.parentNsps.set(name, parentNsp);
  433. }
  434. else {
  435. this.parentNsps.set((nsp, conn, next) => next(null, name.test(nsp)), parentNsp);
  436. this.parentNamespacesFromRegExp.set(name, parentNsp);
  437. }
  438. if (fn) {
  439. // @ts-ignore
  440. parentNsp.on("connect", fn);
  441. }
  442. return parentNsp;
  443. }
  444. if (String(name)[0] !== "/")
  445. name = "/" + name;
  446. let nsp = this._nsps.get(name);
  447. if (!nsp) {
  448. for (const [regex, parentNamespace] of this.parentNamespacesFromRegExp) {
  449. if (regex.test(name)) {
  450. debug("attaching namespace %s to parent namespace %s", name, regex);
  451. return parentNamespace.createChild(name);
  452. }
  453. }
  454. debug("initializing namespace %s", name);
  455. nsp = new namespace_1.Namespace(this, name);
  456. this._nsps.set(name, nsp);
  457. if (name !== "/") {
  458. // @ts-ignore
  459. this.sockets.emitReserved("new_namespace", nsp);
  460. }
  461. }
  462. if (fn)
  463. nsp.on("connect", fn);
  464. return nsp;
  465. }
  466. /**
  467. * Closes server connection
  468. *
  469. * @param [fn] optional, called as `fn([err])` on error OR all conns closed
  470. */
  471. close(fn) {
  472. for (const socket of this.sockets.sockets.values()) {
  473. socket._onclose("server shutting down");
  474. }
  475. this.engine.close();
  476. // restore the Adapter prototype
  477. (0, uws_1.restoreAdapter)();
  478. if (this.httpServer) {
  479. this.httpServer.close(fn);
  480. }
  481. else {
  482. fn && fn();
  483. }
  484. }
  485. /**
  486. * Registers a middleware, which is a function that gets executed for every incoming {@link Socket}.
  487. *
  488. * @example
  489. * io.use((socket, next) => {
  490. * // ...
  491. * next();
  492. * });
  493. *
  494. * @param fn - the middleware function
  495. */
  496. use(fn) {
  497. this.sockets.use(fn);
  498. return this;
  499. }
  500. /**
  501. * Targets a room when emitting.
  502. *
  503. * @example
  504. * // the “foo” event will be broadcast to all connected clients in the “room-101” room
  505. * io.to("room-101").emit("foo", "bar");
  506. *
  507. * // with an array of rooms (a client will be notified at most once)
  508. * io.to(["room-101", "room-102"]).emit("foo", "bar");
  509. *
  510. * // with multiple chained calls
  511. * io.to("room-101").to("room-102").emit("foo", "bar");
  512. *
  513. * @param room - a room, or an array of rooms
  514. * @return a new {@link BroadcastOperator} instance for chaining
  515. */
  516. to(room) {
  517. return this.sockets.to(room);
  518. }
  519. /**
  520. * Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
  521. *
  522. * @example
  523. * // disconnect all clients in the "room-101" room
  524. * io.in("room-101").disconnectSockets();
  525. *
  526. * @param room - a room, or an array of rooms
  527. * @return a new {@link BroadcastOperator} instance for chaining
  528. */
  529. in(room) {
  530. return this.sockets.in(room);
  531. }
  532. /**
  533. * Excludes a room when emitting.
  534. *
  535. * @example
  536. * // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
  537. * io.except("room-101").emit("foo", "bar");
  538. *
  539. * // with an array of rooms
  540. * io.except(["room-101", "room-102"]).emit("foo", "bar");
  541. *
  542. * // with multiple chained calls
  543. * io.except("room-101").except("room-102").emit("foo", "bar");
  544. *
  545. * @param room - a room, or an array of rooms
  546. * @return a new {@link BroadcastOperator} instance for chaining
  547. */
  548. except(room) {
  549. return this.sockets.except(room);
  550. }
  551. /**
  552. * Emits an event and waits for an acknowledgement from all clients.
  553. *
  554. * @example
  555. * try {
  556. * const responses = await io.timeout(1000).emitWithAck("some-event");
  557. * console.log(responses); // one response per client
  558. * } catch (e) {
  559. * // some clients did not acknowledge the event in the given delay
  560. * }
  561. *
  562. * @return a Promise that will be fulfilled when all clients have acknowledged the event
  563. */
  564. emitWithAck(ev, ...args) {
  565. return this.sockets.emitWithAck(ev, ...args);
  566. }
  567. /**
  568. * Sends a `message` event to all clients.
  569. *
  570. * This method mimics the WebSocket.send() method.
  571. *
  572. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
  573. *
  574. * @example
  575. * io.send("hello");
  576. *
  577. * // this is equivalent to
  578. * io.emit("message", "hello");
  579. *
  580. * @return self
  581. */
  582. send(...args) {
  583. this.sockets.emit("message", ...args);
  584. return this;
  585. }
  586. /**
  587. * Sends a `message` event to all clients. Alias of {@link send}.
  588. *
  589. * @return self
  590. */
  591. write(...args) {
  592. this.sockets.emit("message", ...args);
  593. return this;
  594. }
  595. /**
  596. * Sends a message to the other Socket.IO servers of the cluster.
  597. *
  598. * @example
  599. * io.serverSideEmit("hello", "world");
  600. *
  601. * io.on("hello", (arg1) => {
  602. * console.log(arg1); // prints "world"
  603. * });
  604. *
  605. * // acknowledgements (without binary content) are supported too:
  606. * io.serverSideEmit("ping", (err, responses) => {
  607. * if (err) {
  608. * // some servers did not acknowledge the event in the given delay
  609. * } else {
  610. * console.log(responses); // one response per server (except the current one)
  611. * }
  612. * });
  613. *
  614. * io.on("ping", (cb) => {
  615. * cb("pong");
  616. * });
  617. *
  618. * @param ev - the event name
  619. * @param args - an array of arguments, which may include an acknowledgement callback at the end
  620. */
  621. serverSideEmit(ev, ...args) {
  622. return this.sockets.serverSideEmit(ev, ...args);
  623. }
  624. /**
  625. * Sends a message and expect an acknowledgement from the other Socket.IO servers of the cluster.
  626. *
  627. * @example
  628. * try {
  629. * const responses = await io.serverSideEmitWithAck("ping");
  630. * console.log(responses); // one response per server (except the current one)
  631. * } catch (e) {
  632. * // some servers did not acknowledge the event in the given delay
  633. * }
  634. *
  635. * @param ev - the event name
  636. * @param args - an array of arguments
  637. *
  638. * @return a Promise that will be fulfilled when all servers have acknowledged the event
  639. */
  640. serverSideEmitWithAck(ev, ...args) {
  641. return this.sockets.serverSideEmitWithAck(ev, ...args);
  642. }
  643. /**
  644. * Gets a list of socket ids.
  645. *
  646. * @deprecated this method will be removed in the next major release, please use {@link Server#serverSideEmit} or
  647. * {@link Server#fetchSockets} instead.
  648. */
  649. allSockets() {
  650. return this.sockets.allSockets();
  651. }
  652. /**
  653. * Sets the compress flag.
  654. *
  655. * @example
  656. * io.compress(false).emit("hello");
  657. *
  658. * @param compress - if `true`, compresses the sending data
  659. * @return a new {@link BroadcastOperator} instance for chaining
  660. */
  661. compress(compress) {
  662. return this.sockets.compress(compress);
  663. }
  664. /**
  665. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  666. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  667. * and is in the middle of a request-response cycle).
  668. *
  669. * @example
  670. * io.volatile.emit("hello"); // the clients may or may not receive it
  671. *
  672. * @return a new {@link BroadcastOperator} instance for chaining
  673. */
  674. get volatile() {
  675. return this.sockets.volatile;
  676. }
  677. /**
  678. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  679. *
  680. * @example
  681. * // the “foo” event will be broadcast to all connected clients on this node
  682. * io.local.emit("foo", "bar");
  683. *
  684. * @return a new {@link BroadcastOperator} instance for chaining
  685. */
  686. get local() {
  687. return this.sockets.local;
  688. }
  689. /**
  690. * Adds a timeout in milliseconds for the next operation.
  691. *
  692. * @example
  693. * io.timeout(1000).emit("some-event", (err, responses) => {
  694. * if (err) {
  695. * // some clients did not acknowledge the event in the given delay
  696. * } else {
  697. * console.log(responses); // one response per client
  698. * }
  699. * });
  700. *
  701. * @param timeout
  702. */
  703. timeout(timeout) {
  704. return this.sockets.timeout(timeout);
  705. }
  706. /**
  707. * Returns the matching socket instances.
  708. *
  709. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  710. *
  711. * @example
  712. * // return all Socket instances
  713. * const sockets = await io.fetchSockets();
  714. *
  715. * // return all Socket instances in the "room1" room
  716. * const sockets = await io.in("room1").fetchSockets();
  717. *
  718. * for (const socket of sockets) {
  719. * console.log(socket.id);
  720. * console.log(socket.handshake);
  721. * console.log(socket.rooms);
  722. * console.log(socket.data);
  723. *
  724. * socket.emit("hello");
  725. * socket.join("room1");
  726. * socket.leave("room2");
  727. * socket.disconnect();
  728. * }
  729. */
  730. fetchSockets() {
  731. return this.sockets.fetchSockets();
  732. }
  733. /**
  734. * Makes the matching socket instances join the specified rooms.
  735. *
  736. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  737. *
  738. * @example
  739. *
  740. * // make all socket instances join the "room1" room
  741. * io.socketsJoin("room1");
  742. *
  743. * // make all socket instances in the "room1" room join the "room2" and "room3" rooms
  744. * io.in("room1").socketsJoin(["room2", "room3"]);
  745. *
  746. * @param room - a room, or an array of rooms
  747. */
  748. socketsJoin(room) {
  749. return this.sockets.socketsJoin(room);
  750. }
  751. /**
  752. * Makes the matching socket instances leave the specified rooms.
  753. *
  754. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  755. *
  756. * @example
  757. * // make all socket instances leave the "room1" room
  758. * io.socketsLeave("room1");
  759. *
  760. * // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
  761. * io.in("room1").socketsLeave(["room2", "room3"]);
  762. *
  763. * @param room - a room, or an array of rooms
  764. */
  765. socketsLeave(room) {
  766. return this.sockets.socketsLeave(room);
  767. }
  768. /**
  769. * Makes the matching socket instances disconnect.
  770. *
  771. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  772. *
  773. * @example
  774. * // make all socket instances disconnect (the connections might be kept alive for other namespaces)
  775. * io.disconnectSockets();
  776. *
  777. * // make all socket instances in the "room1" room disconnect and close the underlying connections
  778. * io.in("room1").disconnectSockets(true);
  779. *
  780. * @param close - whether to close the underlying connection
  781. */
  782. disconnectSockets(close = false) {
  783. return this.sockets.disconnectSockets(close);
  784. }
  785. }
  786. exports.Server = Server;
  787. /**
  788. * Expose main namespace (/).
  789. */
  790. const emitterMethods = Object.keys(events_1.EventEmitter.prototype).filter(function (key) {
  791. return typeof events_1.EventEmitter.prototype[key] === "function";
  792. });
  793. emitterMethods.forEach(function (fn) {
  794. Server.prototype[fn] = function () {
  795. return this.sockets[fn].apply(this.sockets, arguments);
  796. };
  797. });
  798. module.exports = (srv, opts) => new Server(srv, opts);
  799. module.exports.Server = Server;
  800. module.exports.Namespace = namespace_1.Namespace;
  801. module.exports.Socket = socket_1.Socket;
  802. var socket_2 = require("./socket");