Pārlūkot izejas kodu

peer handles connection close() call

Michelle Bu 12 gadi atpakaļ
vecāks
revīzija
74f0343d9f
5 mainītis faili ar 39 papildinājumiem un 12 dzēšanām
  1. 13 4
      demo/static/peer.js
  2. 13 4
      dist/peer.js
  3. 0 0
      dist/peer.min.js
  4. 1 0
      lib/connection.js
  5. 12 4
      lib/peer.js

+ 13 - 4
demo/static/peer.js

@@ -1025,6 +1025,7 @@ Peer.prototype._handleServerMessage = function(message) {
           self.emit('connection', connection, message.metadata);
         }
       }, options);
+      this._attachConnectionListeners(connection);
       this.connections[peer] = connection;
       break;
     case 'PEER_READY':
@@ -1037,10 +1038,7 @@ Peer.prototype._handleServerMessage = function(message) {
       if (connection) connection.handleCandidate(message);
       break;
     case 'LEAVE':
-      if (connection) {
-        connection.handleLeave();
-        delete this.connections[peer];
-      }
+      if (connection) connection.handleLeave();
       break;
     case 'PORT':
       if (util.browserisms === 'Firefox') {
@@ -1071,6 +1069,15 @@ Peer.prototype._cleanup = function() {
   }
 };
 
+/** Listeners for DataConnection events. */
+Peer.prototype._attachConnectionListeners = function(connection) {
+  var self = this;
+  connection.on('close', function(peer) {
+    if (self.connections[peer]) delete self.connections[peer];
+  });
+};
+
+
 
 /** Exposed connect function for users. Will try to connect later if user
  * is waiting for an ID. */
@@ -1088,6 +1095,7 @@ Peer.prototype.connect = function(peer, metadata, cb) {
     config: this._config
   };
   var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, cb, options);
+  this._attachConnectionListeners(connection);
 
   this.connections[peer] = connection;
 };
@@ -1428,6 +1436,7 @@ DataConnection.prototype.close = function() {
     dst: self._peer,
     src: self._id,
   }));
+  this.emit('close', this._peer);
 };
 
 

+ 13 - 4
dist/peer.js

@@ -1025,6 +1025,7 @@ Peer.prototype._handleServerMessage = function(message) {
           self.emit('connection', connection, message.metadata);
         }
       }, options);
+      this._attachConnectionListeners(connection);
       this.connections[peer] = connection;
       break;
     case 'PEER_READY':
@@ -1037,10 +1038,7 @@ Peer.prototype._handleServerMessage = function(message) {
       if (connection) connection.handleCandidate(message);
       break;
     case 'LEAVE':
-      if (connection) {
-        connection.handleLeave();
-        delete this.connections[peer];
-      }
+      if (connection) connection.handleLeave();
       break;
     case 'PORT':
       if (util.browserisms === 'Firefox') {
@@ -1071,6 +1069,15 @@ Peer.prototype._cleanup = function() {
   }
 };
 
+/** Listeners for DataConnection events. */
+Peer.prototype._attachConnectionListeners = function(connection) {
+  var self = this;
+  connection.on('close', function(peer) {
+    if (self.connections[peer]) delete self.connections[peer];
+  });
+};
+
+
 
 /** Exposed connect function for users. Will try to connect later if user
  * is waiting for an ID. */
@@ -1088,6 +1095,7 @@ Peer.prototype.connect = function(peer, metadata, cb) {
     config: this._config
   };
   var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, cb, options);
+  this._attachConnectionListeners(connection);
 
   this.connections[peer] = connection;
 };
@@ -1428,6 +1436,7 @@ DataConnection.prototype.close = function() {
     dst: self._peer,
     src: self._id,
   }));
+  this.emit('close', this._peer);
 };
 
 

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/peer.min.js


+ 1 - 0
lib/connection.js

@@ -327,6 +327,7 @@ DataConnection.prototype.close = function() {
     dst: self._peer,
     src: self._id,
   }));
+  this.emit('close', this._peer);
 };
 
 

+ 12 - 4
lib/peer.js

@@ -186,6 +186,7 @@ Peer.prototype._handleServerMessage = function(message) {
           self.emit('connection', connection, message.metadata);
         }
       }, options);
+      this._attachConnectionListeners(connection);
       this.connections[peer] = connection;
       break;
     case 'PEER_READY':
@@ -198,10 +199,7 @@ Peer.prototype._handleServerMessage = function(message) {
       if (connection) connection.handleCandidate(message);
       break;
     case 'LEAVE':
-      if (connection) {
-        connection.handleLeave();
-        delete this.connections[peer];
-      }
+      if (connection) connection.handleLeave();
       break;
     case 'PORT':
       if (util.browserisms === 'Firefox') {
@@ -232,6 +230,15 @@ Peer.prototype._cleanup = function() {
   }
 };
 
+/** Listeners for DataConnection events. */
+Peer.prototype._attachConnectionListeners = function(connection) {
+  var self = this;
+  connection.on('close', function(peer) {
+    if (self.connections[peer]) delete self.connections[peer];
+  });
+};
+
+
 
 /** Exposed connect function for users. Will try to connect later if user
  * is waiting for an ID. */
@@ -249,6 +256,7 @@ Peer.prototype.connect = function(peer, metadata, cb) {
     config: this._config
   };
   var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, cb, options);
+  this._attachConnectionListeners(connection);
 
   this.connections[peer] = connection;
 };

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels