-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
64 lines (55 loc) · 1.7 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
54
55
56
57
58
59
60
61
62
63
64
const express = require('express');
const app = express();
const router = require('./router');
const cors = require('cors');
const port = process.env.PORT || 3050;
const server = require ('http').Server(app);
const io = require('socket.io')(server, { cors: { origin: '*' }});
const {ExpressPeerServer} = require('peer');
const peerServer = ExpressPeerServer(server,
{
path: '/'
}
);
app.use(cors());
app.use(express.json());
app.use(router);
app.use('/peerjs', peerServer);
peerServer.on('connection', peer => {
console.log("Peer connected", peer.id);
});
const users = {};
const socketToRoom = {};
io.on('connection', socket => {
socket.on("join room", roomID => {
if (users[roomID]) {
const length = users[roomID].length;
if (length === 4) {
socket.emit("room full");
return;
}
users[roomID].push(socket.id);
} else {
users[roomID] = [socket.id];
}
socketToRoom[socket.id] = roomID;
const usersInThisRoom = users[roomID].filter(id => id !== socket.id);
socket.emit("all users", usersInThisRoom);
});
socket.on("sending signal", payload => {
io.to(payload.userToSignal).emit('user joined', { signal: payload.signal, callerID: payload.callerID });
});
socket.on("returning signal", payload => {
io.to(payload.callerID).emit('receiving returned signal', { signal: payload.signal, id: socket.id });
});
socket.on('endCall', () => {
const roomID = socketToRoom[socket.id];
let room = users[roomID];
if (room) {
room = room.filter(id => id !== socket.id);
users[roomID] = room;
}
socket.emit('callEnded');
});
});
server.listen(port, () => console.log(`Server listening on port ${port}...`));