index.js 27 KB

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