2020-03-22 05:23:46 +08:00
|
|
|
require('dotenv').config();
|
|
|
|
|
|
|
|
// Twilio init
|
|
|
|
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);
|
|
|
|
|
|
|
|
app.use(express.static('public'));
|
|
|
|
// 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){
|
2020-03-22 22:02:40 +08:00
|
|
|
|
2020-03-22 05:23:46 +08:00
|
|
|
console.log('A client joined')
|
|
|
|
var clients = io.sockets.adapter.rooms[room];
|
|
|
|
var numClients = typeof clients !== 'undefined' ? clients.length : 0;
|
2020-03-22 22:02:40 +08:00
|
|
|
if(numClients === 0){
|
2020-03-22 05:23:46 +08:00
|
|
|
socket.join(room);
|
|
|
|
socket.emit('firstin', room);
|
2020-03-22 22:02:40 +08:00
|
|
|
}else if(numClients === 1){
|
2020-03-22 05:23:46 +08:00
|
|
|
socket.join(room);
|
|
|
|
// When the client is second to join the room, both clients are ready.
|
|
|
|
console.log('Broadcasting ready message')
|
|
|
|
socket.emit('ready', room);
|
|
|
|
socket.broadcast.emit('ready', room);
|
|
|
|
}else{
|
|
|
|
console.log("room full");
|
|
|
|
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(){
|
2020-03-22 22:02:40 +08:00
|
|
|
console.log('Received token request');
|
2020-03-22 05:23:46 +08:00
|
|
|
twilio.tokens.create(function(err, response){
|
|
|
|
if(err){
|
|
|
|
console.log(err);
|
|
|
|
}else{
|
|
|
|
// Return the token to the browser.
|
2020-03-22 22:02:40 +08:00
|
|
|
console.log('Token generated. Returning it to the client');
|
2020-03-22 05:23:46 +08:00
|
|
|
socket.emit('token', response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Relay candidate messages
|
|
|
|
socket.on('candidate', function(candidate){
|
2020-03-22 22:02:40 +08:00
|
|
|
console.log('Received candidate. Broadcasting...');
|
2020-03-22 05:23:46 +08:00
|
|
|
socket.broadcast.emit('candidate', candidate);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Relay offers
|
|
|
|
socket.on('offer', function(offer){
|
2020-03-22 22:02:40 +08:00
|
|
|
console.log('Received offer. Broadcasting...');
|
2020-03-22 05:23:46 +08:00
|
|
|
socket.broadcast.emit('offer', offer);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Relay answers
|
|
|
|
socket.on('answer', function(answer){
|
2020-03-22 22:02:40 +08:00
|
|
|
console.log('Received answer. Broadcasting...');
|
2020-03-22 05:23:46 +08:00
|
|
|
socket.broadcast.emit('answer', answer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
http.listen(3000, function() {
|
|
|
|
console.log("http://localhost:3000");
|
|
|
|
});
|