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}`); });