[Pkg-javascript-commits] [sockjs-client] 269/434: #36 Add info.js, a chunking-test.js replacement.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:18 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository sockjs-client.

commit 0b8bef7a20d41df438e9a8004e3b10c02fa0c822
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Fri Jan 6 13:16:12 2012 +0000

    #36 Add info.js, a chunking-test.js replacement.
---
 lib/index.js           |  1 +
 lib/info.js            | 39 +++++++++++++++++++++++++++++++++++++++
 lib/sockjs.js          | 45 ++++++++++++++++++++++++---------------------
 lib/trans-websocket.js |  3 ++-
 lib/utils.js           |  8 ++++++++
 5 files changed, 74 insertions(+), 22 deletions(-)

diff --git a/lib/index.js b/lib/index.js
index d8ee327..e5fb574 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -16,6 +16,7 @@ SockJS = (function(){
 <!-- include lib/trans-iframe.js -->
 <!-- include lib/trans-iframe-within.js -->
 <!-- include lib/chunking-test.js -->
+<!-- include lib/info.js -->
 <!-- include lib/trans-iframe-eventsource.js -->
 <!-- include lib/trans-iframe-xhr-polling.js -->
 <!-- include lib/trans-iframe-htmlfile.js -->
diff --git a/lib/info.js b/lib/info.js
new file mode 100644
index 0000000..7aacb59
--- /dev/null
+++ b/lib/info.js
@@ -0,0 +1,39 @@
+var InfoReceiver = function(base_url) {
+    var that = this;
+    that.base_url = base_url;
+    that.t0 = (new Date()).getTime();
+    utils.delay(function(){that.doXhr();});
+};
+
+InfoReceiver.prototype = new REventTarget();
+
+InfoReceiver.prototype.doXhr = function() {
+    var that = this;
+    var done = false;
+    var orsc = function(xhr, e, abort_reason) {
+        if (xhr.readyState === 4 && !done) {
+            if (xhr.status === 200) {
+                var data = xhr.responseText;
+                if (data) {
+                    var rtt = (new Date()).getTime() - that.t0;
+                    var info = JSON.parse(data);
+                    if (typeof info !== 'object') info = {};
+                    done = true;
+                    that.dispatchEvent(new SimpleEvent('message',
+                                                       {info: info,
+                                                        rtt: rtt}));
+                }
+            };
+        }
+        if (abort_reason && !done) {
+            done = true;
+            that.dispatchEvent(new SimpleEvent('message',
+                                               {}));
+        }
+    };
+    var createXhr = _window.XDomainRequest ?
+                           utils.createXDR : utils.createXHR;
+    var xhr_close = createXhr('GET', that.base_url + '/info' , null, orsc);
+};
+
+utils.info = InfoReceiver;
diff --git a/lib/sockjs.js b/lib/sockjs.js
index f203a31..ef02ca3 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -1,6 +1,6 @@
 var SockJS = function(url, protocols, options) {
     var that = this;
-    that._options = {devel: false, debug: false, chunking: undefined};
+    that._options = {devel: false, debug: false, info: undefined};
     if (options) {
         utils.objectExtend(that._options, options);
     }
@@ -20,7 +20,19 @@ var SockJS = function(url, protocols, options) {
     }
     that.protocol = null;
     that.readyState = SockJS.CONNECTING;
-    that._didClose();
+    if (!that._options.info) {
+        var ir = new InfoReceiver(that._base_url);
+        ir.onmessage = function(data) {
+            if (data.info) {
+                that._applyInfo(data);
+                that._didClose();
+            } else {
+                that._didClose(3000, 'Can\'t connect to server');
+            }
+        };
+    } else {
+        that._didClose();
+    }
 };
 // Inheritance
 SockJS.prototype = new REventTarget();
@@ -95,7 +107,8 @@ SockJS.prototype._didClose = function(code, reason) {
                         that._didClose(2007,
                                        "Transport timeouted");
                     }
-                }, 5001);
+                }, that._options.delay || 5000);
+
             return;
         }
         close_event = new SimpleEvent("close", {code: 2000,
@@ -149,21 +162,6 @@ SockJS.prototype._try_next_protocol = function(close_event) {
         if (!protocol) {
             return false;
         }
-        // Some protocols require chunking, we may need to run the
-        // test beforehand.
-        if (SockJS[protocol] &&
-              SockJS[protocol].need_chunking === true &&
-              that._options.chunking === undefined) {
-            that._protocols.unshift(protocol);
-            that.protocol = 'chunking-test';
-            // Assert false, in case test timeouts.
-            that._options.chunking = false;
-            chunkingTest(that._base_url, function(chunking) {
-                             that._options.chunking = chunking;
-                             that._try_next_protocol();
-                         }, that._options);
-            return true;
-        }
         // Some protocols require access to `body`, what if were in
         // the `head`?
         if (SockJS[protocol] &&
@@ -178,14 +176,13 @@ SockJS.prototype._try_next_protocol = function(close_event) {
         }
 
         if (!SockJS[protocol] ||
-              (SockJS[protocol].need_chunking === true &&
-                   that._options.chunking !== true) ||
               !SockJS[protocol].enabled(that._options)) {
             that._debug('Skipping transport:', protocol);
         } else {
             var connid = utils.random_string(8);
             var trans_url = that._base_url + '/' + that._server + '/' + connid;
-            that._debug('Opening transport:', protocol, ' url:', trans_url);
+            that._debug('Opening transport:', protocol, ' url:'+trans_url,
+                        ' delay:'+that._options.delay);
             that._transport = new SockJS[protocol](that, trans_url,
                                                    that._base_url);
             return true;
@@ -215,3 +212,9 @@ SockJS.prototype.send = function(data) {
     }
     return true;
 };
+
+SockJS.prototype._applyInfo = function(data) {
+    var that = this;
+    that._options.info = data.info;
+    that._options.delay = utils.countDelay(data.rtt);
+};
diff --git a/lib/trans-websocket.js b/lib/trans-websocket.js
index 1882332..dc1cb38 100644
--- a/lib/trans-websocket.js
+++ b/lib/trans-websocket.js
@@ -44,5 +44,6 @@ WebSocketTransport.prototype.doCleanup = function() {
 };
 
 WebSocketTransport.enabled = function() {
-    return !!(window.WebSocket || window.MozWebSocket);
+    return !!(window.WebSocket || window.MozWebSocket) &&
+        this._options.info.websocket !== false;
 };
diff --git a/lib/utils.js b/lib/utils.js
index 8b65842..ab9a923 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -49,6 +49,14 @@ utils.userSetCode = function (code) {
     return code === 1000 || (code >= 3000 && code <= 4999);
 };
 
+utils.countDelay = function (rtt) {
+    var rtt5 = 5 * rtt;
+    if (rtt5 < 200) {
+        return 200;
+    }
+    return rtt5;
+}
+
 utils.log = function() {
     if (_window.console && console.log && console.log.apply) {
         console.log.apply(console, arguments);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list