GitHub Documentation Patreon donate button

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?


  • administrator

    @mackycheese The error points you to see this URL: https://docs.colyseus.io/migrating/0.10/#new-default-serializer

    Basically, the new default is to use a class extending the Schema for your state as of version 0.10. You can continue using the previous serializer if you'd like to, as stated in the migration guide.

    Using React, you can do this in the client-side:

    // client-side
    this.room.onStateChange.add((newState) => {
        this.setState(newState);
    })
    


  • @endel

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


 

GitHub Documentation Patreon donate button

© 2019 Endel Dreyer