Pārlūkot izejas kodu

Add function to list all Peers.

Michelle Bu 11 gadi atpakaļ
vecāks
revīzija
a16106dbba
3 mainītis faili ar 94 papildinājumiem un 0 dzēšanām
  1. 47 0
      dist/peer.js
  2. 0 0
      dist/peer.min.js
  3. 47 0
      lib/peer.js

+ 47 - 0
dist/peer.js

@@ -1763,6 +1763,53 @@ Peer.prototype.disconnect = function() {
   });
 }
 
+/**
+ * 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
+ * the cloud server, email team@peerjs.com to get the functionality enabled for
+ * your key.
+ */
+Peer.prototype.listAllPeers = function(cb) {
+  cb = cb || function() {};
+  var self = this;
+  var http = new XMLHttpRequest();
+  var protocol = this.options.secure ? 'https://' : 'http://';
+  var url = protocol + this.options.host + ':' + this.options.port
+    + this.options.path + this.options.key + '/peers';
+  var queryString = '?ts=' + new Date().getTime() + '' + Math.random();
+  url += queryString;
+
+  // 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) {
+    self._abort('server-error', 'Could not get peers from the server.');
+    cb([]);
+  }
+  http.onreadystatechange = function() {
+    if (http.readyState !== 4) {
+      return;
+    }
+    if (http.status === 401) {
+      var helpfulError = '';
+      if (self.options.host !== util.CLOUD_HOST) {
+        helpfulError = 'It looks like you\'re using the cloud server. You can email '
+          + 'team@peerjs.com to enable peer listing for your API key.';
+      } else {
+        helpfulError = 'You need to enable `allow_discovery` on your self-hosted'
+          + ' PeerServer to use this feature.';
+      }
+      self._abort('permissions-error', 'It doesn\'t look like you have permission to list peers IDs.');
+      cb([]);
+      return;
+    } else if (http.status !== 200) {
+      cb([]);
+    } else {
+      cb(JSON.parse(http.responseText));
+    }
+  };
+  http.send(null);
+}
+
 exports.Peer = Peer;
 /**
  * Wraps a DataChannel between two Peers.

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


+ 47 - 0
lib/peer.js

@@ -395,4 +395,51 @@ Peer.prototype.disconnect = function() {
   });
 }
 
+/**
+ * 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
+ * the cloud server, email team@peerjs.com to get the functionality enabled for
+ * your key.
+ */
+Peer.prototype.listAllPeers = function(cb) {
+  cb = cb || function() {};
+  var self = this;
+  var http = new XMLHttpRequest();
+  var protocol = this.options.secure ? 'https://' : 'http://';
+  var url = protocol + this.options.host + ':' + this.options.port
+    + this.options.path + this.options.key + '/peers';
+  var queryString = '?ts=' + new Date().getTime() + '' + Math.random();
+  url += queryString;
+
+  // 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) {
+    self._abort('server-error', 'Could not get peers from the server.');
+    cb([]);
+  }
+  http.onreadystatechange = function() {
+    if (http.readyState !== 4) {
+      return;
+    }
+    if (http.status === 401) {
+      var helpfulError = '';
+      if (self.options.host !== util.CLOUD_HOST) {
+        helpfulError = 'It looks like you\'re using the cloud server. You can email '
+          + 'team@peerjs.com to enable peer listing for your API key.';
+      } else {
+        helpfulError = 'You need to enable `allow_discovery` on your self-hosted'
+          + ' PeerServer to use this feature.';
+      }
+      self._abort('permissions-error', 'It doesn\'t look like you have permission to list peers IDs.');
+      cb([]);
+      return;
+    } else if (http.status !== 200) {
+      cb([]);
+    } else {
+      cb(JSON.parse(http.responseText));
+    }
+  };
+  http.send(null);
+}
+
 exports.Peer = Peer;

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