mirror of
https://github.com/ianramzy/decentralized-video-chat.git
synced 2024-11-14 22:29:20 +08:00
118 lines
3.4 KiB
JavaScript
118 lines
3.4 KiB
JavaScript
require("dotenv").config();
|
|
var sslRedirect = require("heroku-ssl-redirect");
|
|
var twilio = require("twilio")(
|
|
process.env.TWILIO_ACCOUNT_SID,
|
|
process.env.TWILIO_AUTH_TOKEN
|
|
);
|
|
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"); // built-in utility
|
|
|
|
// enable ssl redirect
|
|
app.use(sslRedirect());
|
|
|
|
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, "landing.html"));
|
|
});
|
|
|
|
app.get("/pickname", function (req, res) {
|
|
res.sendFile(path.join(public, "pickname.html"));
|
|
});
|
|
|
|
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, "chat.html"));
|
|
}
|
|
});
|
|
|
|
app.use(express.static("public"));
|
|
|
|
function logIt(msg, room) {
|
|
if (room) {
|
|
console.log(room + ": " + msg);
|
|
} else {
|
|
console.log(msg);
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
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);
|
|
});
|
|
});
|
|
|
|
var port = process.env.PORT || 3000;
|
|
http.listen(port, function () {
|
|
console.log("http://localhost:" + port);
|
|
});
|