GitHub Documentation Patreon donate button

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?


  • administrator

    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



  • I have same question. How do you resolve it?
    "Should I set a timeout for each client?"

    I have read source code in here:

    const DEFAULT_SEAT_RESERVATION_TIME = Number(process.env.COLYSEUS_SEAT_RESERVATION_TIME || 5);
    ...
    // seat reservation & reconnection
    protected seatReservationTime: number = DEFAULT_SEAT_RESERVATION_TIME;
    ...
    public setSeatReservationTime(seconds: number) {
    	this.seatReservationTime = seconds;
    	return this;
    }
    

    Variable "DEFAULT_SEAT_RESERVATION_TIME" for change time with this problem?



  • @DinhNguyen and @rscata I think you both are looking for rejoin the game. Below snippet will solve your problem. For more details: Rejoin Link.

    async onLeave (client, consented: boolean) {
      // flag client as inactive for other users
      this.state.players[client.sessionId].connected = false;
    
      try {
        if (consented) {
            throw new Error("consented leave");
        }
    
        // allow disconnected client to rejoin into this room until 20 seconds
        await this.allowReconnection(client, 20);
    
        // client returned! let's re-activate it.
        this.state.players[client.sessionId].connected = true;
    
      } catch (e) {
    
        // 20 seconds expired. let's remove the client.
        delete this.state.players[client.sessionId];
      }
    }
    

 

GitHub Documentation Patreon donate button

© 2019 Endel Dreyer