Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 25KB

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