From 94e27cd072c8a4eeb9636f6ffbb7a21d382f36b0 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Sat, 10 Jul 2021 11:48:46 +0200 Subject: [PATCH] fix: fix io.except() method Previously, calling `io.except("theroom").emit(...)` did not exclude the sockets in the given room. This method was forgotten in [1]. [1]: https://github.com/socketio/socket.io/commit/ac9e8ca6c71e00d4af45ee03f590fe56f3951186 --- lib/index.ts | 7 ++----- test/socket.io.ts | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 6f9f882ad6..c7b04d03cd 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -700,11 +700,8 @@ export class Server< * @return self * @public */ - public except( - name: Room | Room[] - ): Server { - this.sockets.except(name); - return this; + public except(name: Room | Room[]): BroadcastOperator { + return this.sockets.except(name); } /** diff --git a/test/socket.io.ts b/test/socket.io.ts index 87c36693fb..7860405a88 100644 --- a/test/socket.io.ts +++ b/test/socket.io.ts @@ -836,6 +836,27 @@ describe("socket.io", () => { }); it("should exclude a specific socket when emitting", (done) => { + const srv = createServer(); + const io = new Server(srv); + + srv.listen(() => { + const socket1 = client(srv, "/"); + const socket2 = client(srv, "/"); + + socket2.on("a", () => { + done(new Error("should not happen")); + }); + socket1.on("a", () => { + done(); + }); + + socket2.on("connect", () => { + io.except(socket2.id).emit("a"); + }); + }); + }); + + it("should exclude a specific socket when emitting (in a namespace)", (done) => { const srv = createServer(); const sio = new Server(srv);