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:
@@ -341,3 +341,69 @@ test('GM roster, keys and token panel drive the real server', { timeout: 240000
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// #88: a plain reconnect is not a rename, so `roster` has to follow connections coming and going.
|
||||
async function waitRosterState(page, id, want, timeout = 30000) {
|
||||
try {
|
||||
await page.waitForFunction(
|
||||
([playerId, state]) => {
|
||||
const node = document.querySelector(
|
||||
`#gm-roster-list li[data-player="${playerId}"] .gm-roster-state, ` +
|
||||
`#gm-roster-list li[data-player="${playerId}"] #gm-roster-state`,
|
||||
)
|
||||
return node?.textContent === state
|
||||
},
|
||||
[id, want],
|
||||
{ timeout },
|
||||
)
|
||||
} catch (error) {
|
||||
const rows = await page.evaluate(() =>
|
||||
[...document.querySelectorAll('#gm-roster-list li')].map(li => li.textContent),
|
||||
)
|
||||
throw new Error(`${error.message}; roster was ${JSON.stringify(rows)}`)
|
||||
}
|
||||
}
|
||||
|
||||
test('GM roster connected follows a player tab reload and close', { timeout: 180000 }, async () => {
|
||||
await withStack(async ({ origin, gmKey }) => {
|
||||
const raw = await openAs(origin, gmKey)
|
||||
await raw.ws.waitText('hello')
|
||||
await raw.ws.waitText('walls')
|
||||
const bob = await createPlayer(raw.ws)
|
||||
await namePlayer(origin, bob.key, 'Bob')
|
||||
const browser = await launchBrowser()
|
||||
try {
|
||||
const gmView = await newPage(browser)
|
||||
const gm = gmView.page
|
||||
await gm.goto(`${origin}/`)
|
||||
await waitForVisible(gm, '#signin')
|
||||
await signIn(gm, gmKey)
|
||||
await waitForVisible(gm, '#gm-panels')
|
||||
await gm.locator(`#gm-roster-list li[data-player="${bob.id}"]`).waitFor({ state: 'visible' })
|
||||
await waitRosterState(gm, bob.id, 'offline')
|
||||
|
||||
const bobView = await newPage(browser)
|
||||
const bobPage = bobView.page
|
||||
await bobPage.goto(`${origin}/`)
|
||||
await waitForVisible(bobPage, '#signin')
|
||||
await signIn(bobPage, bob.key)
|
||||
await waitForVisible(bobPage, '#hud')
|
||||
await waitForText(bobPage, '#me', /^Bob$/)
|
||||
await waitRosterState(gm, bob.id, 'connected')
|
||||
|
||||
// A reload is a plain reconnect: no setname, but the GM row must end up online again.
|
||||
await bobPage.reload()
|
||||
await waitForVisible(bobPage, '#hud')
|
||||
await waitForText(bobPage, '#me', /^Bob$/)
|
||||
await waitRosterState(gm, bob.id, 'connected')
|
||||
|
||||
// Closing the tab must flip the row offline without any intent from the player.
|
||||
await bobView.context.close()
|
||||
await waitRosterState(gm, bob.id, 'offline')
|
||||
await gmView.context.close()
|
||||
} finally {
|
||||
raw.ws.close()
|
||||
await browser.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
+26
-3
@@ -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
|
||||
|
||||
|
||||
@@ -612,6 +612,43 @@ test('restart: characters, reveals and encountered lists come back', async () =>
|
||||
}
|
||||
})
|
||||
|
||||
const rosterEntry = (frame, id) => frame.players.find(entry => entry.id === id)
|
||||
const waitRosterState = (ws, from, id, connected, timeoutMs = 5000) =>
|
||||
ws.waitFor((m, i) => i >= from && m.t === 'roster' &&
|
||||
rosterEntry(m, id)?.connected === connected, timeoutMs)
|
||||
|
||||
// A plain reconnect is not a `player.setname`: the GM's roster must still follow the connection
|
||||
// coming and going, or a player who reloads looks offline for ever (DESIGN 6.1 `roster`/6.3).
|
||||
test('roster: connected follows a plain connect and close, not only setname', async () => {
|
||||
await withGmServer(async (s, gmKey) => {
|
||||
const gm = await gmConnect(s.origin, gmKey)
|
||||
const alice = await createPlayer(gm.ws)
|
||||
let mark = gm.ws.messages.length
|
||||
const first = await connectNamed(s.origin, alice.key, 'Alice')
|
||||
await waitRosterState(gm.ws, mark, alice.id, true)
|
||||
|
||||
// Close the player connection: no intent is sent, only the WS close.
|
||||
mark = gm.ws.messages.length
|
||||
first.ws.close()
|
||||
await waitRosterState(gm.ws, mark, alice.id, false)
|
||||
|
||||
// Reconnect with the same key and name: hello only, so no setname/roster from the player side.
|
||||
mark = gm.ws.messages.length
|
||||
const again = await openAs_(s.origin, alice.key)
|
||||
const hello = await again.ws.waitText('hello')
|
||||
assert.equal(hello.me.name, 'Alice', 'the reconnect is named from the stored player row')
|
||||
await waitRosterState(gm.ws, mark, alice.id, true)
|
||||
assert.equal(again.ws.messages.some(m => m.t === 'roster'), true,
|
||||
'the connecting connection still gets its own roster in the resync')
|
||||
|
||||
// And it goes offline again on close.
|
||||
mark = gm.ws.messages.length
|
||||
again.ws.close()
|
||||
await waitRosterState(gm.ws, mark, alice.id, false)
|
||||
gm.ws.close()
|
||||
})
|
||||
})
|
||||
|
||||
// Local alias so the imports stay at the top of the file.
|
||||
async function openAs_(origin, key) {
|
||||
const { cookie } = await signIn(origin, key)
|
||||
|
||||
@@ -43,8 +43,11 @@ test('hello is the first frame; n counts server frames per connection, never glo
|
||||
assert.equal((await other.waitText('roster')).n, 2)
|
||||
assert.equal((await other.waitText('walls')).n, 3)
|
||||
assert.equal((await other.request({ t: 'conn.ping', ts: 7 })).n, 4)
|
||||
// ... and the first connection's sequence did not move
|
||||
assert.equal((await gm.ws.request({ t: 'conn.ping', ts: 44 })).n, 6)
|
||||
// The second connection is a plain connect, so the first GM connection gets a roster with the
|
||||
// new `connected` state (6.1 roster) and its own counter advances by one.
|
||||
assert.equal((await gm.ws.waitFor(m => m.t === 'roster' && m.n === 6)).n, 6)
|
||||
// ... and the first connection's per-connection counter did not move with `other`'s.
|
||||
assert.equal((await gm.ws.request({ t: 'conn.ping', ts: 44 })).n, 7)
|
||||
for (const m of gm.ws.messages) assert.equal(typeof m.n, 'number', JSON.stringify(m))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user