2020-03-31 02:16:08 +08:00
|
|
|
require("dotenv").config();
|
|
|
|
var sslRedirect = require("heroku-ssl-redirect");
|
2020-04-06 23:17:07 +08:00
|
|
|
// 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);
|
2020-03-31 02:16:08 +08:00
|
|
|
var express = require("express");
|
2020-03-22 05:23:46 +08:00
|
|
|
var app = express();
|
2020-05-30 18:54:57 +08:00
|
|
|
const fs = require("fs");
|
2020-05-30 18:31:56 +08:00
|
|
|
var http = require("http").createServer(app);
|
2020-03-31 02:16:08 +08:00
|
|
|
var io = require("socket.io")(http);
|
|
|
|
var path = require("path");
|
|
|
|
var public = path.join(__dirname, "public");
|
2020-04-06 12:44:16 +08:00
|
|
|
const url = require("url");
|
2020-03-22 05:23:46 +08:00
|
|
|
|
2020-03-26 11:31:26 +08:00
|
|
|
// enable ssl redirect
|
|
|
|
app.use(sslRedirect());
|
2020-03-25 12:54:56 +08:00
|
|
|
|
2020-04-06 12:44:16 +08:00
|
|
|
// Remove trailing slashes in url
|
2020-04-03 21:48:40 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-31 02:16:08 +08:00
|
|
|
app.get("/", function (req, res) {
|
|
|
|
res.sendFile(path.join(public, "landing.html"));
|
2020-03-25 12:54:56 +08:00
|
|
|
});
|
|
|
|
|
2020-04-07 06:28:42 +08:00
|
|
|
app.get("/newcall", function (req, res) {
|
|
|
|
res.sendFile(path.join(public, "newcall.html"));
|
2020-03-23 01:36:29 +08:00
|
|
|
});
|
|
|
|
|
2020-04-05 09:06:07 +08:00
|
|
|
app.get("/join/", function (req, res) {
|
2020-04-01 05:07:03 +08:00
|
|
|
res.redirect("/");
|
|
|
|
});
|
|
|
|
|
2020-04-05 09:06:07 +08:00
|
|
|
app.get("/join/*", function (req, res) {
|
2020-04-01 05:07:03 +08:00
|
|
|
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, "chat.html"));
|
|
|
|
}
|
2020-03-25 07:27:09 +08:00
|
|
|
});
|
|
|
|
|
2020-04-06 02:28:50 +08:00
|
|
|
app.get("/notsupported", function (req, res) {
|
|
|
|
res.sendFile(path.join(public, "notsupported.html"));
|
|
|
|
});
|
|
|
|
|
2020-04-13 13:11:17 +08:00
|
|
|
app.get("/notsupportedios", function (req, res) {
|
|
|
|
res.sendFile(path.join(public, "notsupportedios.html"));
|
|
|
|
});
|
|
|
|
|
2020-04-06 12:44:16 +08:00
|
|
|
// Serve static files in the public directory
|
2020-03-31 02:16:08 +08:00
|
|
|
app.use(express.static("public"));
|
2020-03-25 07:27:09 +08:00
|
|
|
|
2020-04-06 12:44:16 +08:00
|
|
|
// Simple logging function to add room name
|
2020-03-28 00:32:22 +08:00
|
|
|
function logIt(msg, room) {
|
2020-04-01 05:07:03 +08:00
|
|
|
if (room) {
|
|
|
|
console.log(room + ": " + msg);
|
|
|
|
} else {
|
|
|
|
console.log(msg);
|
|
|
|
}
|
2020-03-23 00:47:35 +08:00
|
|
|
}
|
2020-03-23 01:09:22 +08:00
|
|
|
|
2020-03-22 05:23:46 +08:00
|
|
|
// When a socket connects, set up the specific listeners we will use.
|
2020-03-31 02:16:08 +08:00
|
|
|
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);
|
2020-05-30 19:21:27 +08:00
|
|
|
} else if (numClients < 5) {
|
2020-03-31 02:16:08 +08:00
|
|
|
socket.join(room);
|
2020-05-29 16:49:03 +08:00
|
|
|
// When the client is not the first to join the room, all clients are ready.
|
2020-03-31 02:16:08 +08:00
|
|
|
logIt("Broadcasting ready message", room);
|
2020-05-30 12:53:40 +08:00
|
|
|
socket.broadcast.to(room).emit("willInitiateCall", socket.id, room);
|
2020-03-31 02:16:08 +08:00
|
|
|
} else {
|
2020-05-30 18:54:57 +08:00
|
|
|
logIt(
|
|
|
|
"room already full with " + numClients + " people in the room.",
|
|
|
|
room
|
|
|
|
);
|
2020-03-31 02:16:08 +08:00
|
|
|
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.
|
2020-05-30 12:53:40 +08:00
|
|
|
socket.on("token", function (room, uuid) {
|
2020-03-31 02:16:08 +08:00
|
|
|
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);
|
2020-05-30 12:53:40 +08:00
|
|
|
socket.emit("token", response, uuid);
|
2020-03-31 02:16:08 +08:00
|
|
|
}
|
2020-03-23 00:47:35 +08:00
|
|
|
});
|
2020-03-31 02:16:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Relay candidate messages
|
2020-05-29 16:49:03 +08:00
|
|
|
socket.on("candidate", function (candidate, room, uuid) {
|
2020-03-31 02:16:08 +08:00
|
|
|
logIt("Received candidate. Broadcasting...", room);
|
2020-05-29 16:49:03 +08:00
|
|
|
io.to(uuid).emit("candidate", candidate, socket.id);
|
2020-03-31 02:16:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Relay offers
|
2020-05-29 16:49:03 +08:00
|
|
|
socket.on("offer", function (offer, room, uuid) {
|
2020-05-30 18:54:57 +08:00
|
|
|
logIt(
|
|
|
|
"Received offer from " + socket.id + " and emitting to " + uuid,
|
|
|
|
room
|
|
|
|
);
|
2020-05-29 16:49:03 +08:00
|
|
|
io.to(uuid).emit("offer", offer, socket.id);
|
2020-03-31 02:16:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Relay answers
|
2020-05-29 16:49:03 +08:00
|
|
|
socket.on("answer", function (answer, room, uuid) {
|
2020-05-30 18:54:57 +08:00
|
|
|
logIt(
|
|
|
|
"Received answer from " + socket.id + " and emitting to " + uuid,
|
|
|
|
room
|
|
|
|
);
|
2020-05-29 16:49:03 +08:00
|
|
|
io.to(uuid).emit("answer", answer, socket.id);
|
2020-03-31 02:16:08 +08:00
|
|
|
});
|
2020-03-22 05:23:46 +08:00
|
|
|
});
|
|
|
|
|
2020-04-06 12:44:16 +08:00
|
|
|
// Listen for Heroku port, otherwise just use 3000
|
2020-05-30 18:33:41 +08:00
|
|
|
var port = process.env.PORT || 3000;
|
2020-03-24 08:26:28 +08:00
|
|
|
http.listen(port, function () {
|
2020-03-31 02:16:08 +08:00
|
|
|
console.log("http://localhost:" + port);
|
2020-03-22 05:23:46 +08:00
|
|
|
});
|