YussefGAFeer commited on
Commit
867aaf5
·
verified ·
1 Parent(s): ae1d368

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +34 -0
index.js ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const http = require('http');
2
+ const WebSocket = require('ws');
3
+
4
+ // Create a standard HTTP server to satisfy Render's health checks
5
+ const server = http.createServer((req, res) => {
6
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
7
+ res.end('P2P Signaling Server is Live!\n');
8
+ });
9
+
10
+ const wss = new WebSocket.Server({ server });
11
+
12
+ wss.on('connection', (ws) => {
13
+ console.log('New Peer Connected');
14
+
15
+ ws.on('message', (data) => {
16
+ const message = data.toString();
17
+ console.log('Received:', message);
18
+
19
+ // Broadcast to all other connected clients
20
+ wss.clients.forEach((client) => {
21
+ if (client !== ws && client.readyState === WebSocket.OPEN) {
22
+ client.send(message);
23
+ }
24
+ });
25
+ });
26
+
27
+ ws.on('close', () => console.log('Peer Disconnected'));
28
+ ws.on('error', (error) => console.error('WebSocket Error:', error));
29
+ });
30
+
31
+ const port = process.env.PORT || 8080;
32
+ server.listen(port, '0.0.0.0', () => {
33
+ console.log(`Server is running on port ${port}`);
34
+ });