// server.js const express = require('express'); const http = require('http'); const socketIo = require('socket.io'); const app = express(); const server = http.createServer(app); const io = socketIo(server); let clients = {}; io.on('connection', (socket) => { console.log('A client connected:', socket.id); clients[socket.id] = { status: 'connected' }; socket.on('disconnect', () => { console.log('A client disconnected:', socket.id); clients[socket.id] = { status: 'disconnected' }; }); // 定时发送客户端状态 setInterval(() => { io.emit('clients_status', clients); }, 5000); }); server.listen(3000, () => { console.log('Server listening on port 3000'); });