| 489 | * Expose of subset of the attributes and methods of the Socket class |
| 490 | */ |
| 491 | export class RemoteSocket<EmitEvents extends EventsMap, SocketData> |
| 492 | implements TypedEventBroadcaster<EmitEvents> |
| 493 | { |
| 494 | public readonly id: SocketId; |
| 495 | public readonly handshake: Handshake; |
| 496 | public readonly rooms: Set<Room>; |
| 497 | public readonly data: SocketData; |
| 498 | |
| 499 | private readonly operator: BroadcastOperator<EmitEvents, SocketData>; |
| 500 | |
| 501 | constructor(adapter: Adapter, details: SocketDetails<SocketData>) { |
| 502 | this.id = details.id; |
| 503 | this.handshake = details.handshake; |
| 504 | this.rooms = new Set(details.rooms); |
| 505 | this.data = details.data; |
| 506 | this.operator = new BroadcastOperator<EmitEvents, SocketData>( |
| 507 | adapter, |
| 508 | new Set([this.id]), |
| 509 | new Set(), |
| 510 | { |
| 511 | expectSingleResponse: true, // so that remoteSocket.emit() with acknowledgement behaves like socket.emit() |
| 512 | }, |
| 513 | ); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Adds a timeout in milliseconds for the next operation. |
| 518 | * |
| 519 | * @example |
| 520 | * const sockets = await io.fetchSockets(); |
| 521 | * |
| 522 | * for (const socket of sockets) { |
| 523 | * if (someCondition) { |
| 524 | * socket.timeout(1000).emit("some-event", (err) => { |
| 525 | * if (err) { |
| 526 | * // the client did not acknowledge the event in the given delay |
| 527 | * } |
| 528 | * }); |
| 529 | * } |
| 530 | * } |
| 531 | * |
| 532 | * // note: if possible, using a room instead of looping over all sockets is preferable |
| 533 | * io.timeout(1000).to(someConditionRoom).emit("some-event", (err, responses) => { |
| 534 | * // ... |
| 535 | * }); |
| 536 | * |
| 537 | * @param timeout |
| 538 | */ |
| 539 | public timeout( |
| 540 | timeout: number, |
| 541 | ): BroadcastOperator<DecorateAcknowledgements<EmitEvents>, SocketData> { |
| 542 | return this.operator.timeout(timeout); |
| 543 | } |
| 544 | |
| 545 | public emit<Ev extends EventNames<EmitEvents>>( |
| 546 | ev: Ev, |
| 547 | ...args: EventParams<EmitEvents, Ev> |
| 548 | ): boolean { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…