-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
113 lines (91 loc) · 3.16 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
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
import http from "http";
import * as fs from 'node:fs';
import * as path from 'path';
import WebSocket from "websocket";
let __dirname = path.resolve();
let MAIN_SERVER_PORT = 6888;
let WEBSOCKET_SERVER_PORT = 8895;
let connections = {};
let senders = [];
let recievers = [];
//Modify the server as per your filepaths
const server = http.createServer((req, res) => {
if (Object.keys(connections).length > 5) res.end("Max users reached!")
const getFile = (name) => {
const filePath = path.join(__dirname, name);
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200);
res.end(data);
});
};
if (req.url === "/") getFile("index.html");
if (req.url === "/index.js") {res.setHeader("Content-Type", "application/javascript"); getFile("index.js")};
if (req.url === "/index.css") getFile("index.css");
});
let WebSocketServer = WebSocket.server;
const webSocketServer = http.createServer((req, res) => {
res.writeHead(200)
}).listen(WEBSOCKET_SERVER_PORT, () => console.log("WebSocket server listening on", WEBSOCKET_SERVER_PORT));
const wsServer = new WebSocketServer({
"httpServer": webSocketServer
});
wsServer.on("request", (req) => {
const connection = req.accept(null, req.origin);
let userId = assignUserId();
connections[userId] = connection;
console.log(`user: ${userId} connected`);
connection.send(JSON.stringify({
type: "user-id",
userId: userId
}));
connection.on("message", (msg) => {
let data = JSON.parse(msg.utf8Data);
if (data.type === "sender") {
senders.push(data.userId);
sendReceiversList();
};
if (data.type === "reciever") {
recievers.push(data.userId);
sendReceiversList();
};
if (data.type === "initate") {
connections[data.userId].send(JSON.stringify({
type: "join",
userId: userId,
target: data.target
}));
};
if (data.type === "offer" || data.type === "answer" || data.type === "new-ice-candidate"
|| data.type === "accept-request" || data.type === "request-status") {
connections[data.target].send(JSON.stringify(data));
};
});
connection.on("close", () => {
senders = senders.filter((id) => id !== userId);
recievers = recievers.filter((id) => id !== userId);
delete connections[userId];
console.log(`user: ${userId} disconnected`);
sendReceiversList();
});
});
const sendReceiversList = () => {
senders.map((id) => {
connections[id].send(JSON.stringify({
type: "all-recievers",
userIds: recievers
}));
});
};
const assignUserId = () => {
let userId = String(Math.floor(1000 + Math.random() * 9000));
if (userId in connections) {
return assignUserId();
};
return userId;
};
server.listen(MAIN_SERVER_PORT, () => console.log("Main server listening on", MAIN_SERVER_PORT));