fix(server): broadcast roster on plain connect and close (#88)

A reconnect with an existing name fires no intent, so the only roster the
GM saw was the connecting connection's own resync; the row stayed offline
while the player badge said Connected.

Send the GM roster when a connection activates (skipping the connecting
connection, which already got it in the 6.3 resync) and after a close, with
the registry entry removed first so connected is recomputed. Also cover the
server-initiated malformed-flood close. The setname ack-before-resync
ordering is unchanged.

Tests: WS connect/close/reconnect roster states; e2e GM row follows a
player tab reload and close. protocol test updated for the extra roster on
a second connection.
This commit is contained in:
2026-11-21 23:44:51 +01:00
parent b938e6f256
commit fda83621f3
4 changed files with 134 additions and 5 deletions
+26 -3
View File
@@ -338,6 +338,17 @@ proc sendRoster() =
let n = nextN(c)
if n > 0: c.ws.send($withN(if c.role == prGm: frames.everyone else: frames.namedOnly, n))
proc sendRosterToGms(exceptConnId = 0) =
## A plain connect or close changes `connected` (6.1 `roster`), but no intent fires: without this
## the GM's row for a player who reloads stays `offline` until some unrelated rename/key op. Only
## GM connections show (and act on) the flag, so only they are told; a connecting GM already got
## the same roster as the first frame of its 6.3 resync and is skipped. Caller holds the lock.
let frames = rosterFrames()
for c in app.conns.snapshot():
if c.role != prGm or c.id == exceptConnId: continue
let n = nextN(c)
if n > 0: c.ws.send($withN(frames.everyone, n))
proc sendHello(connId: int, player: Player) =
## The `hello` frame (6.1): the first frame on a connection, and again after `player.setname`
## (which is why it lives in a proc both paths call). Keyed by the app connId so a caller holding
@@ -913,6 +924,9 @@ proc wsUpgrade(request: Request) {.gcsafe.} =
# The 6.3 order: `roster`, walls (GM), characters, encountered, then vision/explored and the
# tokens. Players never receive wall data (3.1, T2); `resync` refreshes the player's sight.
resync(activated.id, named, playerRec)
# The new connection changed this player's `connected` flag. The connection itself already got
# the roster above; every other GM connection learns about it here (6.1 `roster`).
sendRosterToGms(activated.id)
# ---------------------------------------------------------------------------------------------------
# Intent pipeline (DESIGN 6.5). Order: parse, rate limit, unknown type, unnamed player, role,
@@ -2283,6 +2297,10 @@ proc malformedFrame(conn: Conn, nowMs: int64) {.gcsafe.} =
## DESIGN 6.5 step 2 / row 8: one `error malformed`, and the tenth inside 10 s closes with 1008.
if app.conns.recordMalformed(conn.id, nowMs):
closeRegistered(conn, ReasonMalformedFlood, CloseMalformedFlood)
# A server-initiated close is still a close: if this was the player's last connection the GM's
# roster must say `offline` (6.1 `roster`), exactly as for a browser-initiated close.
withLock app.dbLock:
sendRosterToGms()
app.lg.log(lvWarn, "conn_malformed_flood", [("player", $conn.playerId), ("conn", $conn.id)])
else:
sendError(conn, CodeMalformed)
@@ -2337,9 +2355,14 @@ proc websocketEvent(ws: WebSocket, event: WebSocketEvent, message: Message) {.gc
of CloseEvent, ErrorEvent:
let found = app.conns.connForWs(ws)
if found.isSome:
app.conns.remove(found.get.id)
app.lg.log(lvDebug, "conn_close", [("player", $found.get.playerId),
("conn", $found.get.id)])
let conn = found.get
# Remove first so `activeCountFor` is current, then tell the GMs: a player whose last
# connection closed is offline, and without a roster broadcast the row would stay `connected`
# (6.1 `roster`, 6.3). No intent fires on a plain close, so this is the only place it can.
withLock app.dbLock:
app.conns.remove(conn.id)
sendRosterToGms()
app.lg.log(lvDebug, "conn_close", [("player", $conn.playerId), ("conn", $conn.id)])
else:
discard