File size: 1,241 Bytes
d6fc514
 
 
 
 
 
 
19d7ae8
d6fc514
 
 
19d7ae8
d6fc514
19d7ae8
 
 
 
 
d6fc514
 
 
 
 
 
 
 
 
 
 
 
 
 
19d7ae8
d6fc514
 
 
 
 
 
 
 
 
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
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}`);
});