Spaces:
Sleeping
Sleeping
Download server.js from rifat1670/e-requestssrn: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/spaces/rifat1670/e-requestssrn/resolve/main/server.js
- Command line
-
hf download hf://spaces/rifat1670/e-requestssrn/server.js
-
curl -L -o server.js https://huggingface.co/spaces/rifat1670/e-requestssrn/resolve/main/server.js
1.81 kB
| const WebSocket = require('ws'); | |
| const http = require('http'); | |
| // HTTP server for health checks and status | |
| const server = http.createServer((req, res) => { | |
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
| res.end('MeshLink Signaling Server is Active!\n'); | |
| }); | |
| const wss = new WebSocket.Server({ server }); | |
| // Map to store rooms and their connected clients | |
| const rooms = new Map(); | |
| wss.on('connection', (ws) => { | |
| console.log('New client connected'); | |
| ws.on('message', (message) => { | |
| try { | |
| const data = JSON.parse(message); | |
| // Handle 'join' action | |
| if (data.action === 'join') { | |
| const { room, sender } = data; | |
| ws.room = room; | |
| ws.userId = sender; | |
| if (!rooms.has(room)) { | |
| rooms.set(room, new Set()); | |
| } | |
| rooms.get(room).add(ws); | |
| console.log(`User ${sender} joined room: ${room}`); | |
| return; | |
| } | |
| // Relay all other signals (offer, answer, ice) to others in the same room | |
| if (ws.room && rooms.has(ws.room)) { | |
| rooms.get(ws.room).forEach((client) => { | |
| if (client !== ws && client.readyState === WebSocket.OPEN) { | |
| client.send(JSON.stringify(data)); | |
| } | |
| }); | |
| } | |
| } catch (e) { | |
| console.error('Failed to parse message:', e.message); | |
| } | |
| }); | |
| ws.on('close', () => { | |
| if (ws.room && rooms.has(ws.room)) { | |
| rooms.get(ws.room).delete(ws); | |
| if (rooms.get(ws.room).size === 0) { | |
| rooms.delete(ws.room); | |
| } | |
| } | |
| console.log(`User ${ws.userId || 'unknown'} disconnected`); | |
| }); | |
| ws.on('error', (error) => { | |
| console.error('WebSocket error:', error); | |
| }); | |
| }); | |
| const PORT = process.env.PORT || 7860; | |
| server.listen(PORT, '0.0.0.0', () => { | |
| console.log(`Signaling server running on port ${PORT}`); | |
| }); | |