You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
729 B
29 lines
729 B
// 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');
|
|
}); |