@@ -30,6 +30,7 @@ async function createRoom() {
30
30
document . querySelector ( '#createBtn' ) . disabled = true ;
31
31
document . querySelector ( '#joinBtn' ) . disabled = true ;
32
32
const db = firebase . firestore ( ) ;
33
+ const roomRef = await db . collection ( 'rooms' ) . doc ( ) ;
33
34
34
35
console . log ( 'Create PeerConnection with configuration: ' , configuration ) ;
35
36
peerConnection = new RTCPeerConnection ( configuration ) ;
@@ -40,6 +41,19 @@ async function createRoom() {
40
41
peerConnection . addTrack ( track , localStream ) ;
41
42
} ) ;
42
43
44
+ // Code for collecting ICE candidates below
45
+ const callerCandidatesCollection = roomRef . collection ( 'callerCandidates' ) ;
46
+
47
+ peerConnection . addEventListener ( 'icecandidate' , event => {
48
+ if ( ! event . candidate ) {
49
+ console . log ( 'Got final candidate!' ) ;
50
+ return ;
51
+ }
52
+ console . log ( 'Got candidate: ' , event . candidate ) ;
53
+ callerCandidatesCollection . add ( event . candidate . toJSON ( ) ) ;
54
+ } ) ;
55
+ // Code for collecting ICE candidates above
56
+
43
57
// Code for creating a room below
44
58
const offer = await peerConnection . createOffer ( ) ;
45
59
await peerConnection . setLocalDescription ( offer ) ;
@@ -51,26 +65,13 @@ async function createRoom() {
51
65
sdp : offer . sdp ,
52
66
} ,
53
67
} ;
54
- const roomRef = await db . collection ( 'rooms' ) . add ( roomWithOffer ) ;
68
+ await roomRef . set ( roomWithOffer ) ;
55
69
roomId = roomRef . id ;
56
70
console . log ( `New room created with SDP offer. Room ID: ${ roomRef . id } ` ) ;
57
71
document . querySelector (
58
72
'#currentRoom' ) . innerText = `Current room is ${ roomRef . id } - You are the caller!` ;
59
73
// Code for creating a room above
60
74
61
- // Code for collecting ICE candidates below
62
- const callerCandidatesCollection = roomRef . collection ( 'callerCandidates' ) ;
63
-
64
- peerConnection . addEventListener ( 'icecandidate' , event => {
65
- if ( ! event . candidate ) {
66
- console . log ( 'Got final candidate!' ) ;
67
- return ;
68
- }
69
- console . log ( 'Got candidate: ' , event . candidate ) ;
70
- callerCandidatesCollection . add ( event . candidate . toJSON ( ) ) ;
71
- } ) ;
72
- // Code for collecting ICE candidates above
73
-
74
75
peerConnection . addEventListener ( 'track' , event => {
75
76
console . log ( 'Got remote track:' , event . streams [ 0 ] ) ;
76
77
event . streams [ 0 ] . getTracks ( ) . forEach ( track => {
0 commit comments