-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (33 loc) · 927 Bytes
/
server.js
File metadata and controls
40 lines (33 loc) · 927 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
38
39
40
const io = require('socket.io')(3000, {
cors: { origin: '*' },
pingTimeout: 60000,
pingInterval: 25000
});
const rooms = {};
io.on('connection', (socket) => {
socket.on('create-id', () => {
const id = Math.random().toString(36).substring(2, 8).toUpperCase();
rooms[id] = socket.id;
socket.emit('id-generated', id);
});
socket.on('join-id', (id) => {
const hostId = rooms[id];
if (!hostId) {
socket.emit('error', 'ID não encontrado');
return;
}
socket.emit('peer-joined', hostId);
io.to(hostId).emit('peer-ready', socket.id);
});
socket.on('peer-ready', ({ to }) => {
io.to(to).emit('peer-ready', socket.id);
});
socket.on('signal', ({ to, signal }) => {
io.to(to).emit('signal', { from: socket.id, signal });
});
socket.on('disconnect', () => {
for (const id in rooms) {
if (rooms[id] === socket.id) delete rooms[id];
}
});
});