decentralized-video-chat/index.js

87 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-03-22 05:23:46 +08:00
require('dotenv').config();
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
app.use(express.static('public'));
2020-03-23 00:47:35 +08:00
2020-03-24 08:57:57 +08:00
app.get('/', function (req, res) {
2020-03-23 01:36:29 +08:00
res.sendFile(path.join(public, 'chat.html'));
});
2020-03-24 08:57:57 +08:00
app.get('/landing', function (req, res) {
2020-03-23 01:36:29 +08:00
res.sendFile(path.join(public, 'landing.html'));
});
2020-03-23 01:09:22 +08:00
function log(msg, room) {
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) {
log('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.
log('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 {
log("room already full", room);
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) {
log('Received token request', room);
2020-03-23 00:47:35 +08:00
twilio.tokens.create(function (err, response) {
if (err) {
2020-03-23 01:09:22 +08:00
log(err, room);
2020-03-23 00:47:35 +08:00
} else {
2020-03-23 01:09:22 +08:00
log('Token generated. Returning it to the browser client', room);
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) {
log('Received candidate. Broadcasting...', room);
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) {
log('Received offer. Broadcasting...', room);
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) {
log('Received answer. Broadcasting...', room);
socket.broadcast.to(room).emit('answer', answer);
2020-03-23 00:47:35 +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