99'use strict' ;
1010const MAX_CHUNK_SIZE = 262144 ;
1111
12- let localConnection ;
13- let remoteConnection ;
12+ let pc1 ;
13+ let pc2 ;
1414let sendChannel ;
1515let receiveChannel ;
1616let chunkSize ;
@@ -61,28 +61,28 @@ async function createConnection() {
6161 const number = Number . parseInt ( megsToSend . value ) ;
6262 bytesToSend = number * 1024 * 1024 ;
6363
64- localConnection = new RTCPeerConnection ( servers ) ;
64+ pc1 = new RTCPeerConnection ( servers ) ;
6565
6666 // Let's make a data channel!
6767 const dataChannelParams = { ordered : false } ;
6868 if ( orderedCheckbox . checked ) {
6969 dataChannelParams . ordered = true ;
7070 }
71- sendChannel = localConnection . createDataChannel ( 'sendDataChannel' , dataChannelParams ) ;
71+ sendChannel = pc1 . createDataChannel ( 'sendDataChannel' , dataChannelParams ) ;
7272 sendChannel . addEventListener ( 'open' , onSendChannelOpen ) ;
7373 sendChannel . addEventListener ( 'close' , onSendChannelClosed ) ;
7474 console . log ( 'Created send data channel: ' , sendChannel ) ;
7575
76- console . log ( 'Created local peer connection object localConnection : ' , localConnection ) ;
76+ console . log ( 'Created local peer connection object pc1 : ' , pc1 ) ;
7777
78- localConnection . addEventListener ( 'icecandidate' , e => onIceCandidate ( localConnection , e ) ) ;
78+ pc1 . addEventListener ( 'icecandidate' , e => onIceCandidate ( pc1 , e ) ) ;
7979
80- remoteConnection = new RTCPeerConnection ( servers ) ;
81- remoteConnection . addEventListener ( 'icecandidate' , e => onIceCandidate ( remoteConnection , e ) ) ;
82- remoteConnection . addEventListener ( 'datachannel' , receiveChannelCallback ) ;
80+ pc2 = new RTCPeerConnection ( servers ) ;
81+ pc2 . addEventListener ( 'icecandidate' , e => onIceCandidate ( pc2 , e ) ) ;
82+ pc2 . addEventListener ( 'datachannel' , receiveChannelCallback ) ;
8383
8484 try {
85- const localOffer = await localConnection . createOffer ( ) ;
85+ const localOffer = await pc1 . createOffer ( ) ;
8686 await handleLocalDescription ( localOffer ) ;
8787 } catch ( e ) {
8888 console . error ( 'Failed to create session description: ' , e ) ;
@@ -143,32 +143,32 @@ function startSendingData() {
143143}
144144
145145function maybeReset ( ) {
146- if ( localConnection === null && remoteConnection === null ) {
146+ if ( pc1 === null && pc2 === null ) {
147147 sendButton . disabled = false ;
148148 megsToSend . disabled = false ;
149149 }
150150}
151151
152152async function handleLocalDescription ( desc ) {
153- localConnection . setLocalDescription ( desc ) ;
154- console . log ( 'Offer from localConnection :\n' , desc . sdp ) ;
155- remoteConnection . setRemoteDescription ( desc ) ;
153+ pc1 . setLocalDescription ( desc ) ;
154+ console . log ( 'Offer from pc1 :\n' , desc . sdp ) ;
155+ pc2 . setRemoteDescription ( desc ) ;
156156 try {
157- const remoteAnswer = await remoteConnection . createAnswer ( ) ;
157+ const remoteAnswer = await pc2 . createAnswer ( ) ;
158158 handleRemoteAnswer ( remoteAnswer ) ;
159159 } catch ( e ) {
160160 console . error ( 'Error when creating remote answer: ' , e ) ;
161161 }
162162}
163163
164164function handleRemoteAnswer ( desc ) {
165- remoteConnection . setLocalDescription ( desc ) ;
166- console . log ( 'Answer from remoteConnection :\n' , desc . sdp ) ;
167- localConnection . setRemoteDescription ( desc ) ;
165+ pc2 . setLocalDescription ( desc ) ;
166+ console . log ( 'Answer from pc2 :\n' , desc . sdp ) ;
167+ pc1 . setRemoteDescription ( desc ) ;
168168}
169169
170170function getOtherPc ( pc ) {
171- return ( pc === localConnection ) ? remoteConnection : localConnection ;
171+ return ( pc === pc1 ) ? pc2 : pc1 ;
172172}
173173
174174async function onIceCandidate ( pc , event ) {
@@ -209,7 +209,7 @@ function onReceiveMessageCallback(event) {
209209function onSendChannelOpen ( ) {
210210 console . log ( 'Send channel is open' ) ;
211211
212- chunkSize = Math . min ( localConnection . sctp . maxMessageSize , MAX_CHUNK_SIZE ) ;
212+ chunkSize = Math . min ( pc1 . sctp . maxMessageSize , MAX_CHUNK_SIZE ) ;
213213 console . log ( 'Determined chunk size: ' , chunkSize ) ;
214214 dataString = new Array ( chunkSize ) . fill ( 'X' ) . join ( '' ) ;
215215 lowWaterMark = chunkSize ; // A single chunk
@@ -227,8 +227,8 @@ function onSendChannelOpen() {
227227
228228function onSendChannelClosed ( ) {
229229 console . log ( 'Send channel is closed' ) ;
230- localConnection . close ( ) ;
231- localConnection = null ;
230+ pc1 . close ( ) ;
231+ pc1 = null ;
232232 console . log ( 'Closed local peer connection' ) ;
233233 maybeReset ( ) ;
234234 console . log ( 'Average time spent in send() (ms): ' +
@@ -241,8 +241,8 @@ function onSendChannelClosed() {
241241
242242function onReceiveChannelClosed ( ) {
243243 console . log ( 'Receive channel is closed' ) ;
244- remoteConnection . close ( ) ;
245- remoteConnection = null ;
244+ pc2 . close ( ) ;
245+ pc2 = null ;
246246 console . log ( 'Closed remote peer connection' ) ;
247247 maybeReset ( ) ;
248248}
0 commit comments