-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (32 loc) · 1017 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
let express = require('express');
let online = 0;
let app = express()
let port = process.env.PORT || 3000
let server = app.listen(port)
app.use(express.static('public'))
console.log('RUNING YOUR SERVER ...')
console.log(`hearing requests at localhost:${port}..`)
let socket = require('socket.io')
let io = socket(server)
io.sockets.on('connection', newConnection)
function newConnection(socket) {
console.log('new connection : ' + socket.id + ' online ' + ++online)
io.sockets.emit('online', online)
socket.on('clearall', () => {
socket.broadcast.emit('clearall')
})
socket.on('data', (data) => {
socket.broadcast.emit('data', data)
})
socket.on('mouse', (d) => {
socket.broadcast.emit('mouse', d)
})
socket.on('disconnect', () => {
console.log('disconnected now online : ' + online)
online = online - 1
io.sockets.emit('online', online)
})
setInterval(() => {
socket.emit('online', online)
}, 500)
}