Browse Source

Close and disconnect edge case cleanup

ericz 11 years ago
parent
commit
c508e85b8f
4 changed files with 13 additions and 24 deletions
  1. 3 13
      lib/dataconnection.js
  2. 1 1
      lib/mediaconnection.js
  3. 7 9
      lib/negotiator.js
  4. 2 1
      lib/peer.js

+ 3 - 13
lib/dataconnection.js

@@ -77,18 +77,6 @@ DataConnection.prototype._configureDataChannel = function() {
   };
 }
 
-DataConnection.prototype._cleanup = function() {
-  // readyState is deprecated but still exists in older versions.
-  if (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed') {
-    this.pc.close();
-    this.open = false;
-    Negotiator.cleanup(this);
-    this.emit('close');
-  } else {
-    this.emit('error', new Error('The connection has already been closed'));
-  }
-}
-
 // Handles a DataChannel message.
 DataConnection.prototype._handleDataMessage = function(e) {
   var self = this;
@@ -124,7 +112,9 @@ DataConnection.prototype.close = function() {
   if (!this.open) {
     return;
   }
-  this._cleanup();
+  this.open = false;
+  Negotiator.cleanup(this);
+  this.emit('close');
 }
 
 /** Allows user to send data. */

+ 1 - 1
lib/mediaconnection.js

@@ -80,7 +80,7 @@ MediaConnection.prototype.close = function() {
   if (!this.open) {
     return;
   }
-  Negotiator.cleanup(this);
   this.open = false;
+  Negotiator.cleanup(this);
   this.emit('close')
 };

+ 7 - 9
lib/negotiator.js

@@ -178,15 +178,13 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
 Negotiator.cleanup = function(connection) {
   connection.close(); // Will fail safely if connection is already closed.
   util.log('Cleanup PeerConnection for ' + connection.peer);
-  /*if (!!this.pc && (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed')) {
-    this.pc.close();
-    this.pc = null;
-  }*/
-
-  connection.provider.socket.send({
-    type: 'LEAVE',
-    dst: connection.peer
-  });
+
+  var pc = connection.pc;
+
+  if (!!pc && (pc.readyState !== 'closed' || pc.signalingState !== 'closed')) {
+    pc.close();
+    connection.pc = null;
+  }
 }
 
 Negotiator._makeOffer = function(connection) {

+ 2 - 1
lib/peer.js

@@ -115,7 +115,7 @@ Peer.prototype._retrieveId = function(cb) {
 
   // If there's no ID we need to wait for one before trying to init socket.
   http.open('get', url, true);
-  http.onerror = function(e) { 
+  http.onerror = function(e) {
     util.error('Error retrieving ID', e);
     self._abort('server-error', 'Could not get an ID from the server');
   }
@@ -341,6 +341,7 @@ Peer.prototype._cleanup = function() {
   for (var i = 0, ii = peers.length; i < ii; i++) {
     this._cleanupPeer(peers[i]);
   }
+
   this.emit('close');
 }