Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,92 +1,100 @@
|
|
| 1 |
-
// server.js —
|
| 2 |
-
// -
|
| 3 |
-
// -
|
| 4 |
-
// -
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
const
|
| 9 |
-
const
|
| 10 |
-
|
| 11 |
-
const
|
| 12 |
-
const
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
//
|
| 23 |
-
app.
|
| 24 |
-
|
| 25 |
-
//
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
(s
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
const
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
socket.data.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
socket.on('stats:refresh', () => emitStats(room));
|
| 58 |
-
|
| 59 |
-
//
|
| 60 |
-
socket.on('sync:ping', (
|
| 61 |
-
const
|
| 62 |
-
socket.emit('sync:pong', {
|
| 63 |
-
});
|
| 64 |
-
|
| 65 |
-
// Admin commands
|
| 66 |
-
socket.on('admin:start', ({ delayMs = 3000, label = '' } = {}) => {
|
| 67 |
-
if (socket.data.role !== 'admin') return;
|
| 68 |
-
const
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
io.to(room).emit('cmd',
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
});
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// server.js — Pure async (monotonic) sync, no wall clock.
|
| 2 |
+
// - Uses perf_hooks.performance.now() on server
|
| 3 |
+
// - Robust room/admin/client stats
|
| 4 |
+
// - Commands: start/stop/reset/blackout
|
| 5 |
+
// - Redundant START + preSync burst for tight simultaneity
|
| 6 |
+
// - Docker/Spaces-ready (binds 0.0.0.0, uses PORT)
|
| 7 |
+
|
| 8 |
+
const path = require('path');
|
| 9 |
+
const express = require('express');
|
| 10 |
+
const http = require('http');
|
| 11 |
+
const { Server } = require('socket.io');
|
| 12 |
+
const { performance } = require('perf_hooks');
|
| 13 |
+
|
| 14 |
+
const app = express();
|
| 15 |
+
const server = http.createServer(app);
|
| 16 |
+
const io = new Server(server, {
|
| 17 |
+
pingInterval: 10000,
|
| 18 |
+
pingTimeout: 5000,
|
| 19 |
+
cors: { origin: true }
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
// Serve static files
|
| 23 |
+
app.use(express.static(path.join(__dirname, 'public')));
|
| 24 |
+
|
| 25 |
+
// Single entry (merged Admin+Client UI lives in public/index.html)
|
| 26 |
+
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'public', 'index.html')));
|
| 27 |
+
|
| 28 |
+
const HOST = '0.0.0.0';
|
| 29 |
+
const PORT = process.env.PORT || 7860;
|
| 30 |
+
|
| 31 |
+
// ---------- Helpers ----------
|
| 32 |
+
function normRoom(x) { return String(x || 'default').trim().toLowerCase(); }
|
| 33 |
+
function normRole(x) { return String(x || 'client').trim().toLowerCase(); }
|
| 34 |
+
|
| 35 |
+
function emitStats(room) {
|
| 36 |
+
const ids = io.sockets.adapter.rooms.get(room) || new Set();
|
| 37 |
+
let numAdmins = 0, numClients = 0;
|
| 38 |
+
for (const id of ids) {
|
| 39 |
+
const s = io.sockets.sockets.get(id);
|
| 40 |
+
if (!s) continue;
|
| 41 |
+
(s.data?.role === 'admin') ? numAdmins++ : numClients++;
|
| 42 |
+
}
|
| 43 |
+
io.to(room).emit('stats', { numAdmins, numClients });
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// ---------- Socket.io ----------
|
| 47 |
+
io.on('connection', (socket) => {
|
| 48 |
+
const room = normRoom(socket.handshake.query.room);
|
| 49 |
+
const role = normRole(socket.handshake.query.role);
|
| 50 |
+
|
| 51 |
+
socket.data.room = room;
|
| 52 |
+
socket.data.role = role;
|
| 53 |
+
|
| 54 |
+
socket.join(room);
|
| 55 |
+
emitStats(room);
|
| 56 |
+
|
| 57 |
+
socket.on('stats:refresh', () => emitStats(room));
|
| 58 |
+
|
| 59 |
+
// SYNC: server sends its monotonic time; client samples arrival with its own monotonic time
|
| 60 |
+
socket.on('sync:ping', () => {
|
| 61 |
+
const tS = performance.now(); // server monotonic ms
|
| 62 |
+
socket.emit('sync:pong', { tS });
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
// Admin commands
|
| 66 |
+
socket.on('admin:start', ({ delayMs = 3000, label = '' } = {}) => {
|
| 67 |
+
if (socket.data.role !== 'admin') return;
|
| 68 |
+
const startServerPerf = performance.now() + Math.max(800, Number(delayMs)); // future server perf time (ms)
|
| 69 |
+
const payload = { type: 'start', startServerPerf, label };
|
| 70 |
+
|
| 71 |
+
// Redundant emits for resilience
|
| 72 |
+
io.to(room).emit('cmd', payload);
|
| 73 |
+
setTimeout(() => io.to(room).emit('cmd', payload), 250);
|
| 74 |
+
setTimeout(() => io.to(room).emit('cmd', payload), 500);
|
| 75 |
+
|
| 76 |
+
// Ask clients to enter high-rate sync mode until T0
|
| 77 |
+
io.to(room).emit('cmd', { type: 'preSync', untilServerPerf: startServerPerf });
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
socket.on('admin:stop', () => {
|
| 81 |
+
if (socket.data.role !== 'admin') return;
|
| 82 |
+
io.to(room).emit('cmd', { type: 'stop' });
|
| 83 |
+
});
|
| 84 |
+
|
| 85 |
+
socket.on('admin:reset', () => {
|
| 86 |
+
if (socket.data.role !== 'admin') return;
|
| 87 |
+
io.to(room).emit('cmd', { type: 'reset' });
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
socket.on('admin:blackout', ({ on = true } = {}) => {
|
| 91 |
+
if (socket.data.role !== 'admin') return;
|
| 92 |
+
io.to(room).emit('cmd', { type: 'blackout', on: !!on });
|
| 93 |
+
});
|
| 94 |
+
|
| 95 |
+
socket.on('disconnect', () => emitStats(room));
|
| 96 |
+
});
|
| 97 |
+
|
| 98 |
+
server.listen(PORT, HOST, () => {
|
| 99 |
+
console.log(`Timer server listening on http://${HOST}:${PORT}`);
|
| 100 |
+
});
|