audio / server.js
vish85521's picture
Update server.js
19d7ae8 verified
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const PORT = process.env.PORT || 7860;
// Serve the files from the 'public' directory
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log('User connected to page:', socket.id);
// Wait for the user to turn on their mic before telling others to call them
socket.on('join-room', () => {
console.log('User joined audio:', socket.id);
socket.broadcast.emit('user-connected', socket.id);
});
// Handle chat messages
socket.on('chat-message', (msg) => {
io.emit('chat-message', { id: socket.id, text: msg });
});
// WebRTC Signaling: Route offers, answers, and ICE candidates
socket.on('signal', (data) => {
io.to(data.to).emit('signal', {
from: socket.id,
signal: data.signal
});
});
// Handle user leaving the page
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
socket.broadcast.emit('user-disconnected', socket.id);
});
});
http.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});