diff --git a/packages/socket.io/lib/namespace.ts b/packages/socket.io/lib/namespace.ts index d7ae1308fe..245551e61b 100644 --- a/packages/socket.io/lib/namespace.ts +++ b/packages/socket.io/lib/namespace.ts @@ -161,7 +161,7 @@ export class Namespace< SocketData >; - protected _fns: Array< + private _fns: Array< ( socket: Socket, next: (err?: ExtendedError) => void, @@ -187,6 +187,23 @@ export class Namespace< this._initAdapter(); } + protected static copyMiddleware< + ListenEvents extends EventsMap, + EmitEvents extends EventsMap, + ServerSideEvents extends EventsMap, + SocketData + >( + destination: Namespace< + ListenEvents, + EmitEvents, + ServerSideEvents, + SocketData + >, + source: Namespace + ) { + source._fns.forEach((fn) => destination.use(fn)); + } + /** * Initializes the `Adapter` for this nsp. * Run upon changing adapter by `Server#adapter` diff --git a/packages/socket.io/lib/parent-namespace.ts b/packages/socket.io/lib/parent-namespace.ts index 6139b8e969..43ac6be350 100644 --- a/packages/socket.io/lib/parent-namespace.ts +++ b/packages/socket.io/lib/parent-namespace.ts @@ -67,7 +67,7 @@ export class ParentNamespace< ): Namespace { debug("creating child namespace %s", name); const namespace = new Namespace(this.server, name); - this._fns.forEach((fn) => namespace.use(fn)); + Namespace.copyMiddleware(namespace, this); this.listeners("connect").forEach((listener) => namespace.on("connect", listener), );