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

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/358-live-sends-everyone-a-coalesced-presence → main 1 commit fork

  • Merged. smith/358-live-sends-everyone-a-coalesced-presence was merged into main as 97ab64b.

Review

Sign in to review this change.
2 files changed +81 −0
Software/so101/src/lobby.test.ts +54
@@ -1029,3 +1029,57 @@ describe("lobby", () => {
10291029 });
10301030 });
10311031 });
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 {
8383 /** How often each socket is pinged, and how long it may stay silent before it counts as dropped. */
8484 pingMs?: number;
8585 silentMs?: number;
86 /** The most often a presence frame goes out after a connect or disconnect (default 1000). */
87 presenceMs?: number;
8688 }
8789
8890 interface Seat {
@@ -123,10 +125,13 @@ export class Lobby {
123125 private readonly pinger: ReturnType<typeof setInterval>;
124126 private readonly silentMs: number;
125127 private lastActivityState = 0;
128 private readonly presenceMs: number;
129 private presenceTimer: ReturnType<typeof setTimeout> | undefined;
126130 private readonly now: () => number;
127131
128132 constructor(options: LobbyOptions = {}) {
129133 this.now = options.now ?? Date.now;
134 this.presenceMs = options.presenceMs ?? 1000;
130135 this.key = options.identityKey ?? "";
131136 this.local = Boolean(options.local);
132137 this.admin = options.admin ?? new Admin({ file: options.adminFile });
@@ -274,6 +279,7 @@ export class Lobby {
274279
275280 close(): void {
276281 clearInterval(this.pinger);
282 clearTimeout(this.presenceTimer);
277283 for (const s of this.seats) s.ws.terminate();
278284 this.wss.close();
279285 this.turns.dispose();
@@ -284,9 +290,11 @@ export class Lobby {
284290 private seat(ws: WebSocket, who: Who, from: Visitor, verified: boolean): void {
285291 const seat: Seat = { ws, who, from, verified, heard: Date.now() };
286292 this.seats.add(seat);
293 this.presenceChanged();
287294 if (who) this.turns.connect(who.id);
288295 this.send(seat, this.hello(seat));
289296 this.send(seat, this.state());
297 this.send(seat, this.presence());
290298 this.send(seat, this.chatLog());
291299 if (this.mayHearMuted(seat)) {
292300 this.send(seat, this.mutedList());
@@ -320,6 +328,7 @@ export class Lobby {
320328 });
321329 ws.on("close", () => {
322330 this.seats.delete(seat);
331 this.presenceChanged();
323332 this.reports.drop(seat);
324333 if (who) this.turns.disconnect(who.id);
325334 });
@@ -621,6 +630,24 @@ export class Lobby {
621630 return this.seats.size;
622631 }
623632
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
624651 private send(seat: Seat, body: unknown): void {
625652 if (seat.ws.readyState === WebSocket.OPEN) seat.ws.send(JSON.stringify(body));
626653 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs