Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Everyone sees '12 here · 4 signed in · 8 watching' on one line at the top, updated live over /live #295
smith/arm:smith/291-everyone-sees-12-here-4-signed-in-8-watc → main 1 commit fork
4 files
changed +44
−2
Software/so101/src/lobby.test.ts +14
| @@ -105,6 +105,20 @@ describe("lobby", () => { | ||
| 105 | 105 | expect((await anon.next((m) => m.type === "error")).error).toContain("join to take part"); |
| 106 | 106 | }); |
| 107 | 107 | |
| 108 | test("presence counts change on connect and disconnect, coalesced", async () => { | |
| 109 | const port = await start(); | |
| 110 | const seen = (c: Awaited<ReturnType<typeof join>>, total: number, signedIn: number) => c.next((m) => m.type === "presence" && m.total === total && m.signedIn === signedIn); | |
| 111 | const anon = await join(port); | |
| 112 | const alice = await join(port, ALICE); | |
| 113 | const bob = await join(port, BOB); | |
| 114 | expect(await seen(anon, 3, 2)).toMatchObject({ anonymous: 1 }); | |
| 115 | expect(anon.heard.filter((m) => m.type === "presence").length).toBeLessThanOrEqual(2); // three connects, not three broadcasts | |
| 116 | bob.ws.close(); | |
| 117 | expect(await seen(alice, 2, 1)).toMatchObject({ anonymous: 1 }); | |
| 118 | alice.ws.close(); | |
| 119 | expect(await seen(anon, 1, 0)).toMatchObject({ anonymous: 1 }); | |
| 120 | }); | |
| 121 | ||
| 108 | 122 | test("a forged identity header is nobody", async () => { |
| 109 | 123 | const port = await start(); |
| 110 | 124 | const forged = { "x-forge-identity": `u-owner|trevato|admin|${Date.now() + 60_000}`, "x-forge-signature": sign(`u-owner|trevato|admin|${Date.now() + 60_000}`, "not the key") }; |
Software/so101/src/lobby.ts +27 −1
| @@ -22,7 +22,7 @@ | ||
| 22 | 22 | * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused |
| 23 | 23 | * {"type": "pong"} the answer to {"type": "ping"} |
| 24 | 24 | * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one |
| 25 | * Server → client: hello (who you are, and whether you may moderate: delete a line), state (the turn, the line, pause, the announcement and the speed cap), | |
| 25 | * Server → client: presence (how many are here, signed in and anonymous, coalesced to one frame per PRESENCE_MS), hello (who you are, and whether you may moderate: delete a line), state (the turn, the line, pause, the announcement and the speed cap), | |
| 26 | 26 | * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way), |
| 27 | 27 | * said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only: |
| 28 | 28 | * settings, hidden lines, mute suggestions), banned and words (owners only), ping, error; and whatever the relay |
| @@ -68,6 +68,8 @@ export interface LobbyOptions { | ||
| 68 | 68 | /** How often each socket is pinged, and how long it may stay silent before it counts as dropped. */ |
| 69 | 69 | pingMs?: number; |
| 70 | 70 | silentMs?: number; |
| 71 | /** Connects and disconnects within this many ms are told to the room as one presence frame. */ | |
| 72 | presenceMs?: number; | |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | 75 | interface Seat { |
| @@ -102,6 +104,8 @@ export class Lobby { | ||
| 102 | 104 | private readonly arriveListeners = new Set<(tell: (text: string) => void) => void>(); |
| 103 | 105 | private readonly pinger: ReturnType<typeof setInterval>; |
| 104 | 106 | private readonly silentMs: number; |
| 107 | private readonly presenceMs: number; | |
| 108 | private presenceTimer: ReturnType<typeof setTimeout> | undefined; | |
| 105 | 109 | private lastActivityState = 0; |
| 106 | 110 | private readonly now: () => number; |
| 107 | 111 | |
| @@ -117,6 +121,7 @@ export class Lobby { | ||
| 117 | 121 | this.accounts = options.accounts ?? new Accounts(); |
| 118 | 122 | this.filter = new WordFilter(options.wordsFile); |
| 119 | 123 | this.silentMs = options.silentMs ?? 15_000; |
| 124 | this.presenceMs = options.presenceMs ?? 300; | |
| 120 | 125 | this.moderator = options.moderator; |
| 121 | 126 | if (this.moderator) { |
| 122 | 127 | this.moderator.attach({ |
| @@ -248,6 +253,7 @@ export class Lobby { | ||
| 248 | 253 | |
| 249 | 254 | close(): void { |
| 250 | 255 | clearInterval(this.pinger); |
| 256 | clearTimeout(this.presenceTimer); | |
| 251 | 257 | for (const s of this.seats) s.ws.terminate(); |
| 252 | 258 | this.wss.close(); |
| 253 | 259 | this.turns.dispose(); |
| @@ -260,6 +266,8 @@ export class Lobby { | ||
| 260 | 266 | this.seats.add(seat); |
| 261 | 267 | if (who) this.turns.connect(who.id); |
| 262 | 268 | this.send(seat, this.hello(seat)); |
| 269 | this.send(seat, this.presence()); | |
| 270 | this.presenceChanged(); | |
| 263 | 271 | this.send(seat, this.state()); |
| 264 | 272 | this.send(seat, this.chatLog()); |
| 265 | 273 | if (who?.owner) { |
| @@ -290,6 +298,7 @@ export class Lobby { | ||
| 290 | 298 | }); |
| 291 | 299 | ws.on("close", () => { |
| 292 | 300 | this.seats.delete(seat); |
| 301 | this.presenceChanged(); | |
| 293 | 302 | if (who) this.turns.disconnect(who.id); |
| 294 | 303 | }); |
| 295 | 304 | ws.on("error", () => ws.terminate()); |
| @@ -432,6 +441,23 @@ export class Lobby { | ||
| 432 | 441 | this.broadcastState(); |
| 433 | 442 | } |
| 434 | 443 | |
| 444 | /** Who has the room open: everyone, those signed in (a name), and those only watching. */ | |
| 445 | private presence() { | |
| 446 | let signedIn = 0; | |
| 447 | for (const s of this.seats) if (s.who) signedIn++; | |
| 448 | return { type: "presence", total: this.seats.size, signedIn, anonymous: this.seats.size - signedIn }; | |
| 449 | } | |
| 450 | ||
| 451 | /** A connect or disconnect: the room hears the new counts once the burst is over, not once per socket. */ | |
| 452 | private presenceChanged(): void { | |
| 453 | if (this.presenceTimer) return; | |
| 454 | this.presenceTimer = setTimeout(() => { | |
| 455 | this.presenceTimer = undefined; | |
| 456 | this.broadcast(this.presence()); | |
| 457 | }, this.presenceMs); | |
| 458 | this.presenceTimer.unref?.(); | |
| 459 | } | |
| 460 | ||
| 435 | 461 | private state() { |
| 436 | 462 | return { type: "state", ...this.turns.snapshot(), announcement: this.admin.settings.announcement, maxSpeedPercent: this.admin.settings.maxSpeedPercent }; |
| 437 | 463 | } |
Software/so101/src/ui/lobby-panel.js +1
| @@ -251,6 +251,7 @@ export function createRoom(page) { | ||
| 251 | 251 | const m = JSON.parse(e.data); |
| 252 | 252 | if (m.type === "ping") return say({ type: "pong" }); |
| 253 | 253 | if (m.type === "status") { lobby.statusSeen = true; return m.status ? page.applyStatus(m.status) : page.noStatus(); } |
| 254 | if (m.type === "presence") return void ($("presence").textContent = `${m.total} here · ${m.signedIn} signed in · ${m.anonymous} watching`); | |
| 254 | 255 | if (m.type === "hello") lobby.hello = m; |
| 255 | 256 | else if (m.type === "state") { lobby.offset = m.now - Date.now(); lobby.state = m; } |
| 256 | 257 | else if (m.type === "chat") return addChat(m.messages, true), showEarlier(m.more); |
Software/so101/src/viewer.html +2 −1
| @@ -89,6 +89,7 @@ | ||
| 89 | 89 | #strip .who { flex: 1; min-width: 0; } |
| 90 | 90 | #driverLine { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #driverLine.none { color: var(); font-weight: 500; } |
| 91 | 91 | #clock { color: var(); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #clock.idle { color: var(); } |
| 92 | #presence { color: var(); font-size: 12px; line-height: 16px; min-height: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; } | |
| 92 | 93 | #capLine { color: var(); font-size: 12px; } #capLine:empty { display: none; } |
| 93 | 94 | #armLine { display: none; gap: 6px; align-items: center; color: var(); font-size: 12px; white-space: nowrap; } #armText { min-width: 0; overflow: hidden; text-overflow: ellipsis; } /* shown on a phone held upright (ui/mobile.css) */ |
| 94 | 95 | #reconnect { align-self: flex-start; padding: 0 8px; border-radius: 9px; font-size: 11px; line-height: 16px; background: var(); color: #000; visibility: hidden; } #reconnect.on { visibility: visible; } |
| @@ -326,7 +327,7 @@ | ||
| 326 | 327 | |
| 327 | 328 | <section id="strip" aria-label="Who drives" aria-live="polite"> |
| 328 | 329 | <div id="ring" hidden aria-hidden="true"><span id="ringN"></span></div> |
| 329 | <div class="who"><div id="driverLine" class="none">Connecting…</div><span id="reconnect" role="status">reconnecting…</span><div id="clock" aria-live="off"></div><div id="capLine"></div><div id="armLine" aria-live="off"><span class="dot" id="armDot"></span><span id="armText"></span></div></div> | |
| 330 | <div class="who"><div id="driverLine" class="none">Connecting…</div><span id="reconnect" role="status">reconnecting…</span><div id="clock" aria-live="off"></div><div id="presence" aria-live="off"></div><div id="capLine"></div><div id="armLine" aria-live="off"><span class="dot" id="armDot"></span><span id="armText"></span></div></div> | |
| 330 | 331 | <div id="turnActions"></div> |
| 331 | 332 | <button id="sheetToggle" hidden aria-controls="sheet" aria-expanded="true">Hide</button> |
| 332 | 333 | <button id="stop" class="estop" data-stop title="Stop (space or Esc): halt everything, hold with torque, until someone resumes" aria-label="Stop the arm">Stop</button> |
new above
offline