-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (51 loc) · 1.78 KB
/
server.js
File metadata and controls
63 lines (51 loc) · 1.78 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
require('dotenv').config();
const express = require("express");
const http = require("http");
const app = express();
const server = http.createServer(app);
var io = require('socket.io').listen(server);
const path = require("path");
const users = {};
const socketToRoom = {};
io.on('connection', socket => {
socket.on("join room", vroomID => {
if (users[vroomID]) {
const length = users[vroomID].length;
if (length === 10) {
socket.emit("room full");
return;
}
users[vroomID].push(socket.id);
} else {
users[vroomID] = [socket.id];
}
socketToRoom[socket.id] = vroomID;
const usersInThisRoom = users[vroomID].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('disconnect', () => {
const vroomID = socketToRoom[socket.id];
let room = users[vroomID];
if (room) {
room = room.filter(id => id !== socket.id);
users[vroomID] = room;
}
socket.broadcast.emit('user left', socket.id);
});
});
if(process.env.NODE_ENV === 'production'){
app.use(express.static('sendit/build'));
app.get(' * ', (req,res) =>{
res.sendFile(path.join(__dirname, 'sendit/build' , 'index.html'));
});
}
const port = process.env.PORT || 8000;
server.listen(port, () => console.log(
`Server is running on port ${port}`
));