|
1 | 1 | import type { BroadcastFlags, Room, SocketId } from "socket.io-adapter";
|
2 |
| -import { RESERVED_EVENTS } from "./socket"; |
| 2 | +import { Handshake, RESERVED_EVENTS, Socket } from "./socket"; |
3 | 3 | import { PacketType } from "socket.io-parser";
|
4 | 4 | import type { Adapter } from "socket.io-adapter";
|
5 | 5 |
|
@@ -160,4 +160,142 @@ export class BroadcastOperator {
|
160 | 160 | }
|
161 | 161 | return this.adapter.sockets(this.rooms);
|
162 | 162 | }
|
| 163 | + |
| 164 | + /** |
| 165 | + * Returns the matching socket instances |
| 166 | + * |
| 167 | + * @public |
| 168 | + */ |
| 169 | + public fetchSockets(): Promise<RemoteSocket[]> { |
| 170 | + return this.adapter |
| 171 | + .fetchSockets({ |
| 172 | + rooms: this.rooms, |
| 173 | + except: this.exceptRooms, |
| 174 | + }) |
| 175 | + .then((sockets) => { |
| 176 | + return sockets.map((socket) => { |
| 177 | + if (socket instanceof Socket) { |
| 178 | + // FIXME the TypeScript compiler complains about missing private properties |
| 179 | + return (socket as unknown) as RemoteSocket; |
| 180 | + } else { |
| 181 | + return new RemoteSocket(this.adapter, socket as SocketDetails); |
| 182 | + } |
| 183 | + }); |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Makes the matching socket instances join the specified rooms |
| 189 | + * |
| 190 | + * @param room |
| 191 | + * @public |
| 192 | + */ |
| 193 | + public socketsJoin(room: Room | Room[]): void { |
| 194 | + this.adapter.addSockets( |
| 195 | + { |
| 196 | + rooms: this.rooms, |
| 197 | + except: this.exceptRooms, |
| 198 | + }, |
| 199 | + Array.isArray(room) ? room : [room] |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Makes the matching socket instances leave the specified rooms |
| 205 | + * |
| 206 | + * @param room |
| 207 | + * @public |
| 208 | + */ |
| 209 | + public socketsLeave(room: Room | Room[]): void { |
| 210 | + this.adapter.delSockets( |
| 211 | + { |
| 212 | + rooms: this.rooms, |
| 213 | + except: this.exceptRooms, |
| 214 | + }, |
| 215 | + Array.isArray(room) ? room : [room] |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Makes the matching socket instances disconnect |
| 221 | + * |
| 222 | + * @param close - whether to close the underlying connection |
| 223 | + * @public |
| 224 | + */ |
| 225 | + public disconnectSockets(close: boolean = false): void { |
| 226 | + this.adapter.disconnectSockets( |
| 227 | + { |
| 228 | + rooms: this.rooms, |
| 229 | + except: this.exceptRooms, |
| 230 | + }, |
| 231 | + close |
| 232 | + ); |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +/** |
| 237 | + * Format of the data when the Socket instance exists on another Socket.IO server |
| 238 | + */ |
| 239 | +interface SocketDetails { |
| 240 | + id: SocketId; |
| 241 | + handshake: Handshake; |
| 242 | + rooms: Room[]; |
| 243 | + data: any; |
| 244 | +} |
| 245 | + |
| 246 | +/** |
| 247 | + * Expose of subset of the attributes and methods of the Socket class |
| 248 | + */ |
| 249 | +export class RemoteSocket { |
| 250 | + public readonly id: SocketId; |
| 251 | + public readonly handshake: Handshake; |
| 252 | + public readonly rooms: Set<Room>; |
| 253 | + public readonly data: any; |
| 254 | + |
| 255 | + private readonly operator: BroadcastOperator; |
| 256 | + |
| 257 | + constructor(adapter: Adapter, details: SocketDetails) { |
| 258 | + this.id = details.id; |
| 259 | + this.handshake = details.handshake; |
| 260 | + this.rooms = new Set(details.rooms); |
| 261 | + this.data = details.data; |
| 262 | + this.operator = new BroadcastOperator(adapter, new Set([this.id])); |
| 263 | + } |
| 264 | + |
| 265 | + public emit(ev: string, ...args: any[]): boolean { |
| 266 | + return this.operator.emit(ev, ...args); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Joins a room. |
| 271 | + * |
| 272 | + * @param {String|Array} room - room or array of rooms |
| 273 | + * @public |
| 274 | + */ |
| 275 | + public join(room: Room | Room[]): void { |
| 276 | + return this.operator.socketsJoin(room); |
| 277 | + } |
| 278 | + |
| 279 | + /** |
| 280 | + * Leaves a room. |
| 281 | + * |
| 282 | + * @param {String} room |
| 283 | + * @public |
| 284 | + */ |
| 285 | + public leave(room: Room): void { |
| 286 | + return this.operator.socketsLeave(room); |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * Disconnects this client. |
| 291 | + * |
| 292 | + * @param {Boolean} close - if `true`, closes the underlying connection |
| 293 | + * @return {Socket} self |
| 294 | + * |
| 295 | + * @public |
| 296 | + */ |
| 297 | + public disconnect(close = false): this { |
| 298 | + this.operator.disconnectSockets(close); |
| 299 | + return this; |
| 300 | + } |
163 | 301 | }
|
0 commit comments