Browse Source

Merge pull request #38 from MatthiasLohr/master

new method Peer.isConnected() and Peer.getId()
Michelle Bu 12 năm trước cách đây
mục cha
commit
c0b8e0ab56
1 tập tin đã thay đổi với 18 bổ sung0 xóa
  1. 18 0
      lib/peer.js

+ 18 - 0
lib/peer.js

@@ -64,6 +64,7 @@ function Peer(id, options) {
     this.id = id;
     this._init();
   } else {
+    this.id = null;
     this._getId();
   }
 };
@@ -263,6 +264,14 @@ Peer.prototype.connect = function(peer, options) {
   return connectionInfo[1];
 };
 
+/**
+ * Return the peer id or null, if there's no id at the moment.
+ * Reasons for no id could be 'connect in progress' or 'disconnected'
+ */
+Peer.prototype.getId = function() {
+  return this.id;
+};
+
 /**
  * Destroys the Peer: closes all active connections as well as the connection
  *  to the server.
@@ -285,9 +294,18 @@ Peer.prototype.destroy = function() {
 Peer.prototype.disconnect = function() {
   if (!this.disconnected) {
     this._socket.close();
+    this.id = null;
     this.disconnected = true;
   }
 };
 
+/**
+ * Provides a clean method for checking if there's an active connection to the
+ * peer server.
+ */
+Peer.prototype.isConnected = function() {
+  return (!this.disconnected);
+}
+
 
 exports.Peer = Peer;