* Looks up a namespace. * * @example * // with a simple string * const myNamespace = io.of("/my-namespace"); * * // with a regex * const dynamicNsp = io.of(/^\/dynamic-\d+$/).on("connection", (socket) => { * const namespace = socket.nsp; // newNamespace.name === "/dynamic-1
(
name: string | RegExp | ParentNspNameMatchFn,
fn?: (
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>,
) => void,
)
| 767 | * @param fn optional, nsp `connection` ev handler |
| 768 | */ |
| 769 | public of( |
| 770 | name: string | RegExp | ParentNspNameMatchFn, |
| 771 | fn?: ( |
| 772 | socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>, |
| 773 | ) => void, |
| 774 | ): Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData> { |
| 775 | if (typeof name === class="st">"function" || name instanceof RegExp) { |
| 776 | const parentNsp = new ParentNamespace(this); |
| 777 | debug(class="st">"initializing parent namespace %s", parentNsp.name); |
| 778 | if (typeof name === class="st">"function") { |
| 779 | this.parentNsps.set(name, parentNsp); |
| 780 | } else { |
| 781 | this.parentNsps.set( |
| 782 | (nsp, conn, next) => next(null, (name as RegExp).test(nsp)), |
| 783 | parentNsp, |
| 784 | ); |
| 785 | this.parentNamespacesFromRegExp.set(name, parentNsp); |
| 786 | } |
| 787 | if (fn) { |
| 788 | class="cm">// @ts-ignore |
| 789 | parentNsp.on(class="st">"connect", fn); |
| 790 | } |
| 791 | return parentNsp; |
| 792 | } |
| 793 | |
| 794 | if (String(name)[0] !== class="st">"/") name = class="st">"/" + name; |
| 795 | |
| 796 | let nsp = this._nsps.get(name); |
| 797 | if (!nsp) { |
| 798 | for (const [regex, parentNamespace] of this.parentNamespacesFromRegExp) { |
| 799 | if (regex.test(name as string)) { |
| 800 | debug(class="st">"attaching namespace %s to parent namespace %s", name, regex); |
| 801 | return parentNamespace.createChild(name as string); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | debug(class="st">"initializing namespace %s", name); |
| 806 | nsp = new Namespace(this, name); |
| 807 | this._nsps.set(name, nsp); |
| 808 | if (name !== class="st">"/") { |
| 809 | class="cm">// @ts-ignore |
| 810 | this.sockets.emitReserved(class="st">"new_namespace", nsp); |
| 811 | } |
| 812 | } |
| 813 | if (fn) nsp.on(class="st">"connect", fn); |
| 814 | return nsp; |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * Closes server connection |
no test coverage detected