-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
183 lines (157 loc) · 5.65 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require("dotenv").config();
var sslRedirect = require("heroku-ssl-redirect");
// Twillio config
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 path = require("path");
const app = express();
// Live reload for development
if (process.env.NODE_ENV === "development") {
const livereload = require("livereload");
const connectLivereload = require("connect-livereload");
// open livereload high port and start to watch public directory for changes
const liveReloadServer = livereload.createServer();
liveReloadServer.watch(path.join(__dirname, "public"));
// ping browser on Express boot, once browser has reconnected and handshaken
liveReloadServer.server.once("connection", () => {
setTimeout(() => {
liveReloadServer.refresh("/");
}, 100);
});
// monkey patch every served HTML so they know of changes
app.use(connectLivereload());
}
var minify = require("express-minify");
app.use(minify());
var http = require("http").createServer(app);
var io = require("socket.io")(http);
var public = path.join(__dirname, "public");
const url = require("url");
const chalk = require("chalk");
app.use(sslRedirect());
// Serve static files in the public directory
app.use(express.static("public"));
// 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, "newcall.html"));
});
app.get("/newcall", function (req, res) {
res.sendFile(path.join(public, "newcall.html"));
});
app.get("/join/", function (req, res) {
res.redirect("/");
});
app.get("/join/*", function (req, res) {
// Removed to support URL params
// if (Object.keys(req.query).length > 0) {
// console.log(
// "Redirecting from:" + req.url + " to " + url.parse(req.url).pathname
// );
// res.redirect(url.parse(req.url).pathname);
// } else {
// res.sendFile(path.join(public, "chat.html"));
// }
res.sendFile(path.join(public, "chat.html"));
});
app.get("/browser-not-supported", function (req, res) {
res.sendFile(path.join(public, "incompatible.html"));
});
app.get("/browser-not-supported", function (req, res) {
res.sendFile(path.join(public, "notsupportedios.html"));
});
// BEGIN SOCKETIO COMMUNICATION FOR WEBRTC
// 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, acknowledgement) {
console.log("Client joining: ", room);
acknowledgement();
var clients = io.sockets.adapter.rooms[room];
var numClients = typeof clients !== "undefined" ? clients.length : 0;
if (numClients === 0) {
socket.join(room);
} else if (numClients < 15) {
socket.join(room);
// When the client is not the first to join the room, all clients are ready.
console.log("Ready message broadcasted to: ", room);
socket.broadcast.to(room).emit("willInitiateCall", socket.id, room);
} else {
console.log(
room,
" already full with ",
numClients,
"clients in the room."
);
socket.emit("full", room);
}
});
// Client is disconnecting from the server
socket.on("disconnecting", () => {
var room = Object.keys(socket.rooms).filter((item) => item != socket.id); // Socket joins a room of itself, remove that
console.log("Client disconnected from: ", room);
socket.broadcast.to(room).emit("leave", socket.id);
});
// 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, uuid) {
console.log("Received token request to:", room);
twilio.tokens.create(function (err, response) {
if (err) {
console.log(err, "for", room);
} else {
console.log(
room + ": Token generated. Returning it to the browser client"
);
socket.emit("token", response, uuid);
}
});
});
// Relay candidate messages
socket.on("candidate", function (candidate, room, uuid) {
console.log("Received candidate. Broadcasting...", room);
io.to(uuid).emit("candidate", candidate, socket.id);
});
// Relay offers
socket.on("offer", function (offer, room, uuid) {
console.log(
"Offer from " + socket.id + "... emitting to " + uuid + "in room: ",
room
);
io.to(uuid).emit("offer", offer, socket.id);
});
// Relay answers
socket.on("answer", function (answer, room, uuid) {
console.log(
"Answer from " + socket.id + "... emitting to " + uuid + "in room: ",
room
);
io.to(uuid).emit("answer", answer, socket.id);
});
});
var port = process.env.PORT || 3000;
http.listen(port, function () {
if (process.env.NODE_ENV === "development") {
console.warn(chalk`📙: When running in development mode, connections to the server will not be over HTTPS.
If you want to connect to a call from an address {green.bold besides localhost or from another computer},
you must use a service like ngrok or localtunnel.`);
}
console.log(
chalk.green(
"Running Catalyst server on ",
chalk.underline.bgWhite.black.bold(`http://localhost:${port}`)
)
);
});