8000 afterNextPatch feature: send and broadcast need to be in a queue · Issue #330 · colyseus/colyseus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content 10000
afterNextPatch feature: send and broadcast need to be in a queue #330
Closed
@dengzhaofun

Description

@dengzhaofun

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
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0