feat: broadcast and expect multiple acks

Tests will be added in the parent repository.

Related:

- https://github.com/socketio/socket.io/issues/1811
- https://github.com/socketio/socket.io/issues/4163
This commit is contained in:
Damien Arrachequesne
2022-03-29 08:28:54 +02:00
parent 75455fa8ba
commit a7f1c90a32

View File

@@ -9,6 +9,7 @@ export interface BroadcastFlags {
local?: boolean;
broadcast?: boolean;
binary?: boolean;
timeout?: number;
}
export interface BroadcastOptions {
@@ -42,6 +43,15 @@ export class Adapter extends EventEmitter {
*/
public close(): Promise<void> | void {}
/**
* Returns the number of Socket.IO servers in the cluster
*
* @public
*/
public serverCount(): Promise<number> {
return Promise.resolve(1);
}
/**
* Adds a socket to a list of room.
*
@@ -140,6 +150,54 @@ export class Adapter extends EventEmitter {
});
}
/**
* Broadcasts a packet and expects multiple acknowledgements.
*
* Options:
* - `flags` {Object} flags for this packet
* - `except` {Array} sids that should be excluded
* - `rooms` {Array} list of rooms to broadcast to
*
* @param {Object} packet the packet object
* @param {Object} opts the options
* @param clientCountCallback - the number of clients that received the packet
* @param ack - the callback that will be called for each client response
*
* @public
*/
public broadcastWithAck(
packet: any,
opts: BroadcastOptions,
clientCountCallback: (clientCount: number) => void,
ack: (...args: any[]) => void
) {
const flags = opts.flags || {};
const packetOpts = {
preEncoded: true,
volatile: flags.volatile,
compress: flags.compress
};
packet.nsp = this.nsp.name;
// we can use the same id for each packet, since the _ids counter is common (no duplicate)
packet.id = this.nsp._ids++;
const encodedPackets = this.encoder.encode(packet);
let clientCount = 0;
this.apply(opts, socket => {
// track the total number of acknowledgements that are expected
clientCount++;
// call the ack callback for each client response
socket.acks.set(packet.id, ack);
socket.client.writeToEngine(encodedPackets, packetOpts);
});
clientCountCallback(clientCount);
}
/**
* Gets a list of sockets by sid.
*