diff --git a/public/app.js b/public/app.js index c163597..9897296 100644 --- a/public/app.js +++ b/public/app.js @@ -30,6 +30,7 @@ async function createRoom() { document.querySelector('#createBtn').disabled = true; document.querySelector('#joinBtn').disabled = true; const db = firebase.firestore(); + const roomRef = await db.collection('rooms').doc(); console.log('Create PeerConnection with configuration: ', configuration); peerConnection = new RTCPeerConnection(configuration); @@ -40,6 +41,19 @@ async function createRoom() { peerConnection.addTrack(track, localStream); }); + // Code for collecting ICE candidates below + const callerCandidatesCollection = roomRef.collection('callerCandidates'); + + peerConnection.addEventListener('icecandidate', event => { + if (!event.candidate) { + console.log('Got final candidate!'); + return; + } + console.log('Got candidate: ', event.candidate); + callerCandidatesCollection.add(event.candidate.toJSON()); + }); + // Code for collecting ICE candidates above + // Code for creating a room below const offer = await peerConnection.createOffer(); await peerConnection.setLocalDescription(offer); @@ -51,26 +65,13 @@ async function createRoom() { sdp: offer.sdp, }, }; - const roomRef = await db.collection('rooms').add(roomWithOffer); + await roomRef.set(roomWithOffer); roomId = roomRef.id; console.log(`New room created with SDP offer. Room ID: ${roomRef.id}`); document.querySelector( '#currentRoom').innerText = `Current room is ${roomRef.id} - You are the caller!`; // Code for creating a room above - // Code for collecting ICE candidates below - const callerCandidatesCollection = roomRef.collection('callerCandidates'); - - peerConnection.addEventListener('icecandidate', event => { - if (!event.candidate) { - console.log('Got final candidate!'); - return; - } - console.log('Got candidate: ', event.candidate); - callerCandidatesCollection.add(event.candidate.toJSON()); - }); - // Code for collecting ICE candidates above - peerConnection.addEventListener('track', event => { console.log('Got remote track:', event.streams[0]); event.streams[0].getTracks().forEach(track => {