colyseus / colyseus.js

⚔ Colyseus Multiplayer SDK for JavaScript/TypeScript
https://docs.colyseus.io/getting-started/javascript-client/
MIT License
410 stars 107 forks source link

Implement Client-to-Server Schema serialization / deserialization #97

Open tobiasstrebitzer opened 3 years ago

tobiasstrebitzer commented 3 years ago

This Pull Request allows automatic serialization of Schema data when using the room.send method.

Client Example:

class UpdateArgs extends Schema {
  @type('uint8') x: number
  @type('uint8') y: number
}

const updateArgs = new UpdateArgs({ x: 1, y: 2 })
room.send(updateArgs)

Server Example:

class UpdateArgs extends Schema {
  @type('uint8') x: number
  @type('uint8') y: number
}

class GameRoom extends Room {
  async onCreate() {
    this.onMessage(UpdateArgs, (client, args) => {
      console.log(args) // UpdateArgs { x: 1, y: 2 }
      console.log(args instanceof UpdateArgs) // true
    })
  }
}

Notes: