Spaces:
Running
Running
Create server.js
Browse files
server.js
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const http = require('http');
|
3 |
+
const socketIo = require('socket.io');
|
4 |
+
|
5 |
+
const app = express();
|
6 |
+
const server = http.createServer(app);
|
7 |
+
const io = socketIo(server);
|
8 |
+
|
9 |
+
// Serve static files (HTML, JS)
|
10 |
+
app.use(express.static(__dirname));
|
11 |
+
|
12 |
+
// Socket.io connection
|
13 |
+
io.on('connection', (socket) => {
|
14 |
+
console.log('A user connected');
|
15 |
+
|
16 |
+
socket.on('joinRoom', (data) => {
|
17 |
+
socket.join(data.room); // Join the room
|
18 |
+
});
|
19 |
+
|
20 |
+
// Handle offer
|
21 |
+
socket.on('offer', (offer) => {
|
22 |
+
socket.to('voice-room').emit('offer', offer); // Send the offer to the other user in the room
|
23 |
+
});
|
24 |
+
|
25 |
+
// Handle answer
|
26 |
+
socket.on('answer', (answer) => {
|
27 |
+
socket.to('voice-room').emit('answer', answer); // Send answer to the other user
|
28 |
+
});
|
29 |
+
|
30 |
+
// Handle ICE candidates
|
31 |
+
socket.on('iceCandidate', (candidate) => {
|
32 |
+
socket.to('voice-room').emit('iceCandidate', candidate); // Forward ICE candidate to the other user
|
33 |
+
});
|
34 |
+
|
35 |
+
// Handle disconnect
|
36 |
+
socket.on('disconnect', () => {
|
37 |
+
console.log('A user disconnected');
|
38 |
+
});
|
39 |
+
});
|
40 |
+
|
41 |
+
// Start the server on port 3000
|
42 |
+
server.listen(3000, () => {
|
43 |
+
console.log('Server is running on http://localhost:3000');
|
44 |
+
});
|