-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
78 lines (67 loc) · 1.84 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require("express");
const http = require("http");
const path = require("path");
const app = express();
const server = http.createServer(app);
const socket = require("socket.io");
const io = socket(server);
const users = {};
io.on("connection", (socket) => {
if (!users[socket.id]) {
users[socket.id] = socket.id;
}
socket.emit("yourID", socket.id);
io.sockets.emit("allUsers", users);
socket.on("disconnect", () => {
delete users[socket.id];
});
socket.on("message", (data) => {
io.to(data.to).emit("message", data.message);
});
socket.on("callUser", (data) => {
io.to(data.userToCall).emit("hey", {
signal: data.signalData,
from: data.from,
});
});
socket.on("acceptCall", (data) => {
io.to(data.to).emit("callAccepted", data.signal);
});
socket.on("declineCall", (data) => {
io.to(data.to).emit("callDeclined");
});
socket.on("cancelCall", (data) => {
io.to(data.to).emit("callCancelled");
});
});
if (process.env.NODE_ENV === "production") {
/*
* Redirect user to https if requested on http
*
* Refer this for explaination:
* https://www.tonyerwin.com/2014/09/redirecting-http-to-https-with-nodejs.html
*/
app.enable("trust proxy");
app.use((req, res, next) => {
// console.log('secure check');
if (req.secure) {
// console.log('secure');
// request was via https, so do no special handling
next();
} else {
//
// request was via http, so redirect to https
res.redirect(`https://${req.headers.host}${req.url}`);
}
});
}
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static(path.join(__dirname, "./client/build/")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
}
server.listen(process.env.PORT || 8000, () =>
console.log(`Server is running on port ${process.env.PORT || 8000}`)
);