-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
32 lines (25 loc) · 847 Bytes
/
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
const io = require('socket.io')(9000);
const handlers = require('./core/handlers/document');
const documentChannel = ({ socketId, socket }) => async ({ type, action }) => {
if (typeof handlers[type] === 'function') {
const result = await handlers[type](action);
console.log({ result });
if (result) socket.emit(action.id, result);
}
};
const cncChannel = ({ socketId }) => (data) => {
const { type, action } = JSON.parse(data);
};
io.on('connection', (socket) => {
const socketId = socket.id;
// attach document channel
socket.on('document', documentChannel({ socketId, socket }));
// attach cnc channel
socket.on('cdc', cncChannel({ socketId, socket }));
});
io.on('disconnect', (socket) => {
socket.broadcast.emit('disconnect', {
data: 'You have disconnected!'
});
});
console.log(`Socket listening on 9000`);