浏览代码

Fix leave

Michelle Bu 12 年之前
父节点
当前提交
8eb31bb416
共有 6 个文件被更改,包括 18 次插入32 次删除
  1. 9 16
      dist/peer.js
  2. 0 0
      dist/peer.min.js
  3. 1 1
      lib/dataconnection.js
  4. 6 12
      lib/negotiator.js
  5. 1 1
      lib/peer.js
  6. 1 2
      lib/socket.js

+ 9 - 16
dist/peer.js

@@ -1548,7 +1548,7 @@ Peer.prototype._cleanup = function() {
 /** Closes all connections to this peer. */
 /** Closes all connections to this peer. */
 Peer.prototype._cleanupPeer = function(peer) {
 Peer.prototype._cleanupPeer = function(peer) {
   var connections = this.connections[peer];
   var connections = this.connections[peer];
-  for (var j = 0, jj = connections; j < jj; j += 1) {
+  for (var j = 0, jj = connections.length; j < jj; j += 1) {
     connections[j].close();
     connections[j].close();
   }
   }
 }
 }
@@ -1665,7 +1665,7 @@ DataConnection.prototype._cleanup = function() {
     this._dc = null;
     this._dc = null;
   }
   }
   this.open = false;
   this.open = false;
-  // Negotiator will listen for this and take care of the PC if appropriate.
+  Negotiator.cleanup(this);
   this.emit('close');
   this.emit('close');
 }
 }
 
 
@@ -1973,7 +1973,7 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
     switch (pc.iceConnectionState) {
     switch (pc.iceConnectionState) {
       case 'failed':
       case 'failed':
         util.log('iceConnectionState is disconnected, closing connections to ' + peerId);
         util.log('iceConnectionState is disconnected, closing connections to ' + peerId);
-        Negotiator._cleanup();
+        Negotiator.cleanup(connection);
         break;
         break;
       case 'completed':
       case 'completed':
         pc.onicecandidate = util.noop;
         pc.onicecandidate = util.noop;
@@ -2011,17 +2011,18 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
   };
   };
 }
 }
 
 
-Negotiator._cleanup = function(provider, peerId, connectionId) {
+Negotiator.cleanup = function(connection) {
+  connection.close(); // Will fail safely if connection is already closed.
   // TODO: close PeerConnection when all connections are closed.
   // TODO: close PeerConnection when all connections are closed.
-  util.log('Cleanup PeerConnection for ' + peerId);
+  util.log('Cleanup PeerConnection for ' + connection.peer);
   /*if (!!this.pc && (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed')) {
   /*if (!!this.pc && (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed')) {
     this.pc.close();
     this.pc.close();
     this.pc = null;
     this.pc = null;
   }*/
   }*/
 
 
-  provider.socket.send({
+  connection.provider.socket.send({
     type: 'LEAVE',
     type: 'LEAVE',
-    dst: peerId
+    dst: connection.peer
   });
   });
 }
 }
 
 
@@ -2112,7 +2113,6 @@ Negotiator.handleSDP = function(type, connection, sdp) {
           connection.addStream(pc.getRemoteStreams()[0]);
           connection.addStream(pc.getRemoteStreams()[0]);
         });
         });
       }
       }
-      // TODO. also, why setZeroTimeout up there?
       Negotiator._makeAnswer(connection);
       Negotiator._makeAnswer(connection);
     }
     }
   }, function(err) {
   }, function(err) {
@@ -2127,12 +2127,6 @@ Negotiator.handleCandidate = function(connection, candidate) {
   connection.pc.addIceCandidate(candidate);
   connection.pc.addIceCandidate(candidate);
   util.log('Added ICE candidate for:', connection.peer);
   util.log('Added ICE candidate for:', connection.peer);
 }
 }
-
-/** Handle peer leaving. */
-Negotiator.handleLeave = function(connection) {
-  util.log('Peer ' + connection.peer + ' disconnected.');
-  // TODO: clean up PC if this is the last connection on that PC.
-}
 /**
 /**
  * An abstraction on top of WebSockets and XHR streaming to provide fastest
  * An abstraction on top of WebSockets and XHR streaming to provide fastest
  * possible connection for peers.
  * possible connection for peers.
@@ -2277,7 +2271,7 @@ Socket.prototype._setHTTPTimeout = function() {
     var old = self._http;
     var old = self._http;
     if (!self._wsOpen()) {
     if (!self._wsOpen()) {
       self._startXhrStream(old._streamIndex + 1);
       self._startXhrStream(old._streamIndex + 1);
-      self._http.old = old;        
+      self._http.old = old;
     } else {
     } else {
       old.abort();
       old.abort();
     }
     }
@@ -2298,7 +2292,6 @@ Socket.prototype._sendQueuedMessages = function() {
 
 
 /** Exposed send for DC & Peer. */
 /** Exposed send for DC & Peer. */
 Socket.prototype.send = function(data) {
 Socket.prototype.send = function(data) {
-  console.log(data)
   if (this.disconnected) {
   if (this.disconnected) {
     return;
     return;
   }
   }

文件差异内容过多而无法显示
+ 0 - 0
dist/peer.min.js


+ 1 - 1
lib/dataconnection.js

@@ -85,7 +85,7 @@ DataConnection.prototype._cleanup = function() {
     this._dc = null;
     this._dc = null;
   }
   }
   this.open = false;
   this.open = false;
-  // Negotiator will listen for this and take care of the PC if appropriate.
+  Negotiator.cleanup(this);
   this.emit('close');
   this.emit('close');
 }
 }
 
 

+ 6 - 12
lib/negotiator.js

@@ -128,7 +128,7 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
     switch (pc.iceConnectionState) {
     switch (pc.iceConnectionState) {
       case 'failed':
       case 'failed':
         util.log('iceConnectionState is disconnected, closing connections to ' + peerId);
         util.log('iceConnectionState is disconnected, closing connections to ' + peerId);
-        Negotiator._cleanup();
+        Negotiator.cleanup(connection);
         break;
         break;
       case 'completed':
       case 'completed':
         pc.onicecandidate = util.noop;
         pc.onicecandidate = util.noop;
@@ -166,17 +166,18 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
   };
   };
 }
 }
 
 
-Negotiator._cleanup = function(provider, peerId, connectionId) {
+Negotiator.cleanup = function(connection) {
+  connection.close(); // Will fail safely if connection is already closed.
   // TODO: close PeerConnection when all connections are closed.
   // TODO: close PeerConnection when all connections are closed.
-  util.log('Cleanup PeerConnection for ' + peerId);
+  util.log('Cleanup PeerConnection for ' + connection.peer);
   /*if (!!this.pc && (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed')) {
   /*if (!!this.pc && (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed')) {
     this.pc.close();
     this.pc.close();
     this.pc = null;
     this.pc = null;
   }*/
   }*/
 
 
-  provider.socket.send({
+  connection.provider.socket.send({
     type: 'LEAVE',
     type: 'LEAVE',
-    dst: peerId
+    dst: connection.peer
   });
   });
 }
 }
 
 
@@ -267,7 +268,6 @@ Negotiator.handleSDP = function(type, connection, sdp) {
           connection.addStream(pc.getRemoteStreams()[0]);
           connection.addStream(pc.getRemoteStreams()[0]);
         });
         });
       }
       }
-      // TODO. also, why setZeroTimeout up there?
       Negotiator._makeAnswer(connection);
       Negotiator._makeAnswer(connection);
     }
     }
   }, function(err) {
   }, function(err) {
@@ -282,9 +282,3 @@ Negotiator.handleCandidate = function(connection, candidate) {
   connection.pc.addIceCandidate(candidate);
   connection.pc.addIceCandidate(candidate);
   util.log('Added ICE candidate for:', connection.peer);
   util.log('Added ICE candidate for:', connection.peer);
 }
 }
-
-/** Handle peer leaving. */
-Negotiator.handleLeave = function(connection) {
-  util.log('Peer ' + connection.peer + ' disconnected.');
-  // TODO: clean up PC if this is the last connection on that PC.
-}

+ 1 - 1
lib/peer.js

@@ -311,7 +311,7 @@ Peer.prototype._cleanup = function() {
 /** Closes all connections to this peer. */
 /** Closes all connections to this peer. */
 Peer.prototype._cleanupPeer = function(peer) {
 Peer.prototype._cleanupPeer = function(peer) {
   var connections = this.connections[peer];
   var connections = this.connections[peer];
-  for (var j = 0, jj = connections; j < jj; j += 1) {
+  for (var j = 0, jj = connections.length; j < jj; j += 1) {
     connections[j].close();
     connections[j].close();
   }
   }
 }
 }

+ 1 - 2
lib/socket.js

@@ -142,7 +142,7 @@ Socket.prototype._setHTTPTimeout = function() {
     var old = self._http;
     var old = self._http;
     if (!self._wsOpen()) {
     if (!self._wsOpen()) {
       self._startXhrStream(old._streamIndex + 1);
       self._startXhrStream(old._streamIndex + 1);
-      self._http.old = old;        
+      self._http.old = old;
     } else {
     } else {
       old.abort();
       old.abort();
     }
     }
@@ -163,7 +163,6 @@ Socket.prototype._sendQueuedMessages = function() {
 
 
 /** Exposed send for DC & Peer. */
 /** Exposed send for DC & Peer. */
 Socket.prototype.send = function(data) {
 Socket.prototype.send = function(data) {
-  console.log(data)
   if (this.disconnected) {
   if (this.disconnected) {
     return;
     return;
   }
   }

部分文件因为文件数量过多而无法显示