-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (41 loc) · 1.22 KB
/
server.js
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
const http = require('http');
const { Server } = require('socket.io');
const httpServer = http.createServer();
const io = new Server(httpServer, {
cors: {
origin: 'http://localhost:3000',
methods: ['GET', 'POST'],
allowedHeaders: ['my-custom-header'],
credentials: true,
},
});
const peers = [];
io.on('connection', (socket) => {
const ids = Array.from(io.of('/').sockets.keys());
socket.emit('users-connected', ids);
console.log('A user connected:', socket.id);
socket.on('user-joined', (payload) => {
try {
peers.push(payload);
io.emit('receive-peers', peers);
} catch (ex) {
console.log('Error Returning Signal:', ex);
}
});
socket.on('join_room', (roomId) => {
socket.join(roomId);
console.log(`user with id-${socket.id} joined room - ${roomId}`);
});
socket.on('send_msg', (data) => {
console.log(data, 'DATA');
//This will send a message to a specific room ID
socket.to(data.roomId).emit('receive_msg', data);
});
socket.on('disconnect', () => {
console.log('A user disconnected:', socket.id);
});
});
const PORT = process.env.PORT || 3001;
httpServer.listen(PORT, () => {
console.log(`Socket.io server is running on port ${PORT}`);
});