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.
35 lines
678 B
35 lines
678 B
4 years ago
|
const server = require('http').createServer()
|
||
|
const io = require('socket.io')(server)
|
||
|
|
||
|
/**
|
||
|
* on client connection
|
||
|
*
|
||
|
*/
|
||
|
io.on('connection', (socket) => {
|
||
|
|
||
|
// send count to all clients
|
||
|
io.sockets.emit('count', getCurrentState(socket))
|
||
|
|
||
|
// send count to all clients if user disconnect
|
||
|
socket.on('disconnect', () => {
|
||
|
io.sockets.emit('count', getCurrentState(socket))
|
||
|
})
|
||
|
|
||
|
})
|
||
|
|
||
|
/**
|
||
|
* get current state, count of clients and time
|
||
|
*
|
||
|
* @return object
|
||
|
*/
|
||
|
function getCurrentState(socket)
|
||
|
{
|
||
|
const now = new Date().toUTCString()
|
||
|
|
||
|
return {
|
||
|
'count': socket.client.conn.server.clientsCount,
|
||
|
'now' : now
|
||
|
}
|
||
|
}
|
||
|
|
||
|
server.listen(3000)
|