GitHub | Documentation | Become a Patron

onLeave timeout



  • on server side, when player leave room, you can set a time out to avoid session destroying for few seconds if he wants to return?


  • administrators

    Hi @rscata,

    You can achieve this by turning autoDispose to false, and implementing this timeout by yourself. Like:

    import { Room, Delayed } from "colyseus";
    
    class MyRoom extends Room {
      autoDispose: boolean = false;
    
      onLeave () {
        if (this.clients.length === 0) {
          setTimeout(() => this._disposeIfEmpty(), 5000);
        }
      }
    }
    

    The _disposeIfEmpty method is not documented.

    Let me know if you have any suggestions to make this more intuitive. Cheers!



  • Thanks @endel

    Yes, this solution is good not to close the room, if there is no player in the room, but if in a room I have 5 players who play for example poker and by mistake one of them leave the room, how can I set an interval in which to return to the game room?
    Should I set a timeout for each client?

    I want to have 30 sec. to return to the room.

    Or, for example, if refresh on the browser page.

    In onLeave function, I implemented the destruction of the player instance, but I would like to give her some time to come back before destroying the instance.

    onLeave () {
        if (this.clients.length === 0) {
          setTimeout(() => this._disposeIfEmpty(), 5000);
        } else {
              // TODO: the player can return to play
    
              // remove instance
              this.state.removePlayer(client);
        }
      }
    

    thank you very much for your help


Log in to reply
 

Looks like your connection to Colyseus Forum was lost, please wait while we try to reconnect.