Bladeren bron

Do not abort if already disconnected. Emit events consistently.

Michelle Bu 11 jaren geleden
bovenliggende
commit
0df19e5ca9
3 gewijzigde bestanden met toevoegingen van 39 en 19 verwijderingen
  1. 27 17
      dist/peer.js
  2. 0 0
      dist/peer.min.js
  3. 12 2
      lib/peer.js

+ 27 - 17
dist/peer.js

@@ -1478,7 +1478,7 @@ Peer.prototype._initializeServerConnection = function() {
   this.socket.on('disconnected', function() {
     // If we haven't explicitly disconnected, emit error and disconnect.
     if (!self.disconnected) {
-      self._error('network', 'Lost connection to server.')
+      self.emitError('network', 'Lost connection to server.')
       self.disconnect();
     }
   });
@@ -1490,20 +1490,6 @@ Peer.prototype._initializeServerConnection = function() {
   });
 };
 
-Peer.prototype.reconnect = function() {
-  if (this.disconnected && !this.destroyed) {
-    this._initializeServerConnection();
-    this._initialize(this._lastServerId);
-  } else if (this.destroyed) {
-    throw new Error('This peer cannot reconnect to the server. It has already been destroyed.');
-  } else if (!this.disconnected && !this.open) {
-    // Do nothing. We're still connecting the first time.
-    util.error('In a hurry? We\'re still trying to make the initial connection!');
-  } else {
-    throw new Error('Peer ' + this.id + ' cannot reconnect because it is not disconnected from the server!');
-  }
-};
-
 /** Get a unique ID from the server via XHR. */
 Peer.prototype._retrieveId = function(cb) {
   var self = this;
@@ -1663,7 +1649,8 @@ Peer.prototype.connect = function(peer, options) {
   if (this.disconnected) {
     util.warn('You cannot connect to a new Peer because you called '
         + '.disconnect() on this Peer and ended your connection with the'
-        + ' server. You can create a new Peer to reconnect.');
+        + ' server. You can create a new Peer to reconnect, or call reconnect'
+        + ' on this peer if you believe its ID to still be available.');
     this.emitError('disconnected', 'Cannot connect to new Peer after disconnecting from server.');
     return;
   }
@@ -1724,9 +1711,16 @@ Peer.prototype._delayedAbort = function(type, message) {
   });
 }
 
-/** Destroys the Peer and emits an error message. */
+/**
+ * Destroys the Peer and emits an error message.
+ * The Peer is not destroyed if it's in a disconnected state, in which case
+ * it retains its disconnected state and its existing connections.
+ */
 Peer.prototype._abort = function(type, message) {
   util.error('Aborting!');
+  if (!this.disconnected) {
+    this.destroy();
+  }
   this.emitError(type, message);
 };
 
@@ -1796,6 +1790,22 @@ Peer.prototype.disconnect = function() {
   });
 }
 
+/** Attempts to reconnect with the same ID. */
+Peer.prototype.reconnect = function() {
+  if (this.disconnected && !this.destroyed) {
+    util.log('Attempting reconnection to server with ID ' + this._lastServerId);
+    this._initializeServerConnection();
+    this._initialize(this._lastServerId);
+  } else if (this.destroyed) {
+    throw new Error('This peer cannot reconnect to the server. It has already been destroyed.');
+  } else if (!this.disconnected && !this.open) {
+    // Do nothing. We're still connecting the first time.
+    util.error('In a hurry? We\'re still trying to make the initial connection!');
+  } else {
+    throw new Error('Peer ' + this.id + ' cannot reconnect because it is not disconnected from the server!');
+  }
+};
+
 /**
  * Get a list of available peer IDs. If you're running your own server, you'll
  * want to set allow_discovery: true in the PeerServer options. If you're using

File diff suppressed because it is too large
+ 0 - 0
dist/peer.min.js


+ 12 - 2
lib/peer.js

@@ -110,7 +110,7 @@ Peer.prototype._initializeServerConnection = function() {
   this.socket.on('disconnected', function() {
     // If we haven't explicitly disconnected, emit error and disconnect.
     if (!self.disconnected) {
-      self._error('network', 'Lost connection to server.')
+      self.emitError('network', 'Lost connection to server.')
       self.disconnect();
     }
   });
@@ -343,9 +343,18 @@ Peer.prototype._delayedAbort = function(type, message) {
   });
 }
 
-/** Destroys the Peer and emits an error message. */
+/**
+ * Destroys the Peer and emits an error message.
+ * The Peer is not destroyed if it's in a disconnected state, in which case
+ * it retains its disconnected state and its existing connections.
+ */
 Peer.prototype._abort = function(type, message) {
   util.error('Aborting!');
+  if (!this._lastServerId) {
+    this.destroy();
+  } else {
+    this.disconnect();
+  }
   this.emitError(type, message);
 };
 
@@ -419,6 +428,7 @@ Peer.prototype.disconnect = function() {
 Peer.prototype.reconnect = function() {
   if (this.disconnected && !this.destroyed) {
     util.log('Attempting reconnection to server with ID ' + this._lastServerId);
+    this.disconnected = false;
     this._initializeServerConnection();
     this._initialize(this._lastServerId);
   } else if (this.destroyed) {

Some files were not shown because too many files changed in this diff