-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (56 loc) · 1.85 KB
/
Copy pathserver.js
File metadata and controls
70 lines (56 loc) · 1.85 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
app.use(express.static('public'));
const rooms = {}; // Track clients in each room
io.on('connection', (socket) => {
console.log('A user connected:', socket.id);
socket.on('create or join', (room) => {
if (!rooms[room]) {
rooms[room] = [];
}
rooms[room].push(socket.id);
socket.join(room);
const otherClients = rooms[room].filter(id => id !== socket.id);
console.log(`User ${socket.id} joined room ${room}. Others:`, otherClients);
socket.emit('all-users', otherClients);
otherClients.forEach(clientId => {
io.to(clientId).emit('new-user', socket.id);
});
});
socket.on('offer', (data) => {
io.to(data.target).emit('offer', {
sdp: data.sdp,
sender: socket.id
});
});
socket.on('answer', (data) => {
io.to(data.target).emit('answer', {
sdp: data.sdp,
sender: socket.id
});
});
socket.on('candidate', (data) => {
io.to(data.target).emit('candidate', {
candidate: data.candidate,
sender: socket.id
});
});
socket.on('disconnect', () => {
for (let room in rooms) {
if (rooms[room].includes(socket.id)) {
rooms[room] = rooms[room].filter(id => id !== socket.id);
socket.to(room).emit('user-disconnected', socket.id);
if (rooms[room].length === 0) {
delete rooms[room];
}
}
}
console.log('User disconnected:', socket.id);
});
});
http.listen(port, () => {
console.log('Server listening on port', port);
});