ericz 12 年之前
父节点
当前提交
bca26d001d
共有 1 个文件被更改,包括 19 次插入12 次删除
  1. 19 12
      lib/socket.js

+ 19 - 12
lib/socket.js

@@ -23,8 +23,9 @@ Socket.prototype._checkIn = function() {
       var http = new XMLHttpRequest();
       var http = new XMLHttpRequest();
       var url = this._httpUrl + '/id';
       var url = this._httpUrl + '/id';
       // Set API key if necessary.
       // Set API key if necessary.
-      if (!!this._key)
+      if (!!this._key) {
         url += '/' + this._key;
         url += '/' + this._key;
+      }
 
 
       // If there's no ID we need to wait for one before trying to init socket.
       // If there's no ID we need to wait for one before trying to init socket.
       http.open('get', url, true);
       http.open('get', url, true);
@@ -57,14 +58,16 @@ Socket.prototype._checkIn = function() {
 
 
 /** Start up websocket communications. */
 /** Start up websocket communications. */
 Socket.prototype._startWebSocket = function() {
 Socket.prototype._startWebSocket = function() {
-  if (!!this._socket)
+  if (!!this._socket) {
     return;
     return;
+  }
 
 
   var wsurl = 'ws://' + this._server + '/ws';
   var wsurl = 'ws://' + this._server + '/ws';
   if (!!this._id) {
   if (!!this._id) {
     wsurl += '?id=' + this._id;
     wsurl += '?id=' + this._id;
-    if (!!this._key)
+    if (!!this._key) {
       wsurl += '&key=' + this._key;
       wsurl += '&key=' + this._key;
+    }
   } else if (!!this._key) {
   } else if (!!this._key) {
     wsurl += '?key=' + this._key;
     wsurl += '?key=' + this._key;
   }
   }
@@ -98,9 +101,9 @@ Socket.prototype._startXhrStream = function() {
     var http = new XMLHttpRequest();
     var http = new XMLHttpRequest();
     var url = this._httpUrl + '/id';
     var url = this._httpUrl + '/id';
     // Set API key if necessary.
     // Set API key if necessary.
-    if (!!this._key)
+    if (!!this._key) {
       url += '/' + this._key;
       url += '/' + this._key;
-
+    }
     http.open('post', url, true);
     http.open('post', url, true);
     http.setRequestHeader('Content-Type', 'application/json');
     http.setRequestHeader('Content-Type', 'application/json');
     http.onreadystatechange = function() {
     http.onreadystatechange = function() {
@@ -122,12 +125,14 @@ Socket.prototype._handleStream = function(http, pad) {
     return;
     return;
   }
   }
 
 
-  if (this._index === undefined)
+  if (this._index === undefined) {
     this._index = pad ? 2 : 1;
     this._index = pad ? 2 : 1;
-
-  if (http.responseText === null)
+  }
+  
+  if (http.responseText === null) {
     return;
     return;
-
+  }
+  
   var message = http.responseText.split('\n')[this._index];
   var message = http.responseText.split('\n')[this._index];
   if (!!message && http.readyState == 3) {
   if (!!message && http.readyState == 3) {
     this._index += 1;
     this._index += 1;
@@ -147,8 +152,9 @@ Socket.prototype._handleHTTPErrors = function(message) {
     // XHR stream closed by timeout.
     // XHR stream closed by timeout.
     case 'HTTP-END':
     case 'HTTP-END':
       util.log('XHR stream timed out.');
       util.log('XHR stream timed out.');
-      if (!!this._socket && this._socket.readyState != 1)
+      if (!!this._socket && this._socket.readyState != 1) {
         this._startXhrStream();
         this._startXhrStream();
+      }
       break;
       break;
     // XHR stream closed by socket connect.
     // XHR stream closed by socket connect.
     case 'HTTP-SOCKET':
     case 'HTTP-SOCKET':
@@ -179,9 +185,10 @@ Socket.prototype.send = function(data) {
     var http = new XMLHttpRequest();
     var http = new XMLHttpRequest();
     var url = this._httpUrl + '/' + type.toLowerCase();
     var url = this._httpUrl + '/' + type.toLowerCase();
     // Set API key if necessary.
     // Set API key if necessary.
-    if (!!this._key)
+    if (!!this._key) {
       url += '/' + this._key;
       url += '/' + this._key;
-
+    }
+      
     http.open('post', url, true);
     http.open('post', url, true);
     http.setRequestHeader('Content-Type', 'application/json');
     http.setRequestHeader('Content-Type', 'application/json');
     http.onload = function() {
     http.onload = function() {