mirror of
https://github.com/ianramzy/decentralized-video-chat.git
synced 2024-11-23 18:49:21 +08:00
Hide logs + cleanup + add smart room hash
This commit is contained in:
parent
5d7e55f7bf
commit
839539b5fa
4
.env
Normal file
4
.env
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
# Required for all uses
|
||||||
|
TWILIO_ACCOUNT_SID=ACfc9705a2b725a7a314995eb8635a9079
|
||||||
|
TWILIO_AUTH_TOKEN=9ab36b8ac190d20687aff0561749dbc1
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
.env
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
/.idea
|
/.idea
|
15
index.js
15
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
|
// When a client tries to join a room, only allow them if they are first or
|
||||||
// second in the room. Otherwise it is full.
|
// second in the room. Otherwise it is full.
|
||||||
socket.on('join', function(room){
|
socket.on('join', function(room){
|
||||||
|
|
||||||
console.log('A client joined')
|
console.log('A client joined')
|
||||||
var clients = io.sockets.adapter.rooms[room];
|
var clients = io.sockets.adapter.rooms[room];
|
||||||
var numClients = typeof clients !== 'undefined' ? clients.length : 0;
|
var numClients = typeof clients !== 'undefined' ? clients.length : 0;
|
||||||
if(numClients == 0){
|
if(numClients === 0){
|
||||||
socket.join(room);
|
socket.join(room);
|
||||||
socket.emit('firstin', room);
|
socket.emit('firstin', room);
|
||||||
}else if(numClients == 1){
|
}else if(numClients === 1){
|
||||||
socket.join(room);
|
socket.join(room);
|
||||||
// When the client is second to join the room, both clients are ready.
|
// When the client is second to join the room, both clients are ready.
|
||||||
console.log('Broadcasting ready message')
|
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
|
// When receiving the token message, use the Twilio REST API to request an
|
||||||
// token to get ephemeral credentials to use the TURN server.
|
// token to get ephemeral credentials to use the TURN server.
|
||||||
socket.on('token', function(){
|
socket.on('token', function(){
|
||||||
console.log('Received token request')
|
console.log('Received token request');
|
||||||
twilio.tokens.create(function(err, response){
|
twilio.tokens.create(function(err, response){
|
||||||
if(err){
|
if(err){
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}else{
|
}else{
|
||||||
// Return the token to the browser.
|
// 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);
|
socket.emit('token', response);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -51,19 +52,19 @@ io.on('connection', function(socket){
|
|||||||
|
|
||||||
// Relay candidate messages
|
// Relay candidate messages
|
||||||
socket.on('candidate', function(candidate){
|
socket.on('candidate', function(candidate){
|
||||||
console.log('Received candidate. Broadcasting...')
|
console.log('Received candidate. Broadcasting...');
|
||||||
socket.broadcast.emit('candidate', candidate);
|
socket.broadcast.emit('candidate', candidate);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Relay offers
|
// Relay offers
|
||||||
socket.on('offer', function(offer){
|
socket.on('offer', function(offer){
|
||||||
console.log('Received offer. Broadcasting...')
|
console.log('Received offer. Broadcasting...');
|
||||||
socket.broadcast.emit('offer', offer);
|
socket.broadcast.emit('offer', offer);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Relay answers
|
// Relay answers
|
||||||
socket.on('answer', function(answer){
|
socket.on('answer', function(answer){
|
||||||
console.log('Received answer. Broadcasting...')
|
console.log('Received answer. Broadcasting...');
|
||||||
socket.broadcast.emit('answer', answer);
|
socket.broadcast.emit('answer', answer);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -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) {
|
function logIt(message, error) {
|
||||||
// Print on console
|
// Print on console
|
||||||
console.log(message);
|
console.log(message);
|
||||||
|
|
||||||
// Add to logs on page
|
// Add to logs on page
|
||||||
let logs = document.getElementById('logs');
|
// let logs = document.getElementById('logs');
|
||||||
let tmp = document.createElement('P');
|
// let tmp = document.createElement('P');
|
||||||
tmp.innerText = message;
|
// tmp.innerText = message;
|
||||||
if (error) {
|
// if (error) {
|
||||||
tmp.classList.add('error');
|
// tmp.classList.add('error');
|
||||||
}
|
// }
|
||||||
logs.appendChild(tmp);
|
// logs.appendChild(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create an object to save various objects to without polluting the global namespace.
|
// Create an object to save various objects to without polluting the global namespace.
|
||||||
@ -17,6 +28,8 @@ var VideoChat = {
|
|||||||
connected: false,
|
connected: false,
|
||||||
firstInChat : false,
|
firstInChat : false,
|
||||||
localICECandidates: [],
|
localICECandidates: [],
|
||||||
|
// Initialise our connection to the WebSocket.
|
||||||
|
socket: io(),
|
||||||
remoteVideo: document.getElementById('remote-video'),
|
remoteVideo: document.getElementById('remote-video'),
|
||||||
localVideo: document.getElementById('local-video'),
|
localVideo: document.getElementById('local-video'),
|
||||||
// videoButton: document.getElementById('get-video'),
|
// videoButton: document.getElementById('get-video'),
|
||||||
@ -24,8 +37,7 @@ var VideoChat = {
|
|||||||
// callButton: document.getElementById('call'),
|
// callButton: document.getElementById('call'),
|
||||||
|
|
||||||
|
|
||||||
// Initialise our connection to the WebSocket.
|
|
||||||
socket: io(),
|
|
||||||
|
|
||||||
// Call to getUserMedia (provided by adapter.js for cross browser compatibility)
|
// 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
|
// asking for access to both the video and audio streams. If the request is
|
||||||
|
@ -54,9 +54,9 @@
|
|||||||
<video id="local-video" height="150" autoplay muted></video>
|
<video id="local-video" height="150" autoplay muted></video>
|
||||||
|
|
||||||
|
|
||||||
<p>Log messages:</p>
|
<!--<p>Log messages:</p>-->
|
||||||
<div id="logs">
|
<!--<div id="logs">-->
|
||||||
</div>
|
<!--</div>-->
|
||||||
|
|
||||||
<script src="/socket.io/socket.io.js"></script>
|
<script src="/socket.io/socket.io.js"></script>
|
||||||
<script src="/app.js"></script>
|
<script src="/app.js"></script>
|
||||||
|
Loading…
Reference in New Issue
Block a user