parent-namespace.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ParentNamespace = void 0;
  4. const namespace_1 = require("./namespace");
  5. class ParentNamespace extends namespace_1.Namespace {
  6. constructor(server) {
  7. super(server, "/_" + ParentNamespace.count++);
  8. this.children = new Set();
  9. }
  10. /**
  11. * @private
  12. */
  13. _initAdapter() {
  14. const broadcast = (packet, opts) => {
  15. this.children.forEach((nsp) => {
  16. nsp.adapter.broadcast(packet, opts);
  17. });
  18. };
  19. // @ts-ignore FIXME is there a way to declare an inner class in TypeScript?
  20. this.adapter = { broadcast };
  21. }
  22. emit(ev, ...args) {
  23. this.children.forEach((nsp) => {
  24. nsp.emit(ev, ...args);
  25. });
  26. return true;
  27. }
  28. createChild(name) {
  29. const namespace = new namespace_1.Namespace(this.server, name);
  30. namespace._fns = this._fns.slice(0);
  31. this.listeners("connect").forEach((listener) => namespace.on("connect", listener));
  32. this.listeners("connection").forEach((listener) => namespace.on("connection", listener));
  33. this.children.add(namespace);
  34. this.server._nsps.set(name, namespace);
  35. return namespace;
  36. }
  37. fetchSockets() {
  38. // note: we could make the fetchSockets() method work for dynamic namespaces created with a regex (by sending the
  39. // regex to the other Socket.IO servers, and returning the sockets of each matching namespace for example), but
  40. // the behavior for namespaces created with a function is less clear
  41. // note²: we cannot loop over each children namespace, because with multiple Socket.IO servers, a given namespace
  42. // may exist on one node but not exist on another (since it is created upon client connection)
  43. throw new Error("fetchSockets() is not supported on parent namespaces");
  44. }
  45. }
  46. exports.ParentNamespace = ParentNamespace;
  47. ParentNamespace.count = 0;