Closed
Description
Because the official does not fully implement the afterNextPath feature, I wrote my own implementation, hoping that the official version of send and broadcast are in a queue to ensure the orderly distribution of messages.
export default class BaseRoom extends Room {
private _afterNextPatch: Array<{type: string, args: [any, any, IBroadcastOptions]}> = [];
constructor() {
super()
this['broadcastAfterPatch'] = () => {
const length = this._afterNextPatch.length;
if (length > 0) {
for (let i = 0; i < length; i++) {
const value = this._afterNextPatch[i]
if (value.type === 'send' && _.indexOf(this.clients, value.args[0]) !== -1) {
this.send.apply(this, value.args)
} else if (value.type === 'broadcast') {
this.broadcast.apply(this, value.args);
}
}
// new messages may have been added in the meantime,
// let's splice the ones that have been processed
this._afterNextPatch.splice(0, length);
}
}
}
delaySend(client: Client, type: string | number, message: any) {
this._afterNextPatch.push({ type: 'send', args: [client, type, message]})
}
delayBroadcast(type: string | number, message: any, options: IBroadcastOptions = {}) {
if (options.afterNextPatch) {
delete options.afterNextPatch;
}
this._afterNextPatch.push({ type: 'broadcast', args: [type, message, options]})
}
}
Metadata
Metadata
Assignees
Labels
No labels