-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (29 loc) · 969 Bytes
/
Copy pathserver.js
File metadata and controls
37 lines (29 loc) · 969 Bytes
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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log('A user connected:', socket.id);
socket.on('join', (role) => {
socket.join(role);
console.log(`User ${socket.id} joined as ${role}`);
socket.broadcast.emit('user-joined', role);
});
socket.on('offer', (data) => {
socket.broadcast.emit('offer', data);
});
socket.on('answer', (data) => {
socket.broadcast.emit('answer', data);
});
socket.on('candidate', (data) => {
socket.broadcast.emit('candidate', data);
});
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
});
});
const PORT = process.env.PORT || 3000;
http.listen(PORT, '0.0.0.0', () => {
console.log(`Server running at http://localhost:${PORT}`);
});