Navigation

    Colyseus
    • Login
    • Search
    • Recent
    • Tags
    • Users
    1. Home
    2. mackycheese
    mackycheese

    mackycheese

    @mackycheese

    Chat Follow Unfollow
    0
    Reputation
    3
    Posts
    1364
    Profile views
    0
    Followers
    0
    Following
    Joined Last Online

    • Profile
    • More
      • Continue chat with mackycheese
      • Flag Profile
      • Following
      • Followers
      • Topics
      • Posts
      • Best
      • Groups
    mackycheese Follow

    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 •
    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 •
    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 •