Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hope that the official support request response communication model #331

Closed
dengzhaofun opened this issue Apr 28, 2020 · 2 comments
Closed

Comments

@dengzhaofun
Copy link

I realized the simple request reponse communication model by myself, and I hope that the official can support it, so that the development will be faster and easier.

client

import { nanoid } from '../plugin/nanoid'
import * as _ from '../plugin/lodash'

export abstract class BaseRoom {
  respFuncs = {}
  constructor(protected readonly room) {
    this.room.onMessage('*', (type, message) => {
      console.log("BaseRoom -> constructor -> message", type, message)
    })
    this.room.onMessage('response', (message) => {
      if (_.isObject(message) && _.isFunction(this.respFuncs[message.respId])) {
        this.respFuncs[message.respId](message)
        delete this.respFuncs[message.respId]
      }
    })
  }

  async request(type: string, message: any, delay: boolean = false, callback: (message) => void) {
    const respId = nanoid()
    this.room.send('request', {type, message, respId, delay})
    if (_.isFunction(callback)) {
      this.respFuncs[respId] = callback
    }
  }
}

server

  async onCreate(options: any) {
    this.onMessage('*', (client: Client, type: string | number, message: any) => {
      const func = this[`${type}_socket`]
      if (_.isFunction(func)) {
        func.call(this, client, message)
      }
    })
  }

  async request_socket(client: Client, wrapMessage: any) {
    const { type, message, respId, delay } = wrapMessage
    if (type && message) {
      const func = this[`${type}_socket`]
      if (_.isFunction(func)) {
        const data = await func.call(this, client, message)
        if (_.isString(respId)) {
          if (delay) {
            this.delaySend(client, 'response', data)
          } else {
            client.send('response', data)
          }
        }
      }
    }
  }
@endel endel added this to 📚 Needs triage in Support / Maintenance Aug 19, 2021
@endel endel moved this from 📚 Needs triage to ↓ Low priority in Support / Maintenance Aug 19, 2021
@endel endel removed this from ↓ Low priority in Support / Maintenance Aug 19, 2021
@endel endel added this to 📚 Backlog in Next version (0.15) Aug 19, 2021
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale label Aug 28, 2021
@github-actions
Copy link

github-actions bot commented Sep 4, 2021

This issue was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this as completed Sep 4, 2021
Next version (0.15) automation moved this from 📚 Backlog to 🎉 Done Sep 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

No branches or pull requests

1 participant