-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
713 lines (630 loc) · 26.7 KB
/
server.ts
File metadata and controls
713 lines (630 loc) · 26.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
import type { ServerWebSocket } from "bun";
// --- Types ---
type ClientMessage =
| { type: "create"; clientId: string; maxClients: number; room?: string }
| { type: "join"; clientId: string; room: string }
| { type: "send"; to?: string; data: unknown };
type ServerMessage =
| { type: "created"; room: string }
| { type: "joined"; room: string; clients: string[] }
| { type: "peer_joined"; clientId: string }
| { type: "peer_left"; clientId: string }
| { type: "message"; from: string; data: unknown }
| { type: "error"; message: string };
interface ClientData {
clientId?: string;
room?: string;
origin?: string;
}
interface Room {
maxClients: number;
origin: string;
clients: Map<string, ServerWebSocket<ClientData>>;
}
// --- Constants ---
const { version: VERSION } = await import("./package.json");
const FAVICON_SVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" rx="14" fill="#1a1a1a"/><line x1="32" y1="18" x2="18" y2="42" stroke="#888" stroke-width="3.5" stroke-linecap="round"/><line x1="32" y1="18" x2="46" y2="42" stroke="#888" stroke-width="3.5" stroke-linecap="round"/><line x1="18" y1="42" x2="46" y2="42" stroke="#888" stroke-width="3.5" stroke-linecap="round"/><circle cx="32" cy="18" r="8" fill="#f472b6"/><circle cx="18" cy="42" r="8" fill="#60a5fa"/><circle cx="46" cy="42" r="8" fill="#4ade80"/></svg>`;
// --- State ---
const rooms = new Map<string, Room>();
const serverStartedAt = Date.now();
let draining = false;
// --- Stats ---
interface DayStats {
connections: number;
rooms: number;
}
// origin -> date -> DayStats
const stats = new Map<string, Map<string, DayStats>>();
function incrementStat(origin: string, field: keyof DayStats) {
const date = new Date().toISOString().slice(0, 10);
let originMap = stats.get(origin);
if (!originMap) { originMap = new Map(); stats.set(origin, originMap); }
const s = originMap.get(date);
if (s) s[field]++;
else originMap.set(date, { connections: 0, rooms: 0, [field]: 1 });
}
function dateStr(d: Date): string {
return d.toISOString().slice(0, 10);
}
function pruneStats() {
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - 366);
const cutoffStr = dateStr(cutoff);
for (const [origin, originMap] of stats) {
for (const date of originMap.keys()) {
if (date < cutoffStr) originMap.delete(date);
}
if (originMap.size === 0) stats.delete(origin);
}
}
function aggregateStats(sinceDate: string, origin: string): DayStats {
const result: DayStats = { connections: 0, rooms: 0 };
const originMap = stats.get(origin);
if (!originMap) return result;
for (const [date, s] of originMap) {
if (date < sinceDate) continue;
result.connections += s.connections;
result.rooms += s.rooms;
}
return result;
}
function getStatsPeriods(origin: string) {
const now = new Date();
const d1 = new Date(now); d1.setDate(d1.getDate() - 1);
const d30 = new Date(now); d30.setDate(d30.getDate() - 30);
const d365 = new Date(now); d365.setDate(d365.getDate() - 365);
return {
day1: aggregateStats(dateStr(d1), origin),
days30: aggregateStats(dateStr(d30), origin),
days365: aggregateStats(dateStr(d365), origin),
};
}
function formatUptime(ms: number): string {
const s = Math.floor(ms / 1000);
const days = Math.floor(s / 86400);
const hours = Math.floor((s % 86400) / 3600);
const mins = Math.floor((s % 3600) / 60);
const parts: string[] = [];
if (days > 0) parts.push(`${days}d`);
if (hours > 0) parts.push(`${hours}h`);
parts.push(`${mins}m`);
return parts.join(" ");
}
// --- Room code generation ---
const ROOM_CHARS = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // no ambiguous chars (0/O, 1/I)
function generateRoomCode(): string {
let code: string;
do {
code = "";
for (let i = 0; i < 4; i++) {
code += ROOM_CHARS[Math.floor(Math.random() * ROOM_CHARS.length)];
}
} while (rooms.has(code));
return code;
}
// --- Helpers ---
function send(ws: ServerWebSocket<ClientData>, msg: ServerMessage) {
ws.send(JSON.stringify(msg));
}
function broadcast(room: Room, msg: ServerMessage, exclude?: string) {
const payload = JSON.stringify(msg);
for (const [id, client] of room.clients) {
if (id !== exclude) {
client.send(payload);
}
}
}
function removeFromRoom(ws: ServerWebSocket<ClientData>) {
const { clientId, room: roomCode } = ws.data;
if (!clientId || !roomCode) return;
const room = rooms.get(roomCode);
if (!room) return;
// Only remove if this ws is still the active connection for this clientId
if (room.clients.get(clientId) === ws) {
room.clients.delete(clientId);
broadcast(room, { type: "peer_left", clientId });
if (room.clients.size === 0) {
rooms.delete(roomCode);
}
}
}
// --- Message handlers ---
function handleCreate(ws: ServerWebSocket<ClientData>, msg: { clientId: string; maxClients: number; room?: string }) {
if (draining) {
return send(ws, { type: "error", message: "Server draining" });
}
if (ws.data.room) {
return send(ws, { type: "error", message: "Already in a room" });
}
if (!msg.clientId || typeof msg.clientId !== "string") {
return send(ws, { type: "error", message: "clientId is required" });
}
if (!msg.maxClients || typeof msg.maxClients !== "number" || msg.maxClients < 1) {
return send(ws, { type: "error", message: "maxClients must be a positive number" });
}
const code = msg.room && /^[A-Z]{4}$/.test(msg.room) && !rooms.has(msg.room)
? msg.room
: generateRoomCode();
const origin = ws.data.origin || "unknown";
const room: Room = { maxClients: msg.maxClients, origin, clients: new Map() };
room.clients.set(msg.clientId, ws);
rooms.set(code, room);
ws.data.clientId = msg.clientId;
ws.data.room = code;
incrementStat(origin, "rooms");
send(ws, { type: "created", room: code });
}
function handleJoin(ws: ServerWebSocket<ClientData>, msg: { clientId: string; room: string }) {
if (ws.data.room) {
return send(ws, { type: "error", message: "Already in a room" });
}
if (!msg.clientId || typeof msg.clientId !== "string") {
return send(ws, { type: "error", message: "clientId is required" });
}
if (!msg.room || typeof msg.room !== "string") {
return send(ws, { type: "error", message: "room is required" });
}
const room = rooms.get(msg.room);
if (!room) {
return send(ws, { type: "error", message: "Room not found" });
}
const existingWs = room.clients.get(msg.clientId);
if (existingWs) {
// Reconnect: detach and close the old connection before swapping in the new one
existingWs.data.room = undefined;
existingWs.data.clientId = undefined;
existingWs.close(4000, "replaced");
room.clients.set(msg.clientId, ws);
ws.data.clientId = msg.clientId;
ws.data.room = msg.room;
const clients = Array.from(room.clients.keys());
send(ws, { type: "joined", room: msg.room, clients });
return;
}
if (room.clients.size >= room.maxClients) {
return send(ws, { type: "error", message: "Room is full" });
}
room.clients.set(msg.clientId, ws);
ws.data.clientId = msg.clientId;
ws.data.room = msg.room;
const clients = Array.from(room.clients.keys());
send(ws, { type: "joined", room: msg.room, clients });
broadcast(room, { type: "peer_joined", clientId: msg.clientId }, msg.clientId);
}
function handleSend(ws: ServerWebSocket<ClientData>, msg: { to?: string; data: unknown }) {
const { clientId, room: roomCode } = ws.data;
if (!clientId || !roomCode) {
return send(ws, { type: "error", message: "Not in a room" });
}
const room = rooms.get(roomCode);
if (!room) {
return send(ws, { type: "error", message: "Room not found" });
}
if (msg.to) {
const target = room.clients.get(msg.to);
if (!target) {
return send(ws, { type: "error", message: "Target client not found" });
}
send(target, { type: "message", from: clientId, data: msg.data });
} else {
broadcast(room, { type: "message", from: clientId, data: msg.data }, clientId);
}
}
// --- Status page ---
interface OriginStats {
rooms: number;
clients: number;
}
function getOriginStats(): { roomCount: number; clientCount: number; origins: Map<string, OriginStats> } {
let roomCount = 0;
let clientCount = 0;
const origins = new Map<string, OriginStats>();
for (const room of rooms.values()) {
roomCount++;
const clients = room.clients.size;
clientCount += clients;
const key = room.origin;
const entry = origins.get(key);
if (entry) {
entry.rooms++;
entry.clients += clients;
} else {
origins.set(key, { rooms: 1, clients });
}
}
return { roomCount, clientCount, origins };
}
function fmt(n: number): string {
return n.toLocaleString("en-US");
}
function statusPage(roomCount: number, clientCount: number, origins: Map<string, OriginStats>, ipFamily?: string): string {
pruneStats();
const uptimeMs = Date.now() - serverStartedAt;
const uptime = formatUptime(uptimeMs);
const uptimeDays = uptimeMs / 86_400_000;
const show30 = uptimeDays > 1;
const show365 = uptimeDays > 30;
const label30 = `${Math.min(Math.floor(uptimeDays), 30)}d`;
const label365 = `${Math.min(Math.floor(uptimeDays), 365)}d`;
// Collect all known origins (live + historical)
const allOrigins = new Set([...origins.keys(), ...stats.keys()]);
const isLocalOrigin = (o: string) => /^https?:\/\/(localhost|127\.|192\.168\.|10\.|172\.(1[6-9]|2\d|3[01])\.)/.test(o);
const hasLocal = Array.from(allOrigins).some(isLocalOrigin);
// Compute non-local counts for the header
let publicRooms = 0, publicClients = 0;
for (const [origin, s] of origins) {
if (!isLocalOrigin(origin)) { publicRooms += s.rooms; publicClients += s.clients; }
}
let originsHtml = "";
if (allOrigins.size > 0) {
const rows = Array.from(allOrigins)
.map((origin) => ({ origin, live: origins.get(origin), periods: getStatsPeriods(origin) }))
.sort((a, b) => (b.live?.clients ?? 0) - (a.live?.clients ?? 0) || b.periods.days365.connections - a.periods.days365.connections)
.map(({ origin, live, periods: p }) => {
const name = origin.replace(/^https?:\/\//, "");
const liveLabel = live
? `<span class="badge">${live.rooms} room${live.rooms !== 1 ? "s" : ""}, ${live.clients} client${live.clients !== 1 ? "s" : ""}</span>`
: '';
const h30 = show30 ? `<th>${label30}</th>` : "";
const h365 = show365 ? `<th>${label365}</th>` : "";
const c30 = (s: DayStats) => show30 ? `<td>${fmt(s.connections)}</td>` : "";
const c365 = (s: DayStats) => show365 ? `<td>${fmt(s.connections)}</td>` : "";
const r30 = (s: DayStats) => show30 ? `<td>${fmt(s.rooms)}</td>` : "";
const r365 = (s: DayStats) => show365 ? `<td>${fmt(s.rooms)}</td>` : "";
const isLocal = isLocalOrigin(origin);
const favicon = origin.startsWith("https://")
? `<img class="ofav" src="${origin}/favicon.ico">`
: '';
return `<div class="origin"${isLocal ? ' data-local' : ''}>
<div class="oh"><span class="on">${favicon}<span class="on-text">${name}</span></span>${liveLabel}</div>
<table>
<thead><tr><th></th><th>24h</th>${h30}${h365}</tr></thead>
<tbody>
<tr><td>Connections</td><td>${fmt(p.day1.connections)}</td>${c30(p.days30)}${c365(p.days365)}</tr>
<tr><td>Rooms</td><td>${fmt(p.day1.rooms)}</td>${r30(p.days30)}${r365(p.days365)}</tr>
</tbody>
</table>
</div>`;
})
.join("");
originsHtml = rows;
}
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,${encodeURIComponent(FAVICON_SVG)}">
<title>Party-Sockets</title>
<style>
* { margin: 0; box-sizing: border-box; }
body { font-family: -apple-system, system-ui, sans-serif; background: #0f0f0f; color: #e0e0e0; display: flex; justify-content: center; align-items: center; min-height: 100vh; padding: 0.75rem; }
.card { background: #1a1a1a; border: 1px solid #2a2a2a; border-radius: 12px; padding: 1.25rem; max-width: 420px; width: 100%; }
h1 { font-size: 1.4rem; }
.header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.25rem; }
.status { display: flex; align-items: center; gap: 0.4rem; color: #4ade80; font-size: 0.8rem; white-space: nowrap; }
.dot { width: 8px; height: 8px; background: #4ade80; border-radius: 50%; box-shadow: 0 0 6px #4ade80; flex-shrink: 0; }
.uptime { color: #555; font-size: 0.75rem; margin-bottom: 1.25rem; }
.live { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; margin-bottom: 1rem; }
.stat { background: #222; border-radius: 8px; padding: 0.75rem; text-align: center; }
.sv { font-size: 1.5rem; font-weight: 700; color: #fff; }
.sl { font-size: 0.7rem; color: #888; margin-top: 0.15rem; text-transform: uppercase; letter-spacing: 0.05em; }
.origin { background: #161616; border: 1px solid #222; border-radius: 8px; padding: 0.75rem; margin-bottom: 0.5rem; overflow: hidden; }
.oh { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.4rem; gap: 0.5rem; min-width: 0; }
.on { color: #fff; font-weight: 600; font-size: 0.85rem; display: flex; align-items: center; gap: 0.4rem; min-width: 0; overflow: hidden; }
.on-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ofav { width: 14px; height: 14px; border-radius: 2px; flex-shrink: 0; }
.badge { font-size: 0.7rem; color: #4ade80; white-space: nowrap; flex-shrink: 0; }
.badge.off { color: #555; }
table { width: 100%; border-collapse: collapse; font-size: 0.8rem; font-variant-numeric: tabular-nums; }
th { color: #555; font-weight: 500; text-align: right; padding: 0 0 0.2rem; font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.05em; }
th:first-child { text-align: left; }
td { padding: 0.15rem 0; color: #ccc; }
td:first-child { color: #666; }
td:nth-child(n+2) { text-align: right; }
.footer { display: flex; justify-content: space-between; margin-top: 1rem; }
a { color: #888; font-size: 0.75rem; text-decoration: none; }
a:hover { color: #bbb; }
.ver { color: #555; font-size: 0.75rem; }
#test-section { margin-top: 1rem; }
#test-btn { background: #222; border: 1px solid #333; color: #ccc; padding: 0.5rem 1rem; border-radius: 6px; cursor: pointer; font-size: 0.8rem; width: 100%; }
#test-btn:hover { background: #2a2a2a; border-color: #444; }
#test-btn:disabled { opacity: 0.5; cursor: default; }
#test-chart { width: 100%; height: 100px; margin-top: 0.75rem; display: none; }
#test-stats { display: none; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 0.5rem; margin-top: 0.5rem; font-size: 0.75rem; text-align: center; }
.ts { background: #222; border-radius: 6px; padding: 0.4rem; }
.ts-val { color: #fff; font-weight: 600; }
.ts-label { color: #666; font-size: 0.6rem; text-transform: uppercase; margin-top: 0.1rem; }
.toggle { display: flex; align-items: center; gap: 0.5rem; font-size: 0.75rem; color: #666; margin-bottom: 0.75rem; cursor: pointer; user-select: none; }
.toggle input { display: none; }
.toggle-track { width: 28px; height: 16px; background: #333; border-radius: 8px; position: relative; transition: background 0.2s; flex-shrink: 0; }
.toggle input:checked + .toggle-track { background: #4ade80; }
.toggle-track::after { content: ''; position: absolute; top: 2px; left: 2px; width: 12px; height: 12px; background: #fff; border-radius: 50%; transition: transform 0.2s; }
.toggle input:checked + .toggle-track::after { transform: translateX(12px); }
html.hide-local [data-local] { display: none; }
@media (min-width: 500px) { .card { padding: 2rem; max-width: 540px; } }
@media (max-width: 360px) {
#test-stats { grid-template-columns: 1fr 1fr; }
table { font-size: 0.7rem; }
}
</style>
<script>if(localStorage.getItem('show-local')!=='1')document.documentElement.classList.add('hide-local');</script>
</head>
<body>
<div class="card" id="card">
<div class="header">
<h1>Party-Sockets</h1>
<div class="status"><span class="dot"></span> Online</div>
</div>
<div class="uptime">Uptime ${uptime}${ipFamily ? ` · ${ipFamily}` : ''}</div>
<div class="live">
<div class="stat"><div class="sv" data-all="${roomCount}" data-public="${publicRooms}">${publicRooms}</div><div class="sl">Rooms</div></div>
<div class="stat"><div class="sv" data-all="${clientCount}" data-public="${publicClients}">${publicClients}</div><div class="sl">Clients</div></div>
</div>
${hasLocal ? '<label class="toggle"><input type="checkbox" id="show-local"><span class="toggle-track"></span> Show local origins</label>' : ''}${originsHtml}
<div id="test-section">
<button id="test-btn">Test Latency</button>
<canvas id="test-chart"></canvas>
<div id="test-stats"></div>
</div>
<div class="footer"><a href="https://github.com/tim4724/Party-Sockets">GitHub</a> <span class="ver">${VERSION}</span></div>
</div>
<script>
function runTest() {
const btn = document.getElementById('test-btn');
const canvas = document.getElementById('test-chart');
const statsEl = document.getElementById('test-stats');
const ctx = canvas.getContext('2d');
btn.disabled = true;
btn.textContent = 'Connecting...';
canvas.style.display = 'block';
statsEl.style.display = 'grid';
statsEl.innerHTML = ['Min','Avg','Max','Jitter'].map(l => '<div class="ts"><div class="ts-val" style="color:#555">-</div><div class="ts-label">' + l + '</div></div>').join('');
const dpr = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
ctx.scale(dpr, dpr);
const W = rect.width, H = rect.height;
const samples = [];
const DURATION = 15000;
const INTERVAL = 200;
let pending = null;
let startTime = null;
function getColor(avg) {
if (avg < 50) return '#4ade80';
if (avg < 150) return '#facc15';
return '#f87171';
}
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
const ws = new WebSocket(proto + '//' + location.host);
const clientId = 'test-' + Math.random().toString(36).slice(2, 8);
function drawChart() {
ctx.clearRect(0, 0, W, H);
if (samples.length < 2) return;
const maxMs = Math.max(...samples.map(s => s.rtt), 10);
const yScale = (H - 20) / maxMs;
const xScale = W / DURATION;
// Grid lines
ctx.strokeStyle = '#2a2a2a';
ctx.lineWidth = 0.5;
const gridLines = [0.25, 0.5, 0.75];
for (const g of gridLines) {
const gy = H - 10 - (maxMs * g * yScale);
ctx.beginPath(); ctx.moveTo(0, gy); ctx.lineTo(W, gy); ctx.stroke();
}
// Y-axis labels
ctx.fillStyle = '#555';
ctx.font = '9px -apple-system, system-ui, sans-serif';
ctx.textAlign = 'right';
ctx.fillText(Math.round(maxMs) + 'ms', W - 2, 12);
ctx.fillText('0', W - 2, H - 2);
// Line with color changing at threshold crossings
ctx.lineWidth = 1.5;
ctx.lineCap = 'round';
const thresholds = [50, 150];
for (let i = 1; i < samples.length; i++) {
const x0 = samples[i-1].t * xScale, y0 = H - 10 - (samples[i-1].rtt * yScale);
const x1 = samples[i].t * xScale, y1 = H - 10 - (samples[i].rtt * yScale);
const rtt0 = samples[i-1].rtt, rtt1 = samples[i].rtt;
// Find threshold crossings and sort by position
const splits = [0];
for (const th of thresholds) {
if ((rtt0 < th && rtt1 >= th) || (rtt0 >= th && rtt1 < th)) {
splits.push((th - rtt0) / (rtt1 - rtt0));
}
}
splits.push(1);
splits.sort((a, b) => a - b);
for (let j = 1; j < splits.length; j++) {
const t0 = splits[j-1], t1 = splits[j];
const sx0 = x0 + (x1 - x0) * t0, sy0 = y0 + (y1 - y0) * t0;
const sx1 = x0 + (x1 - x0) * t1, sy1 = y0 + (y1 - y0) * t1;
const midRtt = rtt0 + (rtt1 - rtt0) * ((t0 + t1) / 2);
ctx.strokeStyle = getColor(midRtt);
ctx.beginPath(); ctx.moveTo(sx0, sy0); ctx.lineTo(sx1, sy1); ctx.stroke();
}
}
// Dots colored individually
for (const s of samples) {
const x = s.t * xScale;
const y = H - 10 - (s.rtt * yScale);
ctx.fillStyle = getColor(s.rtt);
ctx.beginPath(); ctx.arc(x, y, 2, 0, Math.PI * 2); ctx.fill();
}
}
function showStats() {
const rtts = samples.map(s => s.rtt);
const min = Math.min(...rtts);
const avg = rtts.reduce((a, b) => a + b, 0) / rtts.length;
const sorted = [...rtts].sort((a, b) => a - b);
const p95 = sorted[Math.floor(sorted.length * 0.95)];
const jitter = rtts.reduce((sum, r) => sum + Math.abs(r - avg), 0) / rtts.length;
statsEl.style.display = 'grid';
statsEl.innerHTML = [
['Min', min, min.toFixed(1) + 'ms'],
['Avg', avg, avg.toFixed(1) + 'ms'],
['P95', p95, p95.toFixed(1) + 'ms'],
['Jitter', jitter, jitter.toFixed(1) + 'ms'],
].map(([l, n, v]) => '<div class="ts"><div class="ts-val" style="color:' + getColor(n) + '">' + v + '</div><div class="ts-label">' + l + '</div></div>').join('');
}
function finish() {
ws.close();
btn.disabled = false;
btn.textContent = 'Test Again';
}
ws.onopen = () => {
ws.send(JSON.stringify({ type: 'create', clientId, maxClients: 1 }));
};
ws.onmessage = (e) => {
const msg = JSON.parse(e.data);
if (msg.type === 'created') {
startTime = performance.now();
const iv = setInterval(() => {
const elapsed = performance.now() - startTime;
if (elapsed >= DURATION) { clearInterval(iv); finish(); return; }
const remaining = Math.ceil((DURATION - elapsed) / 1000);
btn.textContent = 'Testing... ' + remaining + 's';
if (pending === null) {
pending = performance.now();
ws.send(JSON.stringify({ type: 'send', to: clientId, data: 'ping' }));
}
}, INTERVAL);
} else if (msg.type === 'message' && pending !== null) {
const rtt = performance.now() - pending;
const t = performance.now() - startTime;
pending = null;
samples.push({ t, rtt });
drawChart();
showStats();
} else if (msg.type === 'error') {
btn.textContent = 'Error: ' + msg.message;
btn.disabled = false;
}
};
ws.onerror = () => { btn.textContent = 'Connection failed'; btn.disabled = false; };
}
document.getElementById('test-btn').addEventListener('click', function() { hasTestedOnce = true; runTest(); });
document.querySelectorAll('.ofav').forEach(function(img) {
img.addEventListener('error', function() { this.style.display = 'none'; });
});
var hasTestedOnce = false;
setInterval(function() { if (!hasTestedOnce) location.reload(); }, 5000);
var showLocal = document.getElementById('show-local');
if (showLocal) {
var root = document.documentElement;
if (localStorage.getItem('show-local') === '1') { showLocal.checked = true; root.classList.remove('hide-local'); }
function updateCounts(showAll) {
document.querySelectorAll('.sv[data-all]').forEach(function(el) {
el.textContent = showAll ? el.getAttribute('data-all') : el.getAttribute('data-public');
});
}
updateCounts(showLocal.checked);
showLocal.addEventListener('change', function() {
root.classList.toggle('hide-local', !this.checked);
localStorage.setItem('show-local', this.checked ? '1' : '0');
updateCounts(this.checked);
});
}
</script>
</body>
</html>`;
}
// --- Server ---
const PORT = parseInt(process.env.PORT || "3000", 10);
const server = Bun.serve({
port: PORT,
fetch(req, server) {
const url = new URL(req.url);
if (url.pathname === "/health") {
return new Response(JSON.stringify({ status: "ok" }), {
headers: { "Content-Type": "application/json" },
});
}
if (url.pathname === "/favicon.ico" || url.pathname === "/favicon.svg") {
return new Response(FAVICON_SVG, {
headers: { "Content-Type": "image/svg+xml" },
});
}
const roomMatch = url.pathname.match(/^\/room\/([^/]+)$/);
if (roomMatch) {
const code = decodeURIComponent(roomMatch[1]);
const room = rooms.get(code);
const headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
};
if (!room) {
return new Response(JSON.stringify({ error: "Room not found" }), { status: 404, headers });
}
return new Response(JSON.stringify({
clients: room.clients.size,
maxClients: room.maxClients,
origin: room.origin,
}), { headers });
}
const origin = req.headers.get("origin") || undefined;
const upgraded = server.upgrade(req, { data: { origin } as ClientData });
if (!upgraded) {
const { roomCount, clientCount, origins } = getOriginStats();
const forwarded = req.headers.get("x-forwarded-for")?.split(",")[0].trim();
const ipFamily = forwarded?.includes(":") && !forwarded.startsWith("::ffff:") ? "IPv6" : "IPv4";
return new Response(statusPage(roomCount, clientCount, origins, ipFamily), {
headers: { "Content-Type": "text/html" },
});
}
},
websocket: {
idleTimeout: 10,
open(ws) {
incrementStat(ws.data.origin || "unknown", "connections");
},
message(ws, raw) {
let msg: ClientMessage;
try {
msg = JSON.parse(raw as string);
} catch {
return send(ws, { type: "error", message: "Invalid JSON" });
}
switch (msg.type) {
case "create":
return handleCreate(ws, msg);
case "join":
return handleJoin(ws, msg);
case "send":
return handleSend(ws, msg);
default:
return send(ws, { type: "error", message: "Unknown message type" });
}
},
close(ws) {
removeFromRoom(ws);
},
},
});
console.log(`Party-Sockets running on port ${server.port}`);
// --- Graceful shutdown ---
// On SIGTERM, refuse new `create` messages so fresh games land on the new pod,
// then wait for in-progress rooms to empty before exiting. Kubernetes must grant
// enough terminationGracePeriodSeconds to cover the longest realistic game.
const DRAIN_POLL_INTERVAL_MS = 500;
const DRAIN_DEADLINE_MS = 590_000; // just under a 600s K8s grace period
async function drain(options: { exitOnComplete?: boolean; deadlineMs?: number } = {}): Promise<number> {
if (draining) return rooms.size;
draining = true;
const deadline = Date.now() + (options.deadlineMs ?? DRAIN_DEADLINE_MS);
console.log(`[drain] starting with ${rooms.size} rooms`);
while (rooms.size > 0 && Date.now() < deadline) {
await new Promise((r) => setTimeout(r, DRAIN_POLL_INTERVAL_MS));
}
const remaining = rooms.size;
console.log(`[drain] complete; ${remaining} rooms remaining`);
if (options.exitOnComplete !== false) process.exit(0);
return remaining;
}
function _resetDrainForTest() {
draining = false;
}
process.on("SIGTERM", () => { drain(); });
process.on("SIGINT", () => { drain(); });
export { server, rooms, drain, _resetDrainForTest };