-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
138 lines (117 loc) · 4.7 KB
/
server.js
File metadata and controls
138 lines (117 loc) · 4.7 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
/**
* WebRTC Signaling Server
*
* A minimal WebSocket-based signaling server that relays SDP offers, answers,
* and ICE candidates between WebRTC peers. It also serves the static HTML files.
*
* The signaling server is ONLY needed for the initial handshake. Once a
* peer-to-peer connection is established, all data flows directly between
* browsers — this server is no longer involved.
*
* @author Ariful Islam <https://arifulislamat.com>
*
* @usage
* npm install # install dependencies
* npm start # start the signaling server on port 8080
*
* @routes
* GET / → auto.html (auto-signaling mode)
* GET /auto → auto.html
* GET /sender → sender.html (manual copy-paste mode)
* GET /receiver → receiver.html
*/
"use strict";
const { WebSocketServer } = require("ws");
const http = require("http");
const fs = require("fs");
const path = require("path");
const PORT = process.env.PORT || 8080;
const HOST = "0.0.0.0"; // Listen on all network interfaces (allows cross-device access)
// ────────────────────────────────────────────────────────────
// HTTP Server — serves static HTML files
// ────────────────────────────────────────────────────────────
const ROUTES = {
"/": "auto.html",
"/auto": "auto.html",
"/sender": "sender.html",
"/receiver": "receiver.html",
};
const MIME_TYPES = {
".html": "text/html",
".js": "application/javascript",
".css": "text/css",
};
const httpServer = http.createServer((req, res) => {
const fileName = ROUTES[req.url];
if (!fileName) {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 — Not Found");
return;
}
const filePath = path.join(__dirname, fileName);
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("500 — Internal Server Error");
return;
}
const ext = path.extname(filePath);
const contentType = MIME_TYPES[ext] || "application/octet-stream";
res.writeHead(200, { "Content-Type": contentType });
res.end(data);
});
});
// ────────────────────────────────────────────────────────────
// WebSocket Signaling Server
// ────────────────────────────────────────────────────────────
const wss = new WebSocketServer({ server: httpServer });
const clients = new Set();
wss.on("connection", (ws) => {
clients.add(ws);
console.log(`[ws] Client connected (total: ${clients.size})`);
ws.on("message", (raw) => {
const message = raw.toString();
const preview = message.length > 80 ? message.slice(0, 80) + "…" : message;
console.log(`[ws] Relaying: ${preview}`);
// Broadcast to every OTHER connected client
for (const client of clients) {
if (client !== ws && client.readyState === 1 /* WebSocket.OPEN */) {
client.send(message);
}
}
});
ws.on("close", () => {
clients.delete(ws);
console.log(`[ws] Client disconnected (total: ${clients.size})`);
});
ws.on("error", (err) => {
console.error("[ws] Error:", err.message);
});
});
// ────────────────────────────────────────────────────────────
// Start
// ────────────────────────────────────────────────────────────
httpServer.listen(PORT, HOST, () => {
const os = require("os");
const nets = os.networkInterfaces();
const lanAddresses = Object.values(nets)
.flat()
.filter((i) => i.family === "IPv4" && !i.internal);
console.log();
console.log(" 🚀 WebRTC Signaling Server");
console.log(" ─────────────────────────────");
console.log(` Local: http://localhost:${PORT}/`);
lanAddresses.forEach((i) => {
console.log(` Network: http://${i.address}:${PORT}/`);
});
console.log();
console.log(" Auto mode: / (open in two tabs)");
console.log(" Manual mode: /sender + /receiver");
if (lanAddresses.length > 0) {
console.log();
console.log(
" 📱 Open the Network URL on another device to test cross-device.",
);
}
console.log();
});