Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
/live sends everyone a coalesced presence frame {type:'presence', here, signedIn, watching} within 1 s of a connect or disconnect - lobby and test only #368
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/358-live-sends-everyone-a-coalesced-presence → main 1 commit fork
2 files
changed +81
−0
Software/so101/src/lobby.test.ts +54
| @@ -1029,3 +1029,57 @@ describe("lobby", () => { | ||
| 1029 | 1029 | }); |
| 1030 | 1030 | }); |
| 1031 | 1031 | }); |
| 1032 | ||
| 1033 | describe("lobby presence", () => { | |
| 1034 | let relay: RunningRelay | undefined; | |
| 1035 | let lobby: Lobby | undefined; | |
| 1036 | const sockets: WebSocket[] = []; | |
| 1037 | afterEach(async () => { | |
| 1038 | for (const s of sockets.splice(0)) s.terminate(); | |
| 1039 | await shut(relay); | |
| 1040 | lobby?.close(); | |
| 1041 | relay = lobby = undefined; | |
| 1042 | }); | |
| 1043 | const start = async () => { | |
| 1044 | lobby = new Lobby({ identityKey: KEY, turns: new Turns(), chat: new Chat(), admin: new Admin(), accounts: new Accounts(), presenceMs: 60 }); | |
| 1045 | relay = await startRelay({ port: 0, simulation: `http://127.0.0.1:${simulation.port}`, lobby }); | |
| 1046 | return relay.port; | |
| 1047 | }; | |
| 1048 | const join = async (port: number, headers: Record<string, string> = {}) => { | |
| 1049 | const c = await client(port, headers); | |
| 1050 | sockets.push(c.ws); | |
| 1051 | return c; | |
| 1052 | }; | |
| 1053 | const frames = (c: { heard: Msg[] }) => c.heard.filter((m) => m.type === "presence"); | |
| 1054 | ||
| 1055 | test("a new socket gets the current counts at once; they rise on connect and fall on disconnect", async () => { | |
| 1056 | const port = await start(); | |
| 1057 | const a = await join(port); | |
| 1058 | const hello = await a.first((m) => m.type === "presence"); | |
| 1059 | expect(hello).toEqual({ type: "presence", here: 1, signedIn: 0, watching: 1 }); | |
| 1060 | const b = await join(port, ALICE); | |
| 1061 | expect(await b.first((m) => m.type === "presence")).toEqual({ type: "presence", here: 2, signedIn: 1, watching: 1 }); | |
| 1062 | expect(await a.next((m) => m.type === "presence" && m.here === 2)).toEqual({ type: "presence", here: 2, signedIn: 1, watching: 1 }); | |
| 1063 | b.ws.close(); | |
| 1064 | expect(await a.next((m) => m.type === "presence" && m.here === 1)).toEqual({ type: "presence", here: 1, signedIn: 0, watching: 1 }); | |
| 1065 | }); | |
| 1066 | ||
| 1067 | test("two tabs of one signed-in person count once in signedIn, and the frame holds no names", async () => { | |
| 1068 | const port = await start(); | |
| 1069 | const watcher = await join(port); | |
| 1070 | await join(port, ALICE); | |
| 1071 | await join(port, ALICE); | |
| 1072 | const m = await watcher.next((x) => x.type === "presence" && x.here === 3); | |
| 1073 | expect(m).toEqual({ type: "presence", here: 3, signedIn: 1, watching: 1 }); | |
| 1074 | expect(JSON.stringify(m)).not.toContain("alice"); | |
| 1075 | }); | |
| 1076 | ||
| 1077 | test("a burst of five connects is one frame to those already there", async () => { | |
| 1078 | const port = await start(); | |
| 1079 | const watcher = await join(port); | |
| 1080 | await watcher.first((m) => m.type === "presence"); | |
| 1081 | for (let i = 0; i < 5; i++) await join(port); | |
| 1082 | await sleep(250); | |
| 1083 | expect(frames(watcher).map((m) => m.here)).toEqual([1, 6]); | |
| 1084 | }); | |
| 1085 | }); |
Software/so101/src/lobby.ts +27
| @@ -83,6 +83,8 @@ export interface LobbyOptions { | ||
| 83 | 83 | /** How often each socket is pinged, and how long it may stay silent before it counts as dropped. */ |
| 84 | 84 | pingMs?: number; |
| 85 | 85 | silentMs?: number; |
| 86 | /** The most often a presence frame goes out after a connect or disconnect (default 1000). */ | |
| 87 | presenceMs?: number; | |
| 86 | 88 | } |
| 87 | 89 | |
| 88 | 90 | interface Seat { |
| @@ -123,10 +125,13 @@ export class Lobby { | ||
| 123 | 125 | private readonly pinger: ReturnType<typeof setInterval>; |
| 124 | 126 | private readonly silentMs: number; |
| 125 | 127 | private lastActivityState = 0; |
| 128 | private readonly presenceMs: number; | |
| 129 | private presenceTimer: ReturnType<typeof setTimeout> | undefined; | |
| 126 | 130 | private readonly now: () => number; |
| 127 | 131 | |
| 128 | 132 | constructor(options: LobbyOptions = {}) { |
| 129 | 133 | this.now = options.now ?? Date.now; |
| 134 | this.presenceMs = options.presenceMs ?? 1000; | |
| 130 | 135 | this.key = options.identityKey ?? ""; |
| 131 | 136 | this.local = Boolean(options.local); |
| 132 | 137 | this.admin = options.admin ?? new Admin({ file: options.adminFile }); |
| @@ -274,6 +279,7 @@ export class Lobby { | ||
| 274 | 279 | |
| 275 | 280 | close(): void { |
| 276 | 281 | clearInterval(this.pinger); |
| 282 | clearTimeout(this.presenceTimer); | |
| 277 | 283 | for (const s of this.seats) s.ws.terminate(); |
| 278 | 284 | this.wss.close(); |
| 279 | 285 | this.turns.dispose(); |
| @@ -284,9 +290,11 @@ export class Lobby { | ||
| 284 | 290 | private seat(ws: WebSocket, who: Who, from: Visitor, verified: boolean): void { |
| 285 | 291 | const seat: Seat = { ws, who, from, verified, heard: Date.now() }; |
| 286 | 292 | this.seats.add(seat); |
| 293 | this.presenceChanged(); | |
| 287 | 294 | if (who) this.turns.connect(who.id); |
| 288 | 295 | this.send(seat, this.hello(seat)); |
| 289 | 296 | this.send(seat, this.state()); |
| 297 | this.send(seat, this.presence()); | |
| 290 | 298 | this.send(seat, this.chatLog()); |
| 291 | 299 | if (this.mayHearMuted(seat)) { |
| 292 | 300 | this.send(seat, this.mutedList()); |
| @@ -320,6 +328,7 @@ export class Lobby { | ||
| 320 | 328 | }); |
| 321 | 329 | ws.on("close", () => { |
| 322 | 330 | this.seats.delete(seat); |
| 331 | this.presenceChanged(); | |
| 323 | 332 | this.reports.drop(seat); |
| 324 | 333 | if (who) this.turns.disconnect(who.id); |
| 325 | 334 | }); |
| @@ -621,6 +630,24 @@ export class Lobby { | ||
| 621 | 630 | return this.seats.size; |
| 622 | 631 | } |
| 623 | 632 | |
| 633 | /** Counts only: no names, no addresses. A person with several tabs is one in `signedIn`. */ | |
| 634 | private presence(): { type: "presence"; here: number; signedIn: number; watching: number } { | |
| 635 | const people = new Set<string>(); | |
| 636 | let watching = 0; | |
| 637 | for (const s of this.seats) s.who ? people.add(s.who.id) : watching++; | |
| 638 | return { type: "presence", here: this.seats.size, signedIn: people.size, watching }; | |
| 639 | } | |
| 640 | ||
| 641 | /** One frame per window however many connects and disconnects happened in it. */ | |
| 642 | private presenceChanged(): void { | |
| 643 | if (this.presenceTimer) return; | |
| 644 | this.presenceTimer = setTimeout(() => { | |
| 645 | this.presenceTimer = undefined; | |
| 646 | this.broadcast(this.presence()); | |
| 647 | }, this.presenceMs); | |
| 648 | this.presenceTimer.unref?.(); | |
| 649 | } | |
| 650 | ||
| 624 | 651 | private send(seat: Seat, body: unknown): void { |
| 625 | 652 | if (seat.ws.readyState === WebSocket.OPEN) seat.ws.send(JSON.stringify(body)); |
| 626 | 653 | } |
new above
offline