Spaces:
Sleeping
Sleeping
File size: 3,557 Bytes
41bc250 a4c7ed3 41bc250 a4c7ed3 41bc250 5b94e58 a4c7ed3 5b94e58 41bc250 5b94e58 41bc250 | 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 | import express from 'express';
import { createServer } from 'http';
import { Server } from 'socket.io';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
// Serve static files from the React build (dist)
app.use(express.static(join(__dirname, 'dist')));
// Handle SPA routing - send index.html for any unknown route
app.get('*', (req, res) => {
res.sendFile(join(__dirname, 'dist', 'index.html'));
});
// Socket.io Signaling for WebRTC
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
socket.on('join-room', (roomId) => {
const room = io.sockets.adapter.rooms.get(roomId);
const numClients = room ? room.size : 0;
if (numClients < 10) {
socket.join(roomId);
// Get all OTHER users in the room
const otherUsers = [];
if (room) {
room.forEach(id => {
if (id !== socket.id) otherUsers.push(id);
});
}
// Send list of existing users to the new user
socket.emit('all-users', otherUsers);
// Notify others that a new user joined - REMOVED to avoid double notification with user-info
// socket.to(roomId).emit('user-joined', { signal: null, callerID: socket.id });
} else {
socket.emit('full');
return;
}
// Broadcast updated count
const updatedRoom = io.sockets.adapter.rooms.get(roomId);
io.to(roomId).emit('room-count', updatedRoom ? updatedRoom.size : 0);
});
// Relay WebRTC signals to specific peers
socket.on('offer', ({ offer, to }) => {
io.to(to).emit('offer', { offer, from: socket.id });
});
socket.on('answer', ({ answer, to }) => {
io.to(to).emit('answer', { answer, from: socket.id });
});
socket.on('ice-candidate', ({ candidate, to }) => {
io.to(to).emit('ice-candidate', { candidate, from: socket.id });
});
// Relay Captions (Broadcast to room)
socket.on('caption', ({ text, userName, roomId }) => {
socket.to(roomId).emit('caption', { text, userName });
});
// Relay Chat Messages (Broadcast to room)
socket.on('chat-message', ({ text, userName, roomId }) => {
socket.to(roomId).emit('chat-message', { text, userName });
});
// Exchange User Info (Broadcast to room)
socket.on('user-info', ({ userName, roomId }) => {
// Broadcast to others in the room that this user joined
socket.to(roomId).emit('user-joined', { userName, callerID: socket.id });
// Send user info to room
socket.to(roomId).emit('user-info', { userName, id: socket.id });
});
socket.on('disconnecting', () => {
for (const room of socket.rooms) {
if (room !== socket.id) {
// Get current count, minus this socket which is about to leave
const roomObj = io.sockets.adapter.rooms.get(room);
const count = roomObj ? roomObj.size - 1 : 0;
socket.to(room).emit('room-count', count);
socket.to(room).emit('user-disconnected', socket.id);
socket.to(room).emit('user-left', { userName: 'A user' });
}
}
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
// Use port 7860 for Hugging Face Spaces compatibility, or default to 3000
const PORT = process.env.PORT || 7860;
httpServer.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
}); |