@@ -125,53 +125,19 @@ export class Adapter extends EventEmitter {
125
125
* @public
126
126
*/
127
127
public broadcast ( packet : any , opts : BroadcastOptions ) : void {
128
- const rooms = opts . rooms ;
129
128
const flags = opts . flags || { } ;
130
129
const packetOpts = {
131
130
preEncoded : true ,
132
131
volatile : flags . volatile ,
133
132
compress : flags . compress
134
133
} ;
135
- const ids = new Set ( ) ;
136
- let except = opts . except || new Set ( ) ;
137
134
138
135
packet . nsp = this . nsp . name ;
139
136
const encodedPackets = this . encoder . encode ( packet ) ;
140
137
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
-
155
- if ( rooms . size ) {
156
- for ( const room of rooms ) {
157
- if ( ! this . rooms . has ( room ) ) continue ;
158
-
159
- for ( const id of this . rooms . get ( room ) ) {
160
- if ( ids . has ( id ) || except . has ( id ) ) continue ;
161
- const socket = this . nsp . sockets . get ( id ) ;
162
- if ( socket ) {
163
- socket . packet ( encodedPackets , packetOpts ) ;
164
- ids . add ( id ) ;
165
- }
166
- }
167
- }
168
- } else {
169
- for ( const [ id ] of this . sids ) {
170
- if ( except . has ( id ) ) continue ;
171
- const socket = this . nsp . sockets . get ( id ) ;
172
- if ( socket ) socket . packet ( encodedPackets , packetOpts ) ;
173
- }
174
- }
138
+ this . apply ( opts , socket => {
139
+ socket . packet ( encodedPackets , packetOpts ) ;
140
+ } ) ;
175
141
}
176
142
177
143
/**
@@ -182,31 +148,109 @@ export class Adapter extends EventEmitter {
182
148
public sockets ( rooms : Set < Room > ) : Promise < Set < SocketId > > {
183
149
const sids = new Set < SocketId > ( ) ;
184
150
151
+ this . apply ( { rooms } , socket => {
152
+ sids . add ( socket . id ) ;
153
+ } ) ;
154
+
155
+ return Promise . resolve ( sids ) ;
156
+ }
157
+
158
+ /**
159
+ * Gets the list of rooms a given socket has joined.
160
+ *
161
+ * @param {SocketId } id the socket id
162
+ */
163
+ public socketRooms ( id : SocketId ) : Set < Room > | undefined {
164
+ return this . sids . get ( id ) ;
165
+ }
166
+
167
+ /**
168
+ * Returns the matching socket instances
169
+ *
170
+ * @param opts - the filters to apply
171
+ */
172
+ public fetchSockets ( opts : BroadcastOptions ) : Promise < any [ ] > {
173
+ const sockets = [ ] ;
174
+
175
+ this . apply ( opts , socket => {
176
+ sockets . push ( socket ) ;
177
+ } ) ;
178
+
179
+ return Promise . resolve ( sockets ) ;
180
+ }
181
+
182
+ /**
183
+ * Makes the matching socket instances join the specified rooms
184
+ *
185
+ * @param opts - the filters to apply
186
+ * @param rooms - the rooms to join
187
+ */
188
+ public addSockets ( opts : BroadcastOptions , rooms : Room [ ] ) : void {
189
+ this . apply ( opts , socket => {
190
+ socket . join ( rooms ) ;
191
+ } ) ;
192
+ }
193
+
194
+ /**
195
+ * Makes the matching socket instances leave the specified rooms
196
+ *
197
+ * @param opts - the filters to apply
198
+ * @param rooms - the rooms to leave
199
+ */
200
+ public delSockets ( opts : BroadcastOptions , rooms : Room [ ] ) : void {
201
+ this . apply ( opts , socket => {
202
+ rooms . forEach ( room => socket . leave ( room ) ) ;
203
+ } ) ;
204
+ }
205
+
206
+ /**
207
+ * Makes the matching socket instances disconnect
208
+ *
209
+ * @param opts - the filters to apply
210
+ * @param close - whether to close the underlying connection
211
+ */
212
+ public disconnectSockets ( opts : BroadcastOptions , close : boolean ) : void {
213
+ this . apply ( opts , socket => {
214
+ socket . disconnect ( close ) ;
215
+ } ) ;
216
+ }
217
+
218
+ private apply ( opts : BroadcastOptions , callback : ( socket ) => void ) : void {
219
+ const rooms = opts . rooms ;
220
+ const except = this . computeExceptSids ( opts . except ) ;
221
+
185
222
if ( rooms . size ) {
223
+ const ids = new Set ( ) ;
186
224
for ( const room of rooms ) {
187
225
if ( ! this . rooms . has ( room ) ) continue ;
188
226
189
227
for ( const id of this . rooms . get ( room ) ) {
190
- if ( this . nsp . sockets . has ( id ) ) {
191
- sids . add ( id ) ;
228
+ if ( ids . has ( id ) || except . has ( id ) ) continue ;
229
+ const socket = this . nsp . sockets . get ( id ) ;
230
+ if ( socket ) {
231
+ callback ( socket ) ;
232
+ ids . add ( id ) ;
192
233
}
193
234
}
194
235
}
195
236
} else {
196
237
for ( const [ id ] of this . sids ) {
197
- if ( this . nsp . sockets . has ( id ) ) sids . add ( id ) ;
238
+ if ( except . has ( id ) ) continue ;
239
+ const socket = this . nsp . sockets . get ( id ) ;
240
+ if ( socket ) callback ( socket ) ;
198
241
}
199
242
}
200
-
201
- return Promise . resolve ( sids ) ;
202
243
}
203
244
204
- /**
205
- * Gets the list of rooms a given socket has joined.
206
- *
207
- * @param {SocketId } id the socket id
208
- */
209
- public socketRooms ( id : SocketId ) : Set < Room > | undefined {
210
- return this . sids . get ( id ) ;
245
+ private computeExceptSids ( exceptRooms ?: Set < Room > ) {
246
+ const exceptSids = new Set ( ) ;
247
+ if ( exceptRooms && exceptRooms . size > 0 ) {
248
+ for ( const room of exceptRooms ) {
249
+ if ( this . rooms . has ( room ) ) {
250
+ this . rooms . get ( room ) . forEach ( sid => exceptSids . add ( sid ) ) ;
251
+ }
252
+ }
253
+ }
254
+ return exceptSids ;
211
255
}
212
256
}
0 commit comments