Disconnect from multiplayer server when device screen off

I have developed a multiplayer game with colyseus.
it requires 4 clients and most of the time client have to wait for other clients to join.so some device going to sleep mode after a few seconds and the client Disconnect from the server.

i added window.set_dim_mode (window.DIMMING_ON) this line to init function.but it not works.

also, i need to know about rejoining functions. i used this code and it gives a error “this.state.inactivateClient” is not function;

async onLeave (client) {
  // flag client as inactive for other users
  this.state.inactivateClient(client);

  try {
    // 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.activateClient(client);

  } catch (e) {

    // 20 seconds expired. let's remove the client.
    this.state.removeClient(client);
  }
}

please help me.

window.set_dim_mode (window.DIMMING_ON)

Means that you wanna use dimming mode and screen will turn off, when user inactive.
I think window.set_dim_mode (window.DIMMING_OFF) exactly what you need

3 Likes

thanks a lot.it works.

2 Likes

This is a question for @endel or maybe @selimanac to answer. Or on the Colyseus forum or Colyseus Discord Chat.

2 Likes

Hi @uditha, feel free to join the Colyseus Discord as @britzl mentioned, there’s a nice community over there!

You’re supposed to implement inactivateClient / activateClient / removeClient by yourself in your handler. I’ve simplified a bit here with a real scenario we’re using:

async onLeave(client: Client, consented: boolean) {
    // flag player as disconnected
    this.state.players[client.sessionId].connected = false;

    try {
        if (consented) {
            throw new Error("consented leave");
        }

        // wait for his reconnection (20 seconds)
        await this.allowReconnection(client, 20);
        
        // player is back!
        this.state.players[client.sessionId].connected = true;

    } catch (e) {
        // remove player definitely
        delete this.state.players[client.sessionId];
    }

}
2 Likes

Hi endel,
thanks for your quick reply and can you please provide client side implementation example.
how i need to use rejoin (roomNameOrId: string, sessionId: string) function. can you please send me an example code

@uditha have you checked the docs page? :sweat_smile: https://docs.colyseus.io/client-overview/#rejoin-roomnameorid-string-sessionid-string

you basically need to pass the previous sessionId when re-joining the lost connection, if the rejoin reaches the server before allowReconnection times out, the connection will be re-stablished! cheers!

there’s one caveat for iOS, though. you need to send data to the server the re-activate the WebSocket coroutine (that’s my guess, maybe there’s another workaround for this!)

function window_callback(self, event, data)
	if event == window.WINDOW_EVENT_FOCUS_GAINED then
		-- iOS workaround to re-active WebSocket connection
		room:send({"whatever"})
	end
end

window.set_listener(window_callback)

I’ll add this to colyseus-defold README soon

1 Like

Thanks for your quick reply. I notice that user received a call and when uses defold-sharing option client disconnect form server. please kindly provide a solution to this matter.is there any way to prevent disconnection when user received a call and use sharing options