GitHub Documentation Patreon donate button

Room.clients aren't Room.state.players



  • Hi,
    I'm trying to create a gameroom with a maxClients.(clients are deleted if the leave the room lobby). When it's reached, I want to lock the room and send each client their id in the game as player (that is their order in the Room.clients array).

    The problem I face is the client is added to the array in a async task, so I can't immediately begin the task in the onJoin() method because the last client as not been added yet when onJoin() is called.

    I can't seem to find a callback for the room when a client is effectively added to Room.clients, so I can send each client their player id (different from their client id and sessionId) in the game.

    It's I think a basic question, but I can't figure it out. I'm new to multiplayer gameserver, so maybe I'm not thinking the right way.
    Regards and thanks for any help.


  • administrator

    Hi @elberto, this is actually a bug in the server as of 0.11.x (related to #260). This is going to be fixed on 0.12.x next year.

    For now, you can do this:

    onJoin() {
        if (this.hasReachedMaxClients()) { 
            this.lock()
        }
    }
    

    Hope this helps! Cheers!



  • Hi @endel, thanks for your quick answer. I understand why I was struggling now !
    With your fix, I still can't deliver my messages to all clients.
    For now I did this on async for testing purposes, but it's really ugly and unreliable

    
    if (this.hasReachedMaxClients()) { 
       setTimeout(async () => {
          for (let sessionId in this.clients) {
             this.send(this.clients[sessionId], { message: "Hello world!" });
          }
       }
    },  1000);
    
    

    If you have a better way, I'll take it !
    Have a nice day !


 

GitHub Documentation Patreon donate button

© 2020 Endel Dreyer