-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (94 loc) · 3.48 KB
/
server.js
File metadata and controls
112 lines (94 loc) · 3.48 KB
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
104
105
106
107
108
109
110
111
112
const app = require("express")();
const httpServer = require("http").createServer(app);
const os = require("os");
// Get the network interfaces
const networkInterfaces = os.networkInterfaces();
const io = require("socket.io")(httpServer, { cors: true });
const port = process.env.PORT || 5001;
let rooms = {};
io.on("connection", (socket) => {
console.log("Connected =>", socket.id);
let currentDeviceId;
// Edge will create room of deviceId
socket.on("create room", (info) => {
// console.log("Received message:", info);
console.log("Edge created a room");
currentDeviceId = info.deviceId;
console.log("Edge device id ", currentDeviceId);
if (!rooms[info.deviceId]) {
// Room does not exist for current device. So create it
rooms[info.deviceId] = { cams: info.cams, deviceSocketId: socket.id };
currentDeviceId = info.deviceId;
}
// console.log("emiting room created event now");
// io.to(rooms[info.deviceId].deviceSocketId).emit("roomCreated", {
// roomId: info.deviceId,
// });
// console.log("New Rooms", rooms);
});
// User will join room of deviceId
socket.on("join room", (info) => {
if (!rooms[info.deviceId]) {
// Room does not exist for current device. So device offline
} else {
// console.log("Received message:", info);
console.log("User joined a room");
// Joining room
rooms[info.deviceId].userSocketId = socket.id;
io.to(rooms[info.deviceId].deviceSocketId).emit("userJoined", info);
}
// console.log("New Rooms", rooms);
});
socket.on("answer", (data) => {
// console.log("Server Received answer:", data);
console.log("Server Received answer");
// console.log(rooms[data.deviceId].userSocketId);
io.to(rooms[data.deviceId].userSocketId).emit("answer", data);
});
socket.on("iceCandidate", (data) => {
// console.log("Server Received candidate:", data);
console.log("Server Received candidate");
if (data.user) {
io.to(rooms[data.deviceId].deviceSocketId).emit("iceCandidate", data);
} else {
io.to(rooms[data.deviceId].userSocketId).emit("iceCandidate", data);
}
});
socket.on("disconnect", () => {
if (rooms[currentDeviceId]) {
delete rooms[currentDeviceId];
}
// console.log("New Rooms", rooms);
console.log("Disconnect", currentDeviceId);
});
socket.on("camRequest", (info) => {
console.log("Server Received Cam Request:", info);
io.to(rooms[info.deviceId].deviceSocketId).emit("camRequest", {
camId: info.camId,
});
// socket.broadcast.emit("camRequest", info); // Broadcast the message to all connected clients except self
});
socket.on("camData", (data) => {
console.log("Server Received Cam data:", data);
console.log(rooms[data.deviceId].userSocketId);
io.to(rooms[data.deviceId].userSocketId).emit("camData", data);
});
socket.on("offer", (data) => {
console.log("Recieved an offer from the edge device");
// console.log(JSON.stringify(data.offer, null, 2));
});
});
app.get("/", (req, res) => {
res.send("Hello World!");
});
httpServer.listen(port, () => {
// Loop through the network interfaces to find the IP address
Object.keys(networkInterfaces).forEach((interfaceName) => {
const interfaceData = networkInterfaces[interfaceName];
for (const network of interfaceData) {
if (network.family === "IPv4" && !network.internal) {
console.log(`Server IP address: http://${network.address}:${port}`);
}
}
});
});