-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.js
More file actions
28 lines (25 loc) · 971 Bytes
/
io.js
File metadata and controls
28 lines (25 loc) · 971 Bytes
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
const sio = require('socket.io');
let io = null;
const ids = new Map();
exports.initialize = function (server) {
io = sio.listen(server.listener);
io.on('connection', (socket) => {
console.log(`A user connected with ${socket.id}`);
socket.on('UPDATE_DEVICE_SELECTION', (data) => {
console.log(`UPDATE DEVICE SELECTION by socket id ${socket.id} for device ${data.deviceId}`);
data.prop === "ADD" && ids.set(socket.id, data.deviceId);
data.prop === "REMOVE" && ids.delete(socket.id);
})
socket.on('disconnect', () => {
console.log(`User disconnected ${socket.id}`);
})
})
}
exports.sendDeviceData = function (currentDevice, data) {
for (const [socketId, deviceId] of ids.entries()) {
if (deviceId === currentDevice) {
console.log('SENDING DEVICE DATA of ', currentDevice);
io.to(socketId).emit('DEVICE_DATA', data);
}
}
}