Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from gorisanson/solution
Browse files Browse the repository at this point in the history
Fix solution
  • Loading branch information
ErikHellman authored Mar 16, 2020
2 parents 86d853d + 77ae0b1 commit beb2f9f
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 => {
Expand Down

0 comments on commit beb2f9f

Please sign in to comment.