-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (96 loc) · 3.15 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const e = require("express");
const http = require("http");
const app = require('express')()
const port = 3000
let path = require('path');
let m = true;
let keys = ["135F45F9B4D6B5E5E8397B4861AD9751AE55F93FF4AE72E760E84AB5A6B05F53"];
let deviceID = [];
let webID = [];
let status = {};
const server = http.createServer(app);
const {Server} = require("socket.io");
const express = require("express");
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
io.on('connection', (socket) => {
socket.emit("Welcome", "Welcome to Webroze Your socket ID is " + socket.id)
socket.on('RegisterDevice', (data) => {
if(keys.includes(data.key)){
deviceID.push(socket.id)
}
socket.emit("Status","Update your status" + socket.id)
console.log("deviceID : ",deviceID)
for (let webIDElement of webID) {
io.sockets.to(webIDElement.toString()).emit("DeviceId", deviceID)
}
/* io.sockets.allSockets().then( (data)=>{
for (let socketId of data) {
webID = webID.filter(function( obj) {
return obj === socketId;
});
deviceID = deviceID.filter(function( obj) {
return obj === socketId;
});
}
console.log("deviceID",deviceID)
console.log("webID",webID)
})*/
/*for (let socket1 of io.sockets) {
webID = webID.filter(function( obj) {
return obj !== socket1;
});
deviceID = deviceID.filter(function( obj) {
return obj !== socket1;
});
}*/
});
socket.on('JoinUser', (data) => {
webID.push(socket.id)
console.log("webID : ",webID)
socket.emit("Data",status)
io.sockets.to(socket.id).emit("DeviceId", deviceID)
});
socket.on('status', (data) => {
console.log("status : ",data)
status = data;
for (let webIDElement of webID) {
io.sockets.to(webIDElement.toString()).emit("Data", status)
}
});
socket.on('API', (data) => {
console.log("API : ",data)
let command = data.command;
let pinIndex = data.pinIndex;
pinIndex = pinIndex.split("")[1];
pinIndex = pinIndex-2;
let cmd = "CMDD" + pinIndex + command;
console.log(cmd)
for (let deviceIDElement of deviceID) {
io.sockets.to(deviceIDElement.toString()).emit(cmd, `Update your status${deviceIDElement}`)
}
});
socket.on('disconnect', async () => {
console.log("=========came inside disconnect========");
webID = webID.filter(function( obj) {
return obj !== socket.id;
});
deviceID = deviceID.filter(function( obj) {
return obj !== socket.id;
});
console.log("deviceID",deviceID)
console.log("webID",webID)
})
});
let indexPath = path.join(__dirname, './build');
app.use(express.static(indexPath));
app.get('/*', function (req, res) {
console.log("indexPath",indexPath)
res.sendFile(indexPath);
});
server.listen(3005);
//