Skip to content

Commit 985bb41

Browse files
feat: allow excluding all sockets in a room (#66)
1 parent c95ab42 commit 985bb41

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

lib/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,32 @@ export class Adapter extends EventEmitter {
126126
*/
127127
public broadcast(packet: any, opts: BroadcastOptions): void {
128128
const rooms = opts.rooms;
129-
const except = opts.except || new Set();
130129
const flags = opts.flags || {};
131130
const packetOpts = {
132131
preEncoded: true,
133132
volatile: flags.volatile,
134133
compress: flags.compress
135134
};
136135
const ids = new Set();
136+
let except = opts.except || new Set();
137137

138138
packet.nsp = this.nsp.name;
139139
const encodedPackets = this.encoder.encode(packet);
140140

141+
// Allow ids in `except` to be room ids.
142+
if (except.size > 0) {
143+
const exclude = except;
144+
except = new Set(except);
145+
for (const id of exclude) {
146+
if (!this.rooms.has(id)) continue;
147+
for (const sid of this.rooms.get(id)) {
148+
if (sid !== id) {
149+
except.add(sid);
150+
}
151+
}
152+
}
153+
}
154+
141155
if (rooms.size) {
142156
for (const room of rooms) {
143157
if (!this.rooms.has(room)) continue;

test/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,72 @@ describe("socket.io-adapter", () => {
5858
expect(adapter.socketRooms("s4")).to.be(undefined);
5959
});
6060

61+
it("should exclude sockets in specific rooms when broadcasting", () => {
62+
let ids = [];
63+
function socket(id) {
64+
return [
65+
id,
66+
{
67+
id,
68+
packet() {
69+
ids.push(id);
70+
}
71+
}
72+
];
73+
}
74+
const nsp = {
75+
server: {
76+
encoder: {
77+
encode() {}
78+
}
79+
},
80+
sockets: new Map([socket("s1"), socket("s2"), socket("s3")])
81+
};
82+
const adapter = new Adapter(nsp);
83+
adapter.addAll("s1", new Set(["r1"]));
84+
adapter.addAll("s2", new Set());
85+
adapter.addAll("s3", new Set(["r1"]));
86+
87+
adapter.broadcast([], {
88+
rooms: new Set(),
89+
except: new Set(["r1"])
90+
});
91+
expect(ids).to.eql(["s2"]);
92+
});
93+
94+
it("should exclude sockets in specific rooms when broadcasting to rooms", () => {
95+
let ids = [];
96+
function socket(id) {
97+
return [
98+
id,
99+
{
100+
id,
101+
packet() {
102+
ids.push(id);
103+
}
104+
}
105+
];
106+
}
107+
const nsp = {
108+
server: {
109+
encoder: {
110+
encode() {}
111+
}
112+
},
113+
sockets: new Map([socket("s1"), socket("s2"), socket("s3")])
114+
};
115+
const adapter = new Adapter(nsp);
116+
adapter.addAll("s1", new Set(["r1", "r2"]));
117+
adapter.addAll("s2", new Set(["r2"]));
118+
adapter.addAll("s3", new Set(["r1"]));
119+
120+
adapter.broadcast([], {
121+
rooms: new Set(["r1"]),
122+
except: new Set(["r2"])
123+
});
124+
expect(ids).to.eql(["s3"]);
125+
});
126+
61127
describe("events", () => {
62128
it("should emit a 'create-room' event", done => {
63129
const adapter = new Adapter({ server: { encoder: null } });

0 commit comments

Comments
 (0)