-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
65 lines (51 loc) · 1.53 KB
/
server.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
require("dotenv").config({ path: "./config.env" });
const express = require("express");
const connectDB = require("./config/db");
const errorHandler = require("./middleware/error");
const http = require("http");
const WebSocket = require("ws");
// Connect DB
connectDB();
const app = express();
app.use(express.json());
app.use("/api/authenticate", require("./routes/authenticate"));
app.use("/api/authorize", require("./routes/authorize"));
// Error Handler
app.use(errorHandler);
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
CLIENTS = [];
wss.on("connection", function connection(ws) {
console.log("New Device Connected!");
ws.on("message", (data) => {
console.log(data, typeof data);
var jsonData = {};
try {
jsonData = JSON.parse(data);
ws.type = jsonData.type;
} catch (error) {
console.log(`Parse Error: ${error}`);
}
if (jsonData.type === "CLIENT") {
CLIENTS.push(ws);
}
if (jsonData.type === "SENSOR") {
if (CLIENTS.length > 0) {
for (let i = 0; i < CLIENTS.length; i++) {
CLIENTS[i].send(JSON.stringify(jsonData.sensorData));
}
}
}
});
ws.on("close", () => {
console.log(`${ws.type} Disconnected!`);
});
});
const PORT = process.env.PORT || 5000;
const serverListener = server.listen(PORT, () =>
console.log(`Server running on port ${PORT}`)
);
process.on("unhandledRejection", (err, promise) => {
console.log(`Logged Error: ${err}`);
serverListener.close(() => process.exit(1));
});