Connection Drops and Rejoining¶
Connections drop. A phone changes from wifi to mobile data, a laptop sleeps, a player reloads the page. GameFuse Multiplayer holds the player's seat for a while and lets them back in.
You choose how long with reconnectSeconds in your room code's config.
What happens when a connection drops¶
- The player's connection is lost.
- In your room code,
playerDropped(room, player)is called. Inroom.players, that player'sconnectedis nowfalse. Their seat is held. - In that player's browser, the
reconnectingevent fires. The client library starts trying to get back in on its own. - The other players' browsers get a
playersevent, withconnected: falsefor the dropped player.
Then one of two things happens.
They get back within reconnectSeconds. playerReturned(room, player) is called in your room code. The reconnected event fires in their browser. They have the same seat and the same id.
They do not. playerLeft(room, player, 'timeout') is called in your room code. In their browser, the closed event fires with 'lost'.
How the client library retries¶
After a drop the library tries again after 0.5 seconds, then 1, 2, 3, 4 and 5 seconds, and keeps trying for as long as the room holds the seat. You do not write any of this.
In your room code¶
playerDropped(room, player)¶
The connection was lost and the seat is held. Decide what your game does meanwhile: pause, carry on, or let a simple stand-in play.
playerDropped(room, player) {
room.state.paused = true;
room.broadcast('paused', { name: player.name });
},
playerReturned(room, player)¶
They are back. Their browser may have missed messages while it was away, so send them what they need to catch up.
playerReturned(room, player) {
room.state.paused = false;
room.send(player, 'catchUp', {
scores: room.state.scores,
board: room.state.board,
});
room.broadcast('resumed', {});
},
playerLeft(room, player, how)¶
The player is gone for good. how tells you why.
how |
Meaning |
|---|---|
'quit' |
They left on purpose. Their browser called room.leave(). |
'timeout' |
They dropped and did not come back within reconnectSeconds. |
playerLeft(room, player, how) {
const stillHere = room.players.filter((p) => p.id !== player.id);
if (stillHere.length === 1) {
room.broadcast('winner', { id: stillHere[0].id, why: how });
room.end({ winnerId: stillHere[0].id });
}
},
In the browser¶
room.on('reconnecting', () => showBanner('Connection lost. Trying again...'));
room.on('reconnected', () => hideBanner());
room.on('players', (players) => {
for (const p of players) {
setPlayerFaded(p.seat, !p.connected);
}
});
room.on('closed', (reason) => {
if (reason === 'lost') showMessage('You were disconnected.');
showMenu();
});
Page reloads and mp.resume()¶
A reload throws away the page, and the connection with it. The player can still get back in, as long as it is within reconnectSeconds.
The client library keeps what it needs in the browser's sessionStorage. When your page starts, call mp.resume() before you join anything. It returns the room the player was in, or null.
const mp = new GameFuseMultiplayer({
gameId: 123,
gameToken: 'your-game-api-token',
});
await mp.signInAsGuest();
let room = await mp.resume();
if (!room) {
room = await mp.joinQueue('1v1', { clientVersion: '1' });
}
room.on('message', (type, data) => handleMessage(type, data));
sessionStorage belongs to one browser tab. A reload of the same tab can resume. A new tab cannot.
To your room code, a reload looks the same as any other drop: playerDropped, then playerReturned. Send the catch-up message from playerReturned and a reloaded page can draw the game again.
Why a room closed¶
The closed event gives one of four reasons.
| Reason | Meaning | The room code saw |
|---|---|---|
'ended' |
The game is over. Your room code called room.end(result). |
gameEnd(room, result) |
'kicked' |
Your room code removed this player with room.kick(player, reason). |
|
'lost' |
This player's connection dropped and they could not get back in time. | playerDropped, then playerLeft with 'timeout' |
'left' |
This player's browser called room.leave(). |
playerLeft with 'quit' |
After closed, the room object is finished. To play again, join a queue or a room again.
Choosing reconnectSeconds¶
A short time, such as 10 to 15 seconds, suits fast games where waiting for someone spoils it for the others. A longer time, such as 60 seconds or more, suits turn-based games where a player can be away without harm.
All other numbers are on the limits page.