Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
const path = require('path');
|
| 2 |
const express = require('express');
|
| 3 |
const http = require('http');
|
|
@@ -11,43 +16,77 @@ const io = new Server(server, {
|
|
| 11 |
cors: { origin: true }
|
| 12 |
});
|
| 13 |
|
| 14 |
-
// Serve static files
|
| 15 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 16 |
|
| 17 |
-
//
|
| 18 |
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'public', 'index.html')));
|
| 19 |
|
|
|
|
| 20 |
const HOST = '0.0.0.0';
|
| 21 |
const PORT = process.env.PORT || 7860;
|
| 22 |
|
| 23 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
io.on('connection', (socket) => {
|
| 25 |
-
const room = socket.handshake.query.room
|
| 26 |
-
const role = socket.handshake.query.role
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
socket.join(room);
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
socket.on('sync:ping', (msg = {}) => {
|
| 30 |
const t1 = Date.now();
|
| 31 |
socket.emit('sync:pong', { t0: msg.t0, t1, t2: Date.now() });
|
| 32 |
});
|
| 33 |
|
| 34 |
// Admin commands
|
| 35 |
-
socket.on('admin:start', ({ delayMs = 3000, label = '' }) => {
|
|
|
|
| 36 |
const startAt = Date.now() + Math.max(500, Number(delayMs));
|
| 37 |
io.to(room).emit('cmd', { type: 'start', startAt, label });
|
| 38 |
});
|
| 39 |
|
| 40 |
-
socket.on('admin:stop',
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
io.to(room).emit('cmd', { type: 'blackout', on });
|
| 46 |
});
|
| 47 |
|
| 48 |
-
socket.on('disconnect', () =>
|
| 49 |
});
|
| 50 |
|
| 51 |
server.listen(PORT, HOST, () => {
|
| 52 |
-
console.log(`
|
| 53 |
});
|
|
|
|
| 1 |
+
// server.js — HF Spaces / Docker ready (PORT + 0.0.0.0)
|
| 2 |
+
// - Robust room/admin/client stats
|
| 3 |
+
// - Commands: start/stop/reset/blackout
|
| 4 |
+
// - NTP-style sync (sync:ping -> sync:pong)
|
| 5 |
+
|
| 6 |
const path = require('path');
|
| 7 |
const express = require('express');
|
| 8 |
const http = require('http');
|
|
|
|
| 16 |
cors: { origin: true }
|
| 17 |
});
|
| 18 |
|
| 19 |
+
// Serve static files
|
| 20 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 21 |
|
| 22 |
+
// Single entry page (index.html handles role=admin|client)
|
| 23 |
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'public', 'index.html')));
|
| 24 |
|
| 25 |
+
// HF Spaces / Docker
|
| 26 |
const HOST = '0.0.0.0';
|
| 27 |
const PORT = process.env.PORT || 7860;
|
| 28 |
|
| 29 |
+
// ---------- Helpers ----------
|
| 30 |
+
function normRoom(x) { return String(x || 'default').trim().toLowerCase(); }
|
| 31 |
+
function normRole(x) { return String(x || 'client').trim().toLowerCase(); }
|
| 32 |
+
|
| 33 |
+
// Emit counts for a room using socket.data.role
|
| 34 |
+
function emitStats(room) {
|
| 35 |
+
const ids = io.sockets.adapter.rooms.get(room) || new Set();
|
| 36 |
+
let numAdmins = 0, numClients = 0;
|
| 37 |
+
for (const id of ids) {
|
| 38 |
+
const s = io.sockets.sockets.get(id);
|
| 39 |
+
if (!s) continue;
|
| 40 |
+
(s.data?.role === 'admin') ? numAdmins++ : numClients++;
|
| 41 |
+
}
|
| 42 |
+
io.to(room).emit('stats', { numAdmins, numClients });
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// ---------- Socket.io ----------
|
| 46 |
io.on('connection', (socket) => {
|
| 47 |
+
const room = normRoom(socket.handshake.query.room);
|
| 48 |
+
const role = normRole(socket.handshake.query.role);
|
| 49 |
+
|
| 50 |
+
socket.data.room = room;
|
| 51 |
+
socket.data.role = role;
|
| 52 |
+
|
| 53 |
socket.join(room);
|
| 54 |
+
emitStats(room);
|
| 55 |
|
| 56 |
+
// Optional: let any client/admin force a refresh
|
| 57 |
+
socket.on('stats:refresh', () => emitStats(room));
|
| 58 |
+
|
| 59 |
+
// NTP-like sync
|
| 60 |
socket.on('sync:ping', (msg = {}) => {
|
| 61 |
const t1 = Date.now();
|
| 62 |
socket.emit('sync:pong', { t0: msg.t0, t1, t2: Date.now() });
|
| 63 |
});
|
| 64 |
|
| 65 |
// Admin commands
|
| 66 |
+
socket.on('admin:start', ({ delayMs = 3000, label = '' } = {}) => {
|
| 67 |
+
if (socket.data.role !== 'admin') return;
|
| 68 |
const startAt = Date.now() + Math.max(500, Number(delayMs));
|
| 69 |
io.to(room).emit('cmd', { type: 'start', startAt, label });
|
| 70 |
});
|
| 71 |
|
| 72 |
+
socket.on('admin:stop', () => {
|
| 73 |
+
if (socket.data.role !== 'admin') return;
|
| 74 |
+
io.to(room).emit('cmd', { type: 'stop' });
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
+
socket.on('admin:reset', () => {
|
| 78 |
+
if (socket.data.role !== 'admin') return;
|
| 79 |
+
io.to(room).emit('cmd', { type: 'reset' });
|
| 80 |
+
});
|
| 81 |
|
| 82 |
+
socket.on('admin:blackout', ({ on = true } = {}) => {
|
| 83 |
+
if (socket.data.role !== 'admin') return;
|
| 84 |
+
io.to(room).emit('cmd', { type: 'blackout', on: !!on });
|
| 85 |
});
|
| 86 |
|
| 87 |
+
socket.on('disconnect', () => emitStats(room));
|
| 88 |
});
|
| 89 |
|
| 90 |
server.listen(PORT, HOST, () => {
|
| 91 |
+
console.log(`Timer server on http://${HOST}:${PORT}`);
|
| 92 |
});
|