Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ParentNamespace adapter broadcast bug #5009

Merged
merged 2 commits into from Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions lib/parent-namespace.ts
Expand Up @@ -48,7 +48,7 @@ export class ParentNamespace<
* @private
*/
_initAdapter(): void {
this.adapter = new ParentBroadcastAdapter(this, this.children);
this.adapter = new ParentBroadcastAdapter(this);
}

public emit<Ev extends EventNamesWithoutAck<EmitEvents>>(
Expand Down Expand Up @@ -113,12 +113,8 @@ export class ParentNamespace<
* @private file
*/
class ParentBroadcastAdapter extends Adapter {
constructor(parentNsp: any, private readonly children: Set<Namespace>) {
super(parentNsp);
}

broadcast(packet: any, opts: BroadcastOptions) {
this.children.forEach((nsp) => {
this.nsp.children.forEach((nsp) => {
nsp.adapter.broadcast(packet, opts);
});
}
Expand Down
31 changes: 31 additions & 0 deletions test/namespaces.ts
Expand Up @@ -525,6 +525,37 @@ describe("namespaces", () => {
});
});

it("should allow connections to dynamic namespaces with a regex and emit in a room", (done) => {
const io = new Server(0);
const socket = createClient(io, "/dynamic-101");
const partialDone = createPartialDone(4, successFn(done, io, socket));

let dynamicNsp = io
.of(/^\/dynamic-\d+$/)
.on("connect", (socket) => {
expect(socket.nsp.name).to.be("/dynamic-101");
socket.join("some-room");
dynamicNsp.to("some-room").emit("hello", 4, "3", { 2: "1" });
partialDone();
})
.use((socket, next) => {
next();
partialDone();
});
socket.on("connect_error", (err) => {
expect().fail();
});
socket.on("connect", () => {
partialDone();
});
socket.on("hello", (a, b, c) => {
expect(a).to.eql(4);
expect(b).to.eql("3");
expect(c).to.eql({ 2: "1" });
partialDone();
});
});

it("should allow connections to dynamic namespaces with a function", (done) => {
const io = new Server(0);
const socket = createClient(io, "/dynamic-101");
Expand Down