Working multiple rooms

This commit is contained in:
ian ramzy 2020-03-22 13:09:22 -04:00
parent 72e8c0a865
commit 8283baa636
2 changed files with 26 additions and 33 deletions

View File

@ -1,10 +1,6 @@
require('dotenv').config();
// Twilio init
var twilio = require('twilio')(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
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);
@ -13,9 +9,9 @@ var io = require('socket.io')(http);
app.use(express.static('public'));
function log(msg, room) {
console.log(room + " - " + msg)
console.log(room + ": " + 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
@ -26,10 +22,8 @@ io.on('connection', function (socket) {
var numClients = typeof clients !== 'undefined' ? clients.length : 0;
if (numClients === 0) {
socket.join(room);
// socket.emit("roomtest", room).to(room)
} else if (numClients === 1) {
socket.join(room);
// socket.emit("roomtest", room).to(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);
@ -43,35 +37,34 @@ io.on('connection', function (socket) {
// 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 () {
console.log('Received token request');
socket.on('token', function (room) {
log('Received token request', room);
twilio.tokens.create(function (err, response) {
if (err) {
console.log(err);
log(err, room);
} else {
// Return the token to the browser.
console.log('Token generated. Returning it to the client');
socket.emit('token', response);
log('Token generated. Returning it to the browser client', room);
socket.emit('token', response).to(room);
}
});
});
// Relay candidate messages
socket.on('candidate', function (candidate) {
console.log('Received candidate. Broadcasting...');
socket.broadcast.emit('candidate', candidate);
socket.on('candidate', function (candidate, room) {
log('Received candidate. Broadcasting...', room);
socket.broadcast.to(room).emit('candidate', candidate);
});
// Relay offers
socket.on('offer', function (offer) {
console.log('Received offer. Broadcasting...');
socket.broadcast.emit('offer', offer);
socket.on('offer', function (offer, room) {
log('Received offer. Broadcasting...', room);
socket.broadcast.to(room).emit('offer', offer);
});
// Relay answers
socket.on('answer', function (answer) {
console.log('Received answer. Broadcasting...');
socket.broadcast.emit('answer', answer);
socket.on('answer', function (answer, room) {
log('Received answer. Broadcasting...', room);
socket.broadcast.to(room).emit('answer', answer);
});
});

View File

@ -2,8 +2,8 @@ if (!location.hash) {
// Generate random room name if needed
var adjectives = ["small", "big", "large", "smelly", "new", "happy", "shiny", "old", "clean", "nice", "bad", "cool", "hot", "cold", "warm", "hungry", "slow", "fast"]
var nouns = ["dog", "bat", "wrench", "apple", "pear", "ghost", "cat", "wolf", "squid", "goat", "snail", "hat", "sock", "plum", "bear", "snake", "turtle", "horse","spoon","fork","spider","tree","chair","table"]
var adjective = adjectives[Math.floor(Math.random() * adjectives.length)]
var noun = nouns[Math.floor(Math.random() * nouns.length)]
var adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
var noun = nouns[Math.floor(Math.random() * nouns.length)];
location.hash = adjective + noun
}
const roomHash = location.hash.substring(1);
@ -104,7 +104,7 @@ var VideoChat = {
console.log("startCall");
logIt('>>> Sending token request...');
VideoChat.socket.on('token', VideoChat.onToken(VideoChat.createOffer));
VideoChat.socket.emit('token');
VideoChat.socket.emit('token', roomHash);
// VideoChat.callButton.disabled = true
},
@ -137,7 +137,7 @@ var VideoChat = {
logIt(`<<< Received local ICE candidate from STUN/TURN server (${event.candidate.address})`);
if (VideoChat.connected) {
logIt(`>>> Sending local ICE candidate (${event.candidate.address})`);
VideoChat.socket.emit('candidate', JSON.stringify(event.candidate));
VideoChat.socket.emit('candidate', JSON.stringify(event.candidate), roomHash);
} else {
// If we are not 'connected' to the other peer, we are buffering the local ICE candidates.
// This most likely is happening on the "caller" side.
@ -167,7 +167,7 @@ var VideoChat = {
// and send it over the socket connection to initiate the peerConnection
// on the other side.
VideoChat.peerConnection.setLocalDescription(offer);
VideoChat.socket.emit('offer', JSON.stringify(offer));
VideoChat.socket.emit('offer', JSON.stringify(offer), roomHash);
},
function (err) {
logIt("failed offer creation");
@ -191,7 +191,7 @@ var VideoChat = {
VideoChat.peerConnection.createAnswer(
function (answer) {
VideoChat.peerConnection.setLocalDescription(answer);
VideoChat.socket.emit('answer', JSON.stringify(answer));
VideoChat.socket.emit('answer', JSON.stringify(answer), roomHash);
},
function (err) {
logIt("Failed answer creation.");
@ -207,7 +207,7 @@ var VideoChat = {
console.log("onOffer");
logIt('<<< Received offer');
VideoChat.socket.on('token', VideoChat.onToken(VideoChat.createAnswer(offer)));
VideoChat.socket.emit('token');
VideoChat.socket.emit('token', roomHash);
},
// When an answer is received, add it to the peerConnection as the remote
@ -221,7 +221,7 @@ var VideoChat = {
VideoChat.localICECandidates.forEach(candidate => {
// The caller now knows that the callee is ready to accept new ICE candidates, so sending the buffer over
logIt(`>>> Sending local ICE candidate (${candidate.address})`);
VideoChat.socket.emit('candidate', JSON.stringify(candidate));
VideoChat.socket.emit('candidate', JSON.stringify(candidate), roomHash);
});
// Reset the buffer of local ICE candidates. This is not really needed
// in this specific client, but it's good practice