← All writing

Disconnected is not the same as gone

In my online games, locking your phone lost you the match. The fix was to stop treating a dropped connection as a player leaving.

·2 min read
A game of Fog Chess: your own pieces in white, the opponent's pieces shown only as grey discs, and a move log where their moves say "Hidden piece".

All my tests were green. Then I actually played one of my own games on my phone, locked the screen for a moment, and came back to “You lose”.

The bug

I have two real-time multiplayer games: Fog Chess (chess with fog of war: you see that a square is taken, but not by what) and Kalak, a Tunisian party game. Both talk to their server over a WebSocket.

In both, a closed socket meant one thing: the player left. And a socket closes for many reasons that have nothing to do with leaving:

  • the phone’s screen locks
  • the Wi-Fi drops for a second
  • the page reloads

In Fog Chess, any of those ended the match, and the player who glanced away was told they lost.

The fix: hold the seat

The answer was to treat “disconnected” and “gone” as two different states, with time in between.

1. A seat belongs to a token, not to a connection. When you first join, the server gives you a secret token, and your browser keeps it. Your seat (white or black, in Fog Chess) belongs to that token.

2. A dropped connection holds the seat. When your socket closes, the server doesn’t end anything. It notes a deadline instead: your seat is kept for 60 seconds.

3. Coming back resumes everything. Reconnect with the same token inside the window and you get your seat back, with your exact view: the board, the fog, the score.

4. Only the timeout means gone. If the 60 seconds run out mid-game, then it’s a loss by abandonment.

Tell both players what’s happening

Holding the seat silently would only confuse people, so both sides see a banner:

  • the player who dropped: “Connection lost — reconnecting… Your seat is kept for 60 seconds.”
  • their opponent: “Opponent disconnected — waiting 42 s for them to come back. If they don’t, you win.”, counting down

The page reconnects by itself, so most of the time the banner flashes for a second and the game goes on.

Testing it

These are the tests I should have had from the start. They run a real server with a much shorter window, and check that:

  • a reload mid-game keeps the seat, tells the opponent, and restores the fogged view
  • a player who stays away past the window loses by abandonment
  • the same tab reconnecting before the old socket even dropped takes its seat over
  • when both players are gone past the window, the room is freed

In a real-time app, the network will drop. The question is only whether your game survives it.

The lesson I took away: green tests only prove what you thought of. Playing your own game on a real phone finds the rest.