@@ -15,6 +15,7 @@ function jsFlowTracker (client, announceUrl, opts) {
1515 EventEmitter . call ( self )
1616
1717 self . client = client
18+ self . storeIce = [ ]
1819 self . _opts = opts
1920 self . _announceUrl = announceUrl
2021 self . _peers = { } // peers (peer id -> peer)
@@ -122,15 +123,18 @@ jsFlowTracker.prototype._onData = function (payload, from) {
122123 console . log ( 'GOT SIGNAL, expecting offer/ice' , signalObject ) ;
123124 if ( signalObject . candidate ) { // The signalling is for ICE
124125 var params = {
125- info_hash : self . client . _infoHash . toString ( 'base64' ) ,
126- peer_id : self . _peerID ,
127- candidate : signalObject . candidate
128- }
126+ info_hash : self . client . _infoHash . toString ( 'base64' ) ,
127+ peer_id : self . _peerID ,
128+ candidate : signalObject . candidate
129+ }
129130
130- jsFlow . messageUser ( peerId , params , 'webrtc_ice' ) ;
131+ jsFlow . messageUser ( peerId , params , 'webrtc_ice' ) ;
131132 }
132133 else { // The signalling is for an OFFER
133134 offer = signalObject ;
135+ if ( peer . readyForIce ) {
136+ processStoredCandidates ( self )
137+ }
134138 var params = {
135139 info_hash : self . client . _infoHash . toString ( 'base64' ) ,
136140 peer_id : self . _peerID ,
@@ -159,7 +163,7 @@ jsFlowTracker.prototype._onWebRtcOffer = function (payload, from) {
159163 } )
160164
161165 peer . on ( 'signal' , function ( signalObject ) {
162- console . log ( 'GOT SIGNAL, expecting answer/ice' , signalObject ) ;
166+ console . log ( 'GOT SIGNAL, expecting answer/ice' , signalObject ) ;
163167 if ( signalObject . candidate ) { // The signalling is for ICE
164168 var params = {
165169 info_hash : self . client . _infoHash . toString ( 'base64' ) ,
@@ -205,25 +209,40 @@ jsFlowTracker.prototype._onWebRtcAnswer = function (payload, from) {
205209
206210jsFlowTracker . prototype . _onWebRtcIce = function ( payload , from ) {
207211 var self = this
208- //TODO: Buffer ICE ?
212+
213+ if ( from . readyForIce ) {
209214
210- console . log ( 'WILL ADD ICE CANDIDATE!!!' ) ;
215+ console . log ( 'WILL ADD ICE CANDIDATE!!!' ) ;
211216
212- var peer = self . _peers [ from ]
213- if ( peer ) {
214- if ( payload . candidate ) {
215- peer . signal ( payload )
216- }
217+ var peer = self . _peers [ from ]
218+ if ( peer ) {
219+ if ( payload . candidate ) {
220+ peer . signal ( payload )
221+ }
217222 else {
218223 console . error ( 'Candidate did not exist as expected in payload' , payload ) ;
219224 }
220225 }
221226 else {
222227 console . error ( 'Candidate refers to a peer not yet created. TODO: Buffer ICE!' , payload ) ;
228+ self . storeIce . push ( payload . candidate )
223229 }
224230}
225231
226232jsFlowTracker . prototype . _sendAnnounce = function ( params ) {
227233 var self = this
228234 jsFlow . messageUser ( params . trackerid , params , 'announce' )
229235}
236+
237+ function processStoredCandidates ( tracker ) {
238+ console . log ( "Processing stored candidates." )
239+ var peer = tracker . _peers [ from ]
240+ if ( peer ) {
241+ if ( payload . candidate ) {
242+ peer . signal ( payload )
243+ }
244+ else {
245+ console . error ( 'Candidate did not exist as expected in payload' , payload ) ;
246+ }
247+ }
248+ }
0 commit comments