diff --git a/.env b/.env new file mode 100644 index 0000000..32164cc --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ + +# Required for all uses +TWILIO_ACCOUNT_SID=ACfc9705a2b725a7a314995eb8635a9079 +TWILIO_AUTH_TOKEN=9ab36b8ac190d20687aff0561749dbc1 diff --git a/.gitignore b/.gitignore index c9bddd2..281707e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ node_modules/ -.env .DS_Store /.idea \ No newline at end of file diff --git a/index.js b/index.js index 0422b55..d9abb53 100644 --- a/index.js +++ b/index.js @@ -16,13 +16,14 @@ 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){ + console.log('A client joined') var clients = io.sockets.adapter.rooms[room]; var numClients = typeof clients !== 'undefined' ? clients.length : 0; - if(numClients == 0){ + if(numClients === 0){ socket.join(room); socket.emit('firstin', room); - }else if(numClients == 1){ + }else if(numClients === 1){ socket.join(room); // When the client is second to join the room, both clients are ready. console.log('Broadcasting ready message') @@ -37,13 +38,13 @@ 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') + console.log('Received token request'); twilio.tokens.create(function(err, response){ if(err){ console.log(err); }else{ // Return the token to the browser. - console.log('Token generated. Returning it to the client') + console.log('Token generated. Returning it to the client'); socket.emit('token', response); } }); @@ -51,19 +52,19 @@ io.on('connection', function(socket){ // Relay candidate messages socket.on('candidate', function(candidate){ - console.log('Received candidate. Broadcasting...') + console.log('Received candidate. Broadcasting...'); socket.broadcast.emit('candidate', candidate); }); // Relay offers socket.on('offer', function(offer){ - console.log('Received offer. Broadcasting...') + console.log('Received offer. Broadcasting...'); socket.broadcast.emit('offer', offer); }); // Relay answers socket.on('answer', function(answer){ - console.log('Received answer. Broadcasting...') + console.log('Received answer. Broadcasting...'); socket.broadcast.emit('answer', answer); }); }); diff --git a/public/app.js b/public/app.js index 8c66e63..e761478 100644 --- a/public/app.js +++ b/public/app.js @@ -1,15 +1,26 @@ +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 num = Math.floor(Math.ran1dom() * 100) + location.hash = adjective + "-" + noun +} +const roomHash = location.hash.substring(1); + function logIt(message, error) { // Print on console console.log(message); // Add to logs on page - let logs = document.getElementById('logs'); - let tmp = document.createElement('P'); - tmp.innerText = message; - if (error) { - tmp.classList.add('error'); - } - logs.appendChild(tmp); + // let logs = document.getElementById('logs'); + // let tmp = document.createElement('P'); + // tmp.innerText = message; + // if (error) { + // tmp.classList.add('error'); + // } + // logs.appendChild(tmp); } // Create an object to save various objects to without polluting the global namespace. @@ -17,6 +28,8 @@ var VideoChat = { connected: false, firstInChat : false, localICECandidates: [], + // Initialise our connection to the WebSocket. + socket: io(), remoteVideo: document.getElementById('remote-video'), localVideo: document.getElementById('local-video'), // videoButton: document.getElementById('get-video'), @@ -24,8 +37,7 @@ var VideoChat = { // callButton: document.getElementById('call'), -// Initialise our connection to the WebSocket. - socket: io(), + // Call to getUserMedia (provided by adapter.js for cross browser compatibility) // asking for access to both the video and audio streams. If the request is diff --git a/public/index.html b/public/index.html index 0829936..7aec0a7 100644 --- a/public/index.html +++ b/public/index.html @@ -54,9 +54,9 @@ -
Log messages:
-