-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignalling.js
More file actions
176 lines (151 loc) · 4.91 KB
/
Copy pathsignalling.js
File metadata and controls
176 lines (151 loc) · 4.91 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const WebSocket = require("ws");
const { v4: uuidv4 } = require("uuid"); // To generate unique IDs
const express = require("express");
const cors = require("cors");
const path = require("path");
const http = require("http");
const app = express();
const server = http.createServer(app);
app.use(cors());
app.use(express.static(path.join(__dirname, "src")));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
const port = 9000;
// Create a WebSocket server
const wss = new WebSocket.Server({ server });
app.get("/join", (req, res) => {
res.render("join");
});
app.post("/select-camera", (req, res) => {
var roomId = req.body.roomId;
const username = req.body.username;
const age = req.body.age;
const gender = req.body.gender;
if (!roomId) {
roomId = req.body.genRoomId;
}
res.render("select_camera", {
roomId: roomId,
username: username,
age: age,
gender: gender,
});
});
app.get("/room", (req, res) => {
const leftDeviceId = req.query.leftDevice;
const rightDeviceId = req.query.rightDevice;
const roomId = req.query.roomId;
const username = req.query.username;
const age = req.query.age;
const gender = req.query.gender;
res.render("room", {
roomId: roomId,
username: username,
age: age,
gender: gender,
leftDeviceId: leftDeviceId,
rightDeviceId: rightDeviceId,
});
});
// Create an object to store the rooms and their associated clients
const rooms = {};
// Handle new WebSocket connections
wss.on("connection", (ws) => {
console.log("New client connected");
// Assign a room to the client (the client should send a message specifying the room)
ws.on("message", (message) => {
try {
const data = JSON.parse(message); // Attempt to parse JSON
// New user is joining the room
if (data.type === "join") {
const clientId = uuidv4();
const room = data.room;
if (!rooms[room]) {
rooms[room] = []; // Create room if it doesn't exist
}
rooms[room].push({ wsClient: ws, userId: clientId }); // Add client to the room
ws.room = room; // Track which room the client belongs to
ws.userId = clientId; // Assign the userId to the WebSocket client
const randomNumber = Math.floor(Math.random() * 10) + 1;
ws.send(
JSON.stringify({
userId: clientId,
roomId: room,
username: data.username,
gender: data.gender,
age: data.age,
productivityScore: randomNumber,
})
);
console.log(
`Client joined room: ${room}, Name: ${data.name}, ID: ${clientId}`
);
}
// Broadcast a message to everyone in the room except the sender
if (data.type === "broadcast") {
const room = data.room;
if (rooms[room]) {
rooms[room].forEach((client) => {
if (
client.wsClient !== ws &&
client.wsClient.readyState === WebSocket.OPEN
) {
console.log("offer received on server", ws.userId);
if (data.msgType === "offer") {
client.wsClient.send(
JSON.stringify({
msgType: "offer",
msg: data.msg,
roomId: room,
userId: ws.userId,
})
);
}
if (data.msgType === "candidate") {
console.log("candidate received on server", ws.userId);
client.wsClient.send(
JSON.stringify({
msgType: "candidate",
roomId: room,
candidate: data.candidate,
sdpmid: data.sdpmid,
sdpmlineindex: data.sdpmlineindex,
userId: ws.userId,
})
);
}
if (data.msgType === "answer") {
console.log("answer received on server", ws.userId);
client.wsClient.send(
JSON.stringify({
msgType: "answer",
msg: data.msg,
roomId: room,
userId: ws.userId,
})
);
}
}
});
}
}
} catch (error) {
console.error("Error parsing JSON:", error.message); // Handle any errors
}
});
// Handle client disconnect
ws.on("close", () => {
const room = ws.room;
if (room && rooms[room]) {
rooms[room] = rooms[room].filter((client) => client.wsClient !== ws); // Remove client from room
if (rooms[room].length === 0) {
delete rooms[room]; // Delete room if empty
}
console.log(`Client disconnected from room: ${ws.userId}`);
}
});
});
server.listen(port, () => {
console.log("server is running on port", port);
});