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

Parked opened this
Closed without merging

smith/arm:smith/291-everyone-sees-12-here-4-signed-in-8-watc → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +44 −2
Software/so101/src/lobby.test.ts +14
@@ -105,6 +105,20 @@ describe("lobby", () => {
105105 expect((await anon.next((m) => m.type === "error")).error).toContain("join to take part");
106106 });
107107
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
108122 test("a forged identity header is nobody", async () => {
109123 const port = await start();
110124 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 @@
2222 * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused
2323 * {"type": "pong"} the answer to {"type": "ping"}
2424 * {"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),
2626 * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way),
2727 * said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only:
2828 * settings, hidden lines, mute suggestions), banned and words (owners only), ping, error; and whatever the relay
@@ -68,6 +68,8 @@ export interface LobbyOptions {
6868 /** How often each socket is pinged, and how long it may stay silent before it counts as dropped. */
6969 pingMs?: number;
7070 silentMs?: number;
71 /** Connects and disconnects within this many ms are told to the room as one presence frame. */
72 presenceMs?: number;
7173 }
7274
7375 interface Seat {
@@ -102,6 +104,8 @@ export class Lobby {
102104 private readonly arriveListeners = new Set<(tell: (text: string) => void) => void>();
103105 private readonly pinger: ReturnType<typeof setInterval>;
104106 private readonly silentMs: number;
107 private readonly presenceMs: number;
108 private presenceTimer: ReturnType<typeof setTimeout> | undefined;
105109 private lastActivityState = 0;
106110 private readonly now: () => number;
107111
@@ -117,6 +121,7 @@ export class Lobby {
117121 this.accounts = options.accounts ?? new Accounts();
118122 this.filter = new WordFilter(options.wordsFile);
119123 this.silentMs = options.silentMs ?? 15_000;
124 this.presenceMs = options.presenceMs ?? 300;
120125 this.moderator = options.moderator;
121126 if (this.moderator) {
122127 this.moderator.attach({
@@ -248,6 +253,7 @@ export class Lobby {
248253
249254 close(): void {
250255 clearInterval(this.pinger);
256 clearTimeout(this.presenceTimer);
251257 for (const s of this.seats) s.ws.terminate();
252258 this.wss.close();
253259 this.turns.dispose();
@@ -260,6 +266,8 @@ export class Lobby {
260266 this.seats.add(seat);
261267 if (who) this.turns.connect(who.id);
262268 this.send(seat, this.hello(seat));
269 this.send(seat, this.presence());
270 this.presenceChanged();
263271 this.send(seat, this.state());
264272 this.send(seat, this.chatLog());
265273 if (who?.owner) {
@@ -290,6 +298,7 @@ export class Lobby {
290298 });
291299 ws.on("close", () => {
292300 this.seats.delete(seat);
301 this.presenceChanged();
293302 if (who) this.turns.disconnect(who.id);
294303 });
295304 ws.on("error", () => ws.terminate());
@@ -432,6 +441,23 @@ export class Lobby {
432441 this.broadcastState();
433442 }
434443
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
435461 private state() {
436462 return { type: "state", ...this.turns.snapshot(), announcement: this.admin.settings.announcement, maxSpeedPercent: this.admin.settings.maxSpeedPercent };
437463 }
Software/so101/src/ui/lobby-panel.js +1
@@ -251,6 +251,7 @@ export function createRoom(page) {
251251 const m = JSON.parse(e.data);
252252 if (m.type === "ping") return say({ type: "pong" });
253253 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`);
254255 if (m.type === "hello") lobby.hello = m;
255256 else if (m.type === "state") { lobby.offset = m.now - Date.now(); lobby.state = m; }
256257 else if (m.type === "chat") return addChat(m.messages, true), showEarlier(m.more);
Software/so101/src/viewer.html +2 −1
@@ -89,6 +89,7 @@
8989 #strip .who { flex: 1; min-width: 0; }
9090 #driverLine { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #driverLine.none { color: var(--dim); font-weight: 500; }
9191 #clock { color: var(--dim); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #clock.idle { color: var(--warn); }
92 #presence { color: var(--dim); font-size: 12px; line-height: 16px; min-height: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
9293 #capLine { color: var(--dim); font-size: 12px; } #capLine:empty { display: none; }
9394 #armLine { display: none; gap: 6px; align-items: center; color: var(--dim); font-size: 12px; white-space: nowrap; } #armText { min-width: 0; overflow: hidden; text-overflow: ellipsis; } /* shown on a phone held upright (ui/mobile.css) */
9495 #reconnect { align-self: flex-start; padding: 0 8px; border-radius: 9px; font-size: 11px; line-height: 16px; background: var(--warn); color: #000; visibility: hidden; } #reconnect.on { visibility: visible; }
@@ -326,7 +327,7 @@
326327
327328 <section id="strip" aria-label="Who drives" aria-live="polite">
328329 <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>
330331 <div id="turnActions"></div>
331332 <button id="sheetToggle" hidden aria-controls="sheet" aria-expanded="true">Hide</button>
332333 <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>

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs