Node-Red configuration
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. (0, stream_1.pipeline)(readStream, (0, zlib_1.createBrotliCompress)(), res, onError);
  378. break;
  379. case "gzip":
  380. res.writeHead(200, { "content-encoding": "gzip" });
  381. (0, stream_1.pipeline)(readStream, (0, zlib_1.createGzip)(), res, onError);
  382. break;
  383. case "deflate":
  384. res.writeHead(200, { "content-encoding": "deflate" });
  385. (0, stream_1.pipeline)(readStream, (0, zlib_1.createDeflate)(), res, onError);
  386. break;
  387. default:
  388. res.writeHead(200);
  389. (0, stream_1.pipeline)(readStream, res, onError);
  390. }
  391. }
  392. /**
  393. * Binds socket.io to an engine.io instance.
  394. *
  395. * @param engine engine.io (or compatible) server
  396. * @return self
  397. */
  398. bind(engine) {
  399. this.engine = engine;
  400. this.engine.on("connection", this.onconnection.bind(this));
  401. return this;
  402. }
  403. /**
  404. * Called with each incoming transport connection.
  405. *
  406. * @param {engine.Socket} conn
  407. * @return self
  408. * @private
  409. */
  410. onconnection(conn) {
  411. debug("incoming connection with id %s", conn.id);
  412. const client = new client_1.Client(this, conn);
  413. if (conn.protocol === 3) {
  414. // @ts-ignore
  415. client.connect("/");
  416. }
  417. return this;
  418. }
  419. /**
  420. * Looks up a namespace.
  421. *
  422. * @example
  423. * // with a simple string
  424. * const myNamespace = io.of("/my-namespace");
  425. *
  426. * // with a regex
  427. * const dynamicNsp = io.of(/^\/dynamic-\d+$/).on("connection", (socket) => {
  428. * const namespace = socket.nsp; // newNamespace.name === "/dynamic-101"
  429. *
  430. * // broadcast to all clients in the given sub-namespace
  431. * namespace.emit("hello");
  432. * });
  433. *
  434. * @param name - nsp name
  435. * @param fn optional, nsp `connection` ev handler
  436. */
  437. of(name, fn) {
  438. if (typeof name === "function" || name instanceof RegExp) {
  439. const parentNsp = new parent_namespace_1.ParentNamespace(this);
  440. debug("initializing parent namespace %s", parentNsp.name);
  441. if (typeof name === "function") {
  442. this.parentNsps.set(name, parentNsp);
  443. }
  444. else {
  445. this.parentNsps.set((nsp, conn, next) => next(null, name.test(nsp)), parentNsp);
  446. this.parentNamespacesFromRegExp.set(name, parentNsp);
  447. }
  448. if (fn) {
  449. // @ts-ignore
  450. parentNsp.on("connect", fn);
  451. }
  452. return parentNsp;
  453. }
  454. if (String(name)[0] !== "/")
  455. name = "/" + name;
  456. let nsp = this._nsps.get(name);
  457. if (!nsp) {
  458. for (const [regex, parentNamespace] of this.parentNamespacesFromRegExp) {
  459. if (regex.test(name)) {
  460. debug("attaching namespace %s to parent namespace %s", name, regex);
  461. return parentNamespace.createChild(name);
  462. }
  463. }
  464. debug("initializing namespace %s", name);
  465. nsp = new namespace_1.Namespace(this, name);
  466. this._nsps.set(name, nsp);
  467. if (name !== "/") {
  468. // @ts-ignore
  469. this.sockets.emitReserved("new_namespace", nsp);
  470. }
  471. }
  472. if (fn)
  473. nsp.on("connect", fn);
  474. return nsp;
  475. }
  476. /**
  477. * Closes server connection
  478. *
  479. * @param [fn] optional, called as `fn([err])` on error OR all conns closed
  480. */
  481. close(fn) {
  482. this._nsps.forEach((nsp) => {
  483. nsp.sockets.forEach((socket) => {
  484. socket._onclose("server shutting down");
  485. });
  486. nsp.adapter.close();
  487. });
  488. this.engine.close();
  489. // restore the Adapter prototype, when the Socket.IO server was attached to a uWebSockets.js server
  490. (0, uws_1.restoreAdapter)();
  491. if (this.httpServer) {
  492. this.httpServer.close(fn);
  493. }
  494. else {
  495. fn && fn();
  496. }
  497. }
  498. /**
  499. * Registers a middleware, which is a function that gets executed for every incoming {@link Socket}.
  500. *
  501. * @example
  502. * io.use((socket, next) => {
  503. * // ...
  504. * next();
  505. * });
  506. *
  507. * @param fn - the middleware function
  508. */
  509. use(fn) {
  510. this.sockets.use(fn);
  511. return this;
  512. }
  513. /**
  514. * Targets a room when emitting.
  515. *
  516. * @example
  517. * // the “foo” event will be broadcast to all connected clients in the “room-101” room
  518. * io.to("room-101").emit("foo", "bar");
  519. *
  520. * // with an array of rooms (a client will be notified at most once)
  521. * io.to(["room-101", "room-102"]).emit("foo", "bar");
  522. *
  523. * // with multiple chained calls
  524. * io.to("room-101").to("room-102").emit("foo", "bar");
  525. *
  526. * @param room - a room, or an array of rooms
  527. * @return a new {@link BroadcastOperator} instance for chaining
  528. */
  529. to(room) {
  530. return this.sockets.to(room);
  531. }
  532. /**
  533. * Targets a room when emitting. Similar to `to()`, but might feel clearer in some cases:
  534. *
  535. * @example
  536. * // disconnect all clients in the "room-101" room
  537. * io.in("room-101").disconnectSockets();
  538. *
  539. * @param room - a room, or an array of rooms
  540. * @return a new {@link BroadcastOperator} instance for chaining
  541. */
  542. in(room) {
  543. return this.sockets.in(room);
  544. }
  545. /**
  546. * Excludes a room when emitting.
  547. *
  548. * @example
  549. * // the "foo" event will be broadcast to all connected clients, except the ones that are in the "room-101" room
  550. * io.except("room-101").emit("foo", "bar");
  551. *
  552. * // with an array of rooms
  553. * io.except(["room-101", "room-102"]).emit("foo", "bar");
  554. *
  555. * // with multiple chained calls
  556. * io.except("room-101").except("room-102").emit("foo", "bar");
  557. *
  558. * @param room - a room, or an array of rooms
  559. * @return a new {@link BroadcastOperator} instance for chaining
  560. */
  561. except(room) {
  562. return this.sockets.except(room);
  563. }
  564. /**
  565. * Sends a `message` event to all clients.
  566. *
  567. * This method mimics the WebSocket.send() method.
  568. *
  569. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
  570. *
  571. * @example
  572. * io.send("hello");
  573. *
  574. * // this is equivalent to
  575. * io.emit("message", "hello");
  576. *
  577. * @return self
  578. */
  579. send(...args) {
  580. // This type-cast is needed because EmitEvents likely doesn't have `message` as a key.
  581. // if you specify the EmitEvents, the type of args will be never.
  582. this.sockets.emit("message", ...args);
  583. return this;
  584. }
  585. /**
  586. * Sends a `message` event to all clients. Alias of {@link send}.
  587. *
  588. * @return self
  589. */
  590. write(...args) {
  591. // This type-cast is needed because EmitEvents likely doesn't have `message` as a key.
  592. // if you specify the EmitEvents, the type of args will be never.
  593. this.sockets.emit("message", ...args);
  594. return this;
  595. }
  596. /**
  597. * Sends a message to the other Socket.IO servers of the cluster.
  598. *
  599. * @example
  600. * io.serverSideEmit("hello", "world");
  601. *
  602. * io.on("hello", (arg1) => {
  603. * console.log(arg1); // prints "world"
  604. * });
  605. *
  606. * // acknowledgements (without binary content) are supported too:
  607. * io.serverSideEmit("ping", (err, responses) => {
  608. * if (err) {
  609. * // some servers did not acknowledge the event in the given delay
  610. * } else {
  611. * console.log(responses); // one response per server (except the current one)
  612. * }
  613. * });
  614. *
  615. * io.on("ping", (cb) => {
  616. * cb("pong");
  617. * });
  618. *
  619. * @param ev - the event name
  620. * @param args - an array of arguments, which may include an acknowledgement callback at the end
  621. */
  622. serverSideEmit(ev, ...args) {
  623. return this.sockets.serverSideEmit(ev, ...args);
  624. }
  625. /**
  626. * Sends a message and expect an acknowledgement from the other Socket.IO servers of the cluster.
  627. *
  628. * @example
  629. * try {
  630. * const responses = await io.serverSideEmitWithAck("ping");
  631. * console.log(responses); // one response per server (except the current one)
  632. * } catch (e) {
  633. * // some servers did not acknowledge the event in the given delay
  634. * }
  635. *
  636. * @param ev - the event name
  637. * @param args - an array of arguments
  638. *
  639. * @return a Promise that will be fulfilled when all servers have acknowledged the event
  640. */
  641. serverSideEmitWithAck(ev, ...args) {
  642. return this.sockets.serverSideEmitWithAck(ev, ...args);
  643. }
  644. /**
  645. * Gets a list of socket ids.
  646. *
  647. * @deprecated this method will be removed in the next major release, please use {@link Server#serverSideEmit} or
  648. * {@link Server#fetchSockets} instead.
  649. */
  650. allSockets() {
  651. return this.sockets.allSockets();
  652. }
  653. /**
  654. * Sets the compress flag.
  655. *
  656. * @example
  657. * io.compress(false).emit("hello");
  658. *
  659. * @param compress - if `true`, compresses the sending data
  660. * @return a new {@link BroadcastOperator} instance for chaining
  661. */
  662. compress(compress) {
  663. return this.sockets.compress(compress);
  664. }
  665. /**
  666. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  667. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  668. * and is in the middle of a request-response cycle).
  669. *
  670. * @example
  671. * io.volatile.emit("hello"); // the clients may or may not receive it
  672. *
  673. * @return a new {@link BroadcastOperator} instance for chaining
  674. */
  675. get volatile() {
  676. return this.sockets.volatile;
  677. }
  678. /**
  679. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  680. *
  681. * @example
  682. * // the “foo” event will be broadcast to all connected clients on this node
  683. * io.local.emit("foo", "bar");
  684. *
  685. * @return a new {@link BroadcastOperator} instance for chaining
  686. */
  687. get local() {
  688. return this.sockets.local;
  689. }
  690. /**
  691. * Adds a timeout in milliseconds for the next operation.
  692. *
  693. * @example
  694. * io.timeout(1000).emit("some-event", (err, responses) => {
  695. * if (err) {
  696. * // some clients did not acknowledge the event in the given delay
  697. * } else {
  698. * console.log(responses); // one response per client
  699. * }
  700. * });
  701. *
  702. * @param timeout
  703. */
  704. timeout(timeout) {
  705. return this.sockets.timeout(timeout);
  706. }
  707. /**
  708. * Returns the matching socket instances.
  709. *
  710. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  711. *
  712. * @example
  713. * // return all Socket instances
  714. * const sockets = await io.fetchSockets();
  715. *
  716. * // return all Socket instances in the "room1" room
  717. * const sockets = await io.in("room1").fetchSockets();
  718. *
  719. * for (const socket of sockets) {
  720. * console.log(socket.id);
  721. * console.log(socket.handshake);
  722. * console.log(socket.rooms);
  723. * console.log(socket.data);
  724. *
  725. * socket.emit("hello");
  726. * socket.join("room1");
  727. * socket.leave("room2");
  728. * socket.disconnect();
  729. * }
  730. */
  731. fetchSockets() {
  732. return this.sockets.fetchSockets();
  733. }
  734. /**
  735. * Makes the matching socket instances join the specified rooms.
  736. *
  737. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  738. *
  739. * @example
  740. *
  741. * // make all socket instances join the "room1" room
  742. * io.socketsJoin("room1");
  743. *
  744. * // make all socket instances in the "room1" room join the "room2" and "room3" rooms
  745. * io.in("room1").socketsJoin(["room2", "room3"]);
  746. *
  747. * @param room - a room, or an array of rooms
  748. */
  749. socketsJoin(room) {
  750. return this.sockets.socketsJoin(room);
  751. }
  752. /**
  753. * Makes the matching socket instances leave the specified rooms.
  754. *
  755. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  756. *
  757. * @example
  758. * // make all socket instances leave the "room1" room
  759. * io.socketsLeave("room1");
  760. *
  761. * // make all socket instances in the "room1" room leave the "room2" and "room3" rooms
  762. * io.in("room1").socketsLeave(["room2", "room3"]);
  763. *
  764. * @param room - a room, or an array of rooms
  765. */
  766. socketsLeave(room) {
  767. return this.sockets.socketsLeave(room);
  768. }
  769. /**
  770. * Makes the matching socket instances disconnect.
  771. *
  772. * Note: this method also works within a cluster of multiple Socket.IO servers, with a compatible {@link Adapter}.
  773. *
  774. * @example
  775. * // make all socket instances disconnect (the connections might be kept alive for other namespaces)
  776. * io.disconnectSockets();
  777. *
  778. * // make all socket instances in the "room1" room disconnect and close the underlying connections
  779. * io.in("room1").disconnectSockets(true);
  780. *
  781. * @param close - whether to close the underlying connection
  782. */
  783. disconnectSockets(close = false) {
  784. return this.sockets.disconnectSockets(close);
  785. }
  786. }
  787. exports.Server = Server;
  788. /**
  789. * Expose main namespace (/).
  790. */
  791. const emitterMethods = Object.keys(events_1.EventEmitter.prototype).filter(function (key) {
  792. return typeof events_1.EventEmitter.prototype[key] === "function";
  793. });
  794. emitterMethods.forEach(function (fn) {
  795. Server.prototype[fn] = function () {
  796. return this.sockets[fn].apply(this.sockets, arguments);
  797. };
  798. });
  799. module.exports = (srv, opts) => new Server(srv, opts);
  800. module.exports.Server = Server;
  801. module.exports.Namespace = namespace_1.Namespace;
  802. module.exports.Socket = socket_1.Socket;
  803. var socket_2 = require("./socket");