-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
158 lines (135 loc) · 4.66 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require("dotenv").config();
var sslRedirect = require("heroku-ssl-redirect");
var favicon = require("serve-favicon");
// Get twillio auth and SID from heroku if deployed, else get from local .env file
var twillioAuthToken =
process.env.HEROKU_AUTH_TOKEN || process.env.LOCAL_AUTH_TOKEN;
var twillioAccountSID =
process.env.HEROKU_TWILLIO_SID || process.env.LOCAL_TWILLIO_SID;
var twilio = require("twilio")(twillioAccountSID, twillioAuthToken);
var express = require("express");
var app = express();
var http = require("http").createServer(app);
var io = require("socket.io")(http);
var path = require("path");
var public = path.join(__dirname, "public");
const url = require("url");
function redirectUnmatched(req, res) {
res.redirect("/");
}
// favicon
app.use(favicon(path.join(__dirname, "public", "assets", "img", "logo.png")));
// enable ssl redirect
app.use(sslRedirect());
// Remove trailing slashes in url
app.use(function (req, res, next) {
if (req.path.substr(-1) === "/" && req.path.length > 1) {
let query = req.url.slice(req.path.length);
res.redirect(301, req.path.slice(0, -1) + query);
} else {
next();
}
});
app.get("/", function (req, res) {
res.sendFile(path.join(public, "home.html"));
});
app.get("/demo", function (req, res) {
res.redirect("https://youtu.be/1GUb3c0IIh0");
});
app.get("/ClefNet", function (req, res) {
res.redirect("/ClefNet.pdf");
});
app.get("/repo", function (req, res) {
res.redirect("http://github.com/rvignav/ClefNet");
});
app.get("/preprints", function (req, res) {
res.redirect("https://www.preprints.org/manuscript/202103.0360/v1");
});
app.get("/paper", function (req, res) {
res.redirect("/ClefNet.pdf");
});
app.get("/join/", function (req, res) {
res.redirect("/");
});
app.get("/join/*", function (req, res) {
if (Object.keys(req.query).length > 0) {
logIt("redirect:" + req.url + " to " + url.parse(req.url).pathname);
res.redirect(url.parse(req.url).pathname);
} else {
res.sendFile(path.join(public, "call.html"));
}
});
app.get("/unsupported", function (req, res) {
res.sendFile(path.join(public, "/unsupported/unsupported.html"));
});
app.get("/unsupported-apple", function (req, res) {
res.sendFile(path.join(public, "/unsupported/unsupported-apple.html"));
});
// Serve static files in the public directory
app.use(express.static("public"));
// Simple logging function to add room name
function logIt(msg, room) {
if (room) {
console.log(room + ": " + msg);
} else {
console.log(msg);
}
}
app.use(redirectUnmatched);
// When a socket connects, set up the specific listeners we will use.
io.on("connection", function (socket) {
// When a client tries to join a room, only allow them if they are first or
// second in the room. Otherwise it is full.
socket.on("join", function (room) {
logIt("A client joined the room", room);
var clients = io.sockets.adapter.rooms[room];
var numClients = typeof clients !== "undefined" ? clients.length : 0;
if (numClients === 0) {
socket.join(room);
} else if (numClients === 1) {
socket.join(room);
// When the client is second to join the room, both clients are ready.
logIt("Broadcasting ready message", room);
// First to join call initiates call
socket.broadcast.to(room).emit("willInitiateCall", room);
socket.emit("ready", room).to(room);
socket.broadcast.to(room).emit("ready", room);
} else {
logIt("room already full", room);
socket.emit("full", room);
}
});
// When receiving the token message, use the Twilio REST API to request an
// token to get ephemeral credentials to use the TURN server.
socket.on("token", function (room) {
logIt("Received token request", room);
twilio.tokens.create(function (err, response) {
if (err) {
logIt(err, room);
} else {
logIt("Token generated. Returning it to the browser client", room);
socket.emit("token", response).to(room);
}
});
});
// Relay candidate messages
socket.on("candidate", function (candidate, room) {
logIt("Received candidate. Broadcasting...", room);
socket.broadcast.to(room).emit("candidate", candidate);
});
// Relay offers
socket.on("offer", function (offer, room) {
logIt("Received offer. Broadcasting...", room);
socket.broadcast.to(room).emit("offer", offer);
});
// Relay answers
socket.on("answer", function (answer, room) {
logIt("Received answer. Broadcasting...", room);
socket.broadcast.to(room).emit("answer", answer);
});
});
// Listen for Heroku port, otherwise just use 3000
var port = process.env.PORT || 3000;
http.listen(port, function () {
console.log("http://localhost:" + port);
});