decentralized-video-chat/server.js

115 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-03-22 05:23:46 +08:00
require('dotenv').config();
2020-03-26 11:31:26 +08:00
var sslRedirect = require('heroku-ssl-redirect');
2020-03-23 01:09:22 +08:00
var twilio = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
2020-03-22 05:23:46 +08:00
var express = require('express');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
2020-03-23 01:36:29 +08:00
var path = require('path');
var public = path.join(__dirname, 'public');
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-03-23 01:36:29 +08:00
2020-03-27 04:44:58 +08:00
app.get('/', function (req, res) {
res.sendFile(path.join(public, 'landing.html'));
2020-03-25 12:54:56 +08:00
});
app.get('/newroom', function (req, res) {
2020-03-27 04:44:58 +08:00
res.sendFile(path.join(public, 'newroom.html'));
2020-03-23 01:36:29 +08:00
});
2020-03-27 04:44:58 +08:00
app.get('/room/*', function (req, res) {
2020-03-25 07:27:09 +08:00
res.sendFile(path.join(public, 'chat.html'));
});
2020-03-27 04:44:58 +08:00
app.use(express.static('public'));
2020-03-25 07:27:09 +08:00
function logIt(msg, room) {
2020-03-23 01:09:22 +08:00
console.log(room + ": " + 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-23 00:47:35 +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);
2020-03-23 00:47:35 +08:00
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);
2020-03-23 00:47:35 +08:00
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);
2020-03-23 00:47:35 +08:00
socket.emit('full', room);
}
});
2020-03-22 05:23:46 +08:00
2020-03-23 00:47:35 +08:00
// When receiving the token message, use the Twilio REST API to request an
// token to get ephemeral credentials to use the TURN server.
2020-03-23 01:09:22 +08:00
socket.on('token', function (room) {
logIt('Received token request', room);
2020-03-23 00:47:35 +08:00
twilio.tokens.create(function (err, response) {
if (err) {
logIt(err, room);
2020-03-23 00:47:35 +08:00
} else {
logIt('Token generated. Returning it to the browser client', room);
2020-03-23 01:09:22 +08:00
socket.emit('token', response).to(room);
2020-03-23 00:47:35 +08:00
}
});
2020-03-22 05:23:46 +08:00
});
2020-03-23 00:47:35 +08:00
// Relay candidate messages
2020-03-23 01:09:22 +08:00
socket.on('candidate', function (candidate, room) {
logIt('Received candidate. Broadcasting...', room);
2020-03-23 01:09:22 +08:00
socket.broadcast.to(room).emit('candidate', candidate);
2020-03-23 00:47:35 +08:00
});
2020-03-22 05:23:46 +08:00
2020-03-23 00:47:35 +08:00
// Relay offers
2020-03-23 01:09:22 +08:00
socket.on('offer', function (offer, room) {
logIt('Received offer. Broadcasting...', room);
2020-03-23 01:09:22 +08:00
socket.broadcast.to(room).emit('offer', offer);
2020-03-23 00:47:35 +08:00
});
2020-03-22 05:23:46 +08:00
2020-03-23 00:47:35 +08:00
// Relay answers
2020-03-23 01:09:22 +08:00
socket.on('answer', function (answer, room) {
logIt('Received answer. Broadcasting...', room);
2020-03-23 01:09:22 +08:00
socket.broadcast.to(room).emit('answer', answer);
2020-03-23 00:47:35 +08:00
});
2020-03-29 10:47:24 +08:00
// Relay answers
socket.on('sendCaptions', function (captions, room) {
// logIt(captions, room);
2020-03-29 10:47:24 +08:00
socket.broadcast.to(room).emit('recieveCaptions', captions);
});
// Relay answers
socket.on('requestToggleCaptions', function (room) {
logIt("requesting captions", room);
socket.broadcast.to(room).emit('requestToggleCaptions');
});
2020-03-30 02:59:59 +08:00
// Relay chat messages
socket.on('chat message', function(msg, room){
socket.broadcast.to(room).emit('chat message', msg);
2020-03-30 02:59:59 +08:00
});
2020-03-29 10:47:24 +08:00
2020-03-22 05:23:46 +08:00
});
2020-03-24 08:26:28 +08:00
2020-03-24 08:57:57 +08:00
var port = process.env.PORT || 3000;
2020-03-24 08:26:28 +08:00
http.listen(port, function () {
2020-03-24 08:57:57 +08:00
console.log("http://localhost:" + port);
2020-03-22 05:23:46 +08:00
});
2020-03-23 01:36:29 +08:00