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 #297
smith/arm:smith/291-everyone-sees-12-here-4-signed-in-8-watc → main 5 commits fork 2 threads · 2 unresolved
9 files
changed +87
−6
Software/README.md +3
| @@ -497,6 +497,8 @@ on. While you drive, a ring around the seconds counts the time left, and | ||
| 497 | 497 | **+30 s** adds time while nobody waits. The countdown text is not read out |
| 498 | 498 | by screen readers; the call is announced once. |
| 499 | 499 | |
| 500 | **The presence line.** The strip shows `12 here · 4 signed in · 8 watching` on one line under the clock, updated live over `/live`; its 16px slot is reserved in the page so nothing moves when it fills. It is drawn by JavaScript, so without JavaScript the line stays empty (the terminal has `/here`). | |
| 501 | ||
| 500 | 502 | **The terminal.** The room also has a terminal door (`src/tty.ts`, the |
| 501 | 503 | `tty` in `forge.json`). Connect with the exact command the forge gives in |
| 502 | 504 | `FORGE_APP_SSH`; when the forge has |
| @@ -526,6 +528,7 @@ watches. A line you type is said in the chat; commands start with `/`: | ||
| 526 | 528 | command in and out and every clamp, line by line as it happens. `/machine` |
| 527 | 529 | again prints the joints again; `/machine off` stops the stream. Anyone may, |
| 528 | 530 | guests too: it is read-only. |
| 531 | - `/here`: the page's presence line, `12 here · 4 signed in · 8 watching`, as the room last told it (counted from the live connections, coalesced to a few frames a second). | |
| 529 | 532 | - `/who` (who drives, who waits), `/help`, `/quit`. |
| 530 | 533 | |
| 531 | 534 | The terminal is a client of the running server, like the page: it opens |
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
| @@ -23,7 +23,7 @@ | ||
| 23 | 23 | * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused |
| 24 | 24 | * {"type": "pong"} the answer to {"type": "ping"} |
| 25 | 25 | * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one |
| 26 | * 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), | |
| 26 | * 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), | |
| 27 | 27 | * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way), |
| 28 | 28 | * said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only: |
| 29 | 29 | * settings, hidden lines, mute suggestions), banned and words (owners only), ping, error; and whatever the relay |
| @@ -71,6 +71,8 @@ export interface LobbyOptions { | ||
| 71 | 71 | /** How often each socket is pinged, and how long it may stay silent before it counts as dropped. */ |
| 72 | 72 | pingMs?: number; |
| 73 | 73 | silentMs?: number; |
| 74 | /** Connects and disconnects within this many ms are told to the room as one presence frame. */ | |
| 75 | presenceMs?: number; | |
| 74 | 76 | } |
| 75 | 77 | |
| 76 | 78 | interface Seat { |
| @@ -105,6 +107,8 @@ export class Lobby { | ||
| 105 | 107 | private readonly arriveListeners = new Set<(tell: (text: string) => void) => void>(); |
| 106 | 108 | private readonly pinger: ReturnType<typeof setInterval>; |
| 107 | 109 | private readonly silentMs: number; |
| 110 | private readonly presenceMs: number; | |
| 111 | private presenceTimer: ReturnType<typeof setTimeout> | undefined; | |
| 108 | 112 | private lastActivityState = 0; |
| 109 | 113 | private readonly now: () => number; |
| 110 | 114 | |
| @@ -120,6 +124,7 @@ export class Lobby { | ||
| 120 | 124 | this.accounts = options.accounts ?? new Accounts(); |
| 121 | 125 | this.filter = new WordFilter(options.wordsFile); |
| 122 | 126 | this.silentMs = options.silentMs ?? 15_000; |
| 127 | this.presenceMs = options.presenceMs ?? 300; | |
| 123 | 128 | this.moderator = options.moderator; |
| 124 | 129 | if (this.moderator) { |
| 125 | 130 | this.moderator.attach({ |
| @@ -251,6 +256,7 @@ export class Lobby { | ||
| 251 | 256 | |
| 252 | 257 | close(): void { |
| 253 | 258 | clearInterval(this.pinger); |
| 259 | clearTimeout(this.presenceTimer); | |
| 254 | 260 | for (const s of this.seats) s.ws.terminate(); |
| 255 | 261 | this.wss.close(); |
| 256 | 262 | this.turns.dispose(); |
| @@ -263,6 +269,8 @@ export class Lobby { | ||
| 263 | 269 | this.seats.add(seat); |
| 264 | 270 | if (who) this.turns.connect(who.id); |
| 265 | 271 | this.send(seat, this.hello(seat)); |
| 272 | this.send(seat, this.presence()); | |
| 273 | this.presenceChanged(); | |
| 266 | 274 | this.send(seat, this.state()); |
| 267 | 275 | this.send(seat, this.chatLog()); |
| 268 | 276 | if (who?.owner) { |
| @@ -293,6 +301,7 @@ export class Lobby { | ||
| 293 | 301 | }); |
| 294 | 302 | ws.on("close", () => { |
| 295 | 303 | this.seats.delete(seat); |
| 304 | this.presenceChanged(); | |
| 296 | 305 | if (who) this.turns.disconnect(who.id); |
| 297 | 306 | }); |
| 298 | 307 | ws.on("error", () => ws.terminate()); |
| @@ -447,6 +456,23 @@ export class Lobby { | ||
| 447 | 456 | this.broadcastState(); |
| 448 | 457 | } |
| 449 | 458 | |
| 459 | /** Who has the room open: everyone, those signed in (a name), and those only watching. */ | |
| 460 | private presence() { | |
| 461 | let signedIn = 0; | |
| 462 | for (const s of this.seats) if (s.who) signedIn++; | |
| 463 | return { type: "presence", total: this.seats.size, signedIn, anonymous: this.seats.size - signedIn }; | |
| 464 | } | |
| 465 | ||
| 466 | /** A connect or disconnect: the room hears the new counts once the burst is over, not once per socket. */ | |
| 467 | private presenceChanged(): void { | |
| 468 | if (this.presenceTimer) return; | |
| 469 | this.presenceTimer = setTimeout(() => { | |
| 470 | this.presenceTimer = undefined; | |
| 471 | this.broadcast(this.presence()); | |
| 472 | }, this.presenceMs); | |
| 473 | this.presenceTimer.unref?.(); | |
| 474 | } | |
| 475 | ||
| 450 | 476 | private state() { |
| 451 | 477 | return { type: "state", ...this.turns.snapshot(), announcement: this.admin.settings.announcement, maxSpeedPercent: this.admin.settings.maxSpeedPercent }; |
| 452 | 478 | } |
Software/so101/src/terminal.test.ts +9
| @@ -450,3 +450,12 @@ describe("terminal door", () => { | ||
| 450 | 450 | await waitFor(() => (alice.printed.filter((l) => /^resumed/.test(l)).length === 2 ? true : undefined), "the owner's resume on alice's terminal"); |
| 451 | 451 | }); |
| 452 | 452 | }); |
| 453 | ||
| 454 | test("/here prints the presence line as the page words it, once the room has told it", () => { | |
| 455 | const room = emptyRoom(); | |
| 456 | expect((typed(room, "/here") as { print: string }).print).toBe("no count from the room yet"); | |
| 457 | expect(hear(room, { type: "presence", total: 12, signedIn: 4, anonymous: 8 })).toEqual([]); | |
| 458 | expect((typed(room, "/here") as { print: string }).print).toBe("12 here · 4 signed in · 8 watching"); | |
| 459 | hear(room, { type: "presence", total: -1, signedIn: "x", anonymous: 8 }); | |
| 460 | expect(room.presence).toBe("12 here · 4 signed in · 8 watching"); | |
| 461 | }); |
Software/so101/src/terminal.ts +16 −2
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | * /table <mm> admin: set the table offset, -100..100 mm (PUT /settings tableOffsetMm) |
| 29 | 29 | * /machine [off] the Machine panel (/machine, src/machine-feed.ts): the joints, the link and the server as they are, |
| 30 | 30 | * then every command in and out as it happens, in the page's own words (src/ui/machine.js); off stops it |
| 31 | * /here how many are here, signed in and watching (the page's presence line; also printed when it changes) | |
| 31 | 32 | * /who /help /quit |
| 32 | 33 | * <who> is a name the door has seen (driving, in line, in the chat) or a user id. |
| 33 | 34 | */ |
| @@ -35,6 +36,7 @@ | ||
| 35 | 36 | import WebSocket from "ws"; |
| 36 | 37 | import { UNPRINTABLE } from "./chat.ts"; |
| 37 | 38 | import { ID, NAME, sign } from "./identity.ts"; |
| 39 | import { presenceLine } from "./ui/presence.js"; | |
| 38 | 40 | import { entryLine, jointRows, linkRows, stateRows } from "./ui/machine.js"; |
| 39 | 41 | |
| 40 | 42 | export type Env = Record<string, string | undefined>; |
| @@ -111,11 +113,13 @@ export interface Room { | ||
| 111 | 113 | told: { called: boolean; next: boolean }; |
| 112 | 114 | /** The latest chat line of each author, by user id: what `/delete <who>` removes. */ |
| 113 | 115 | latest: Map<string, number>; |
| 116 | /** The presence line as last told, null until the first frame. */ | |
| 117 | presence: string | null; | |
| 114 | 118 | /** Everyone seen by name, most recent last: to turn a typed name into an id. */ |
| 115 | 119 | seen: Map<string, Someone>; |
| 116 | 120 | } |
| 117 | 121 | |
| 118 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map() }); | |
| 122 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map(), presence: null }); | |
| 119 | 123 | |
| 120 | 124 | const someone = (x: unknown): Someone | null => { |
| 121 | 125 | if (typeof x !== "object" || x === null) return null; |
| @@ -139,7 +143,7 @@ const said = (room: Room, m: unknown): string => { | ||
| 139 | 143 | return from ? `${tag}${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`; |
| 140 | 144 | }; |
| 141 | 145 | |
| 142 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /mute <who> /unmute <who> /table here|<mm> (owner) · /delete <who|#id> (owner, moderator) · /removed /restore #<id> (owner) · /machine [off] · /who · /quit"; | |
| 146 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /mute <who> /unmute <who> /table here|<mm> (owner) · /delete <who|#id> (owner, moderator) · /removed /restore #<id> (owner) · /machine [off] · /here · /who · /quit"; | |
| 143 | 147 | |
| 144 | 148 | /** The Machine feed's socket beside the lobby's: same app, same port. */ |
| 145 | 149 | export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine"); |
| @@ -228,6 +232,14 @@ export function hear(room: Room, frame: Frame): string[] { | ||
| 228 | 232 | if (now === was || (was === undefined && now === null)) return []; |
| 229 | 233 | return [now ?? "resumed: the arm may move again, from a new command"]; |
| 230 | 234 | } |
| 235 | case "presence": { | |
| 236 | const n = (x: unknown) => (typeof x === "number" && Number.isSafeInteger(x) && x >= 0 ? x : null); | |
| 237 | const [total, signedIn, anonymous] = [n(frame.total), n(frame.signedIn), n(frame.anonymous)]; | |
| 238 | if (total === null || signedIn === null || anonymous === null) return []; | |
| 239 | // Kept for /here, not printed: the room changes often and a terminal scrolls. | |
| 240 | room.presence = presenceLine({ total, signedIn, anonymous }); | |
| 241 | return []; | |
| 242 | } | |
| 231 | 243 | case "unsaid": |
| 232 | 244 | return ["· a line was removed"]; |
| 233 | 245 | case "removed": { |
| @@ -309,6 +321,8 @@ export function typed(room: Room, input: string): Typed { | ||
| 309 | 321 | return { machine: arg.toLowerCase() !== "off" }; |
| 310 | 322 | case "who": |
| 311 | 323 | return { print: who(room) }; |
| 324 | case "here": | |
| 325 | return { print: room.presence ?? "no count from the room yet" }; | |
| 312 | 326 | case "help": |
| 313 | 327 | return { print: HELP }; |
| 314 | 328 | case "quit": |
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 import("./presence.js").then((p) => p.show(m)); | |
| 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/ui/presence.js added +8
| @@ -0,0 +1,8 @@ | ||
| 1 | /** The one line the strip shows for the room: everyone here, those signed in, those only watching. */ | |
| 2 | export const presenceLine = (/** @type {{total: number, signedIn: number, anonymous: number}} */ p) => `${p.total} here · ${p.signedIn} signed in · ${p.anonymous} watching`; | |
| 3 | ||
| 4 | /** Fill the slot viewer.html reserves (16px, so nothing moves when the line arrives). */ | |
| 5 | export function show(/** @type {{total: number, signedIn: number, anonymous: number}} */ p) { | |
| 6 | const line = document.getElementById("presence"); | |
| 7 | if (line) line.textContent = presenceLine(p); | |
| 8 | } |
Software/so101/src/ui/presence.test.ts added +6
| @@ -0,0 +1,6 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { presenceLine } from "./presence.js"; | |
| 3 | ||
| 4 | test("the presence line reads '12 here · 4 signed in · 8 watching'", () => { | |
| 5 | expect(presenceLine({ total: 12, signedIn: 4, anonymous: 8 })).toBe("12 here · 4 signed in · 8 watching"); | |
| 6 | }); |
Software/so101/src/viewer.html +3 −3
| @@ -89,13 +89,13 @@ | ||
| 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 | #capLine { color: var(); font-size: 12px; } #capLine:empty { display: none; } | |
| 92 | #capLine, #presence { color: var(); font-size: 12px } #presence { height: 16px; overflow: hidden } #capLine:empty { display: none; } | |
| 93 | 93 | #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 | 94 | #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; } |
| 95 | 95 | #driverLine.paused { color: var(); } |
| 96 | 96 | #turnActions { display: flex; gap: 6px; } #turnActions:empty { display: none; } |
| 97 | 97 | .estop { flex: none; min-width: 72px; min-height: 56px; border: 0; border-radius: 12px; background: #d1242f; color: #fff; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; box-shadow: 0 0 0 3px rgba(209,36,47,.25), 0 2px 8px rgba(0,0,0,.35); } |
| 98 | .estop:hover { background: #e5323d; } .estop:active { background: #b81d27; transform: scale(.97); } /* red under a finger too: a touch is hover then active */ | |
| 98 | .estop:hover { background: #e5323d; } .estop:active { background: #b81d27; transform: scale(.97); } | |
| 99 | 99 | #sheetToggle { flex: none; } |
| 100 | 100 | /* the turn ring: time left as a sweep around the seconds; the driver's name over the video for everyone */ |
| 101 | 101 | #ring { flex: none; width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center; background: conic-gradient(var() var(, 0%), var() 0); font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; position: relative; } |
| @@ -310,7 +310,7 @@ | ||
| 310 | 310 | |
| 311 | 311 | <section id="strip" aria-label="Who drives" aria-live="polite"> |
| 312 | 312 | <div id="ring" hidden aria-hidden="true"><span id="ringN"></span></div> |
| 313 | <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> | |
| 313 | <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" role="status" 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> | |
| 314 | 314 | <div id="turnActions"></div> |
| 315 | 315 | <button id="sheetToggle" hidden aria-controls="sheet" aria-expanded="true">Hide</button> |
| 316 | 316 | <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