浏览代码

api key support added

Michelle Bu 12 年之前
父节点
当前提交
8d319a83f0
共有 3 个文件被更改,包括 26 次插入6 次删除
  1. 13 3
      dist/peer.js
  2. 0 0
      dist/peer.min.js
  3. 13 3
      lib/peer.js

+ 13 - 3
dist/peer.js

@@ -882,8 +882,13 @@ Peer.prototype._checkIn = function() {
   if (!this._id) {
     try {
       var http = new XMLHttpRequest();
+      var url = this._httpUrl + '/id';
+      console.log(this._apikey);
+      if (!!this._apikey)
+        url += '?key=' + this._apikey;
+
       // If there's no ID we need to wait for one before trying to init socket.
-      http.open('get', this._httpUrl + '/id', true);
+      http.open('get', url, true);
       http.onreadystatechange = function() {
         if (!self._id && http.readyState > 2 && !!http.responseText) {
           try {
@@ -922,7 +927,7 @@ Peer.prototype._startXhrStream = function() {
     http.onreadystatechange = function() {
       self._handleStream(http);
     };
-    http.send(JSON.stringify({ id: this._id }));
+    http.send(JSON.stringify({ id: this._id, key: this._apikey }));
   } catch(e) {
     util.log('XMLHttpRequest not available; defaulting to WebSockets');
   }
@@ -960,8 +965,13 @@ Peer.prototype._socketInit = function() {
     return;
 
   var wsurl = 'ws://' + this._server + '/ws';
-  if (!!this._id)
+  if (!!this._id) {
     wsurl += '?id=' + this._id;
+    if (!!this._apikey)
+      wsurl += '&key=' + this._apikey;
+  } else if (!!this._apikey) {
+    wsurl += '?key=' + this._apikey;
+  }
   this._socket = new WebSocket(wsurl);
 
   var self = this;

文件差异内容过多而无法显示
+ 0 - 0
dist/peer.min.js


+ 13 - 3
lib/peer.js

@@ -43,8 +43,13 @@ Peer.prototype._checkIn = function() {
   if (!this._id) {
     try {
       var http = new XMLHttpRequest();
+      var url = this._httpUrl + '/id';
+      console.log(this._apikey);
+      if (!!this._apikey)
+        url += '?key=' + this._apikey;
+
       // If there's no ID we need to wait for one before trying to init socket.
-      http.open('get', this._httpUrl + '/id', true);
+      http.open('get', url, true);
       http.onreadystatechange = function() {
         if (!self._id && http.readyState > 2 && !!http.responseText) {
           try {
@@ -83,7 +88,7 @@ Peer.prototype._startXhrStream = function() {
     http.onreadystatechange = function() {
       self._handleStream(http);
     };
-    http.send(JSON.stringify({ id: this._id }));
+    http.send(JSON.stringify({ id: this._id, key: this._apikey }));
   } catch(e) {
     util.log('XMLHttpRequest not available; defaulting to WebSockets');
   }
@@ -121,8 +126,13 @@ Peer.prototype._socketInit = function() {
     return;
 
   var wsurl = 'ws://' + this._server + '/ws';
-  if (!!this._id)
+  if (!!this._id) {
     wsurl += '?id=' + this._id;
+    if (!!this._apikey)
+      wsurl += '&key=' + this._apikey;
+  } else if (!!this._apikey) {
+    wsurl += '?key=' + this._apikey;
+  }
   this._socket = new WebSocket(wsurl);
 
   var self = this;

部分文件因为文件数量过多而无法显示