Skip to content

Commit 1f106e0

Browse files
author
Andrea Cosentino
committed
fix: ws broadcast all rooms
1 parent f034fdf commit 1f106e0

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

src/models/webSocket.model.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,16 @@ export interface IWebSocketConnection {
159159
broadcast(data: any, options?: { room?: string; includeSelf?: boolean }): void;
160160

161161
/**
162-
* Broadcasts a message to all connections in all rooms that this connection is a member of.
163-
* If not in any rooms, broadcasts globally.
162+
* Broadcasts a message to all connections that share at least one room with
163+
* this connection.
164+
*
165+
* Each room this connection belongs to is iterated and the message is sent
166+
* to every other member of that room (deduplication is handled at the
167+
* manager level). If the connection has not joined any room, this method is
168+
* a no-op — it does **not** fall back to a global broadcast.
164169
*
165170
* @param data - The data to broadcast
166-
* @param includeSelf - Whether to include the current connection
171+
* @param includeSelf - Whether to include the current connection in the broadcast
167172
*/
168173
broadcastAllRooms(data: any, includeSelf?: boolean): void;
169174

src/utils/WebSocket.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,20 @@ export class WebSocketConnection implements IWebSocketConnection {
138138
}
139139

140140
broadcastAllRooms(data: any, includeSelf = false): void {
141-
if (this.rooms.size === 0) {
142-
this.manager.broadcast(data, { excludeId: includeSelf ? undefined : this.id });
143-
} else {
144-
this.rooms.forEach(room => {
145-
this.manager.broadcast(data, { room, excludeId: includeSelf ? undefined : this.id });
146-
});
147-
}
141+
this.rooms.forEach(room => {
142+
this.manager.broadcast(data, { room, excludeId: includeSelf ? undefined : this.id });
143+
});
148144
}
149145

150146
ping(payload?: string | Buffer) {
151-
if (this._closed){
147+
if (this._closed) {
152148
return;
153149
}
154150
this.ws.ping(payload);
155151
}
156152

157153
pong(payload?: string | Buffer) {
158-
if (this._closed){
154+
if (this._closed) {
159155
return;
160156
}
161157
this.ws.pong(payload);

0 commit comments

Comments
 (0)