forked from Ninety-Camera/web-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 1.06 KB
/
index.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
require("dotenv").config();
const app = require("./app");
const http = require("http").Server(app);
const io = require("socket.io")(http);
const sockets = require("./sockets");
const cctvService = require("./src/services/cctvService");
http.listen(process.env.PORT, () => {
console.log("Server started sucessfully!");
});
io.use(async (socket, next) => {
const id = socket.handshake.auth.systemId;
try {
const existence = await cctvService.getCCTVSystem(id);
if (!existence) {
return next(new Error("invalid system Id"));
}
} catch (error) {
next(new Error("Error in connecting"));
}
next();
});
// Socket connection
io.on("connection", function (socket) {
// Validation of the systemId is required
sockets.push({
socket: socket,
systemId: socket.handshake.auth.systemId,
});
// Whenever someone disconnects this piece of code executed
socket.on("disconnect", function () {
const disconnetId = socket.id;
const index = sockets.findIndex((item) => item.socket.id === disconnetId);
sockets.splice(index, 1);
});
});