-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummy_server.js
49 lines (39 loc) · 1.2 KB
/
dummy_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
const WebSocket = require("ws");
const wss = new WebSocket.Server({ port: 8080 });
const clients = {};
wss.on("connection", (ws, req) => {
console.log("Connection established with",req.socket.remoteAddress, ws._socket.address().port)
const id = generateId();
clients[id] = ws;
ws.send(JSON.stringify({ type: "id", id }));
ws.on("message", (message) => {
console.log('Received message')
message = JSON.parse(message);
console.log(message)
let senderId = "";
if (message["data"]) {
senderId = message["id"];
sendToVr(senderId, {type: "yolo", data: message["data"]})
console.log('sent to VR')
}
});
ws.on("close", () => {
delete clients[id];
});
});
function generateId() {
while (true) {
const id = Math.random().toString(36).substring(7);
if (!clients[id]) {
return id;
}
}
}
function sendToVr(senderId, message) {
for (const id in clients) {
if (id !== senderId) {
clients[id].send(JSON.stringify(message));
}
}
}
console.log("WebSocket server running on port 8080");