Navigation

  • Recent
  • Tags
  • Users
  • Search
  • Login
Colyseus
  • Login
  • Search
  • Recent
  • Tags
  • Users

Documentation GitHub

We're migrating to GitHub Discussions. This forum does not accept new registrations since April 6, 2023.
  1. Home
  2. mackycheese
  3. Posts
  • Profile
  • More
    • Continue chat with mackycheese
    • Flag Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

Posts made by mackycheese

Server State Class How To Reference Room

My server does this in onInit():

this.setState(new Game())

I want the Game class to be able to call broadcast on the room.
If I do this.state.room=this in onInit(), then this will be a part of the state and broadcast to the clients, which I don't want.
How can I pass a room reference to the room state without having it be a part of the serialized state?

I'm using the old serializer if it matters.

The reason I need to do this is because my state class handles all the game logic, and the room is basically a wrapper around the state.

posted in Questions & Help • 9 Apr 2019, 18:17
RE: Client throws when setState is called in server room

@endel

I see. Thanks! I switched to the old serializer and it works fine now. Out of curiosity, why would you switch serializers?

posted in Questions & Help • 4 Apr 2019, 00:08
Client throws when setState is called in server room

I have this basic client code:

        if(this.state.game_started) {
            console.log("Game already started")
            return
        }
        this.setState({
            game_started:true
        })
        this.client=new Client("ws://localhost:2657")

        this.room=this.client.join("game-room")
        this.room.onJoin.add(()=>{
            console.log(`Joined room with client id ${this.client.id}`)
        })
        this.room.onStateChange.add(()=>{
            console.log("State change:")
            console.log(this.room.state)
            this.forceUpdate()
        })

This is using React.

And this basic server room:

const colyseus=require("colyseus")

class GameRoom extends colyseus.Room {

    constructor(){
        super(...arguments)
        this.maxClients=2
    }

    onInit(options){
        // this.setState({
    //         message_history: []
    //     })
        this.setState({})
        this.state={
            messages:[]
        }
    }

    requestJoin(options, isNewRoom) {
        return true
    }

    onJoin(client, options, auth) {
        console.log(`Client with id ${client.id} joined.  Options: ${JSON.stringify(options)}`)
        this.state.messages.push({
            sender:"Server",
            content:`Client with id ${client.id} joined`
        })
    }

    onMessage(client, data) {
        if(data.type==="message"){
            this.state.message_history.push(data.message)
        }
    }

    onLeave(client, consented) {
        console.log(`Client with id ${client.id} left. Consented: ${consented}`)
    }

    onDispose() {
    }

}

module.exports = GameRoom

When I comment out the setState in onInit, the client joins perfectly fine. When I leave it uncommented, the client throws this error:

index.js:1446 colyseus.js: server error: SchemaSerializer error. See: https://docs.colyseus.io/migrating/0.10/#new-default-serializer

And the server prints nothing.

This is using latest version of server and client packages, so why is this happening?

posted in Questions & Help • 3 Apr 2019, 03:23

© 2023 Endel Dreyer