Permalink
Browse files

Merge pull request #38 from MatthiasLohr/master

new method Peer.isConnected() and Peer.getId()
  • Loading branch information...
michelle committed Apr 3, 2013
2 parents f8ce0c1 + 178e7b7 commit c0b8e0ab5616531150d8eb99310ed8ed33e4054b
Showing with 18 additions and 0 deletions.
  1. +18 −0 lib/peer.js
View
@@ -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;

0 comments on commit c0b8e0a

Please sign in to comment.