listen event disconnect on client and server

Hello,
How to to listen event disconnect on client and server?

@endel said in listen event disconnect on client and server:

Hi @jack1604,

From client-side: http://colyseus.io/docs/client-room/#onleave
From server-side: http://colyseus.io/docs/api-room/#onleave-client-consented

Cheers!

thanks,
when client rejoin success full, server will send a message, but if server send broadcast client will receive, if server send by method send, client will not receive
what the problem?

this is my code:

async onLeave (client, consented) {
var room = this;
console.log(new Date()," onLeave client.sessionId:", client.sessionId, ' consented:', consented);
this.broadcast(${ client.sessionId } left.);
if(!consented){
client.active = false;
try {
await this.allowReconnection(client, 20);
client.active = true;
this.broadcast(client.sessionId + ' rejoined');// client will receive
this.send(client, client.sessionId + ' rejoined'); // client not receive
} catch (e) {
console.log(e);
}
}
}

Hi @jack1604, that's a good question, the client reference you're using on this.send() is the one that has been disconnected. The new - reconnected - client will be returned from the allowReconnection() method.

Example:

try {
    const reconnectedClient = await this.allowReconnection(client, 20);
    this.send(reconnectedClient, reconnectedClient.sessionId + ' rejoined');
} catch (e) {
    console.log(e);
}

Hope this helps! Cheers!

@endel said in listen event disconnect on client and server:

Hi @jack1604, that's a good question, the client reference you're using on this.send() is the one that has been disconnected. The new - reconnected - client will be returned from the allowReconnection() method.

Example:

try {
    const reconnectedClient = await this.allowReconnection(client, 20);
    this.send(reconnectedClient, reconnectedClient.sessionId + ' rejoined');
} catch (e) {
    console.log(e);
}

Hope this helps! Cheers!

hello,
if client disconnect or leave room , it will receive event on room.leave(), how to detect it is disconnect or leave room? because if client is disconnected, client will rejoin. if client leave room, client will not rejoin.