-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
59 lines (50 loc) · 1.41 KB
/
socket.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const socket = require('socket.io')
module.exports = init
function init (server, state) {
state.clients = state.clients || 0
state.boards = state.boards || 0
const io = socket(server)
const board = io.of('/board')
const client = io.of('client')
board.on('connection', function (socket) {
console.log('► board connected')
state.boards++
board.emit('board:join')
socket.emit('state', JSON.stringify(state))
socket.on('reset', function () {
socket.emit('state', JSON.stringify(state))
})
socket.on('disconnect', function () {
state.boards--
client.emit('board:leave')
board.emit('board:leave')
console.log('► board disconnected')
})
})
client.on('connection', function (socket) {
console.log('► client connected')
socket.on('disconnect', function () {
state.clients--
client.emit('client:leave')
board.emit('client:leave')
console.log('► client disconnected')
})
state.clients++
board.emit('client:join')
socket.on('add', function (data) {
board.emit('add', data)
})
socket.on('move', function (data) {
board.emit('move', data)
})
socket.on('remove', function (data) {
board.emit('remove', data)
})
socket.on('save', function (data) {
board.emit('save', data)
})
socket.on('new', function (data) {
board.emit('new', data)
})
})
}