Spaces:
Sleeping
Sleeping
| const path = require('path'); | |
| const express = require('express'); | |
| const http = require('http'); | |
| const { Server } = require('socket.io'); | |
| const app = express(); | |
| const server = http.createServer(app); | |
| const io = new Server(server, { | |
| pingInterval: 10000, | |
| pingTimeout: 5000, | |
| cors: { origin: true } | |
| }); | |
| // Serve static files from /public | |
| app.use(express.static(path.join(__dirname, 'public'))); | |
| // Fix: Ensure routes for /admin and /client work | |
| app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'public', 'admin.html'))); // Default is admin | |
| app.get('/admin', (req, res) => res.sendFile(path.join(__dirname, 'public', 'admin.html'))); | |
| app.get('/client', (req, res) => res.sendFile(path.join(__dirname, 'public', 'client.html'))); | |
| const HOST = '0.0.0.0'; | |
| const PORT = process.env.PORT || 7860; | |
| // Socket handling | |
| io.on('connection', (socket) => { | |
| const room = socket.handshake.query.room || 'default'; | |
| const role = socket.handshake.query.role || 'client'; | |
| socket.join(room); | |
| socket.on('sync:ping', (msg = {}) => { | |
| const t1 = Date.now(); | |
| socket.emit('sync:pong', { t0: msg.t0, t1, t2: Date.now() }); | |
| }); | |
| // Admin commands | |
| socket.on('admin:start', ({ delayMs = 3000, label = '' }) => { | |
| const startAt = Date.now() + Math.max(500, Number(delayMs)); | |
| io.to(room).emit('cmd', { type: 'start', startAt, label }); | |
| }); | |
| socket.on('admin:stop', () => { io.to(room).emit('cmd', { type: 'stop' }); }); | |
| socket.on('admin:reset', () => { io.to(room).emit('cmd', { type: 'reset' }); }); | |
| // Blackout toggle | |
| socket.on('admin:blackout', ({ on = true }) => { | |
| io.to(room).emit('cmd', { type: 'blackout', on }); | |
| }); | |
| socket.on('disconnect', () => {}); | |
| }); | |
| server.listen(PORT, HOST, () => { | |
| console.log(`Server running on http://${HOST}:${PORT}`); | |
| }); | |