|
@@ -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.
|