[Pkg-javascript-commits] [sockjs-client] 48/434: Treat xhr-streaming as a specialized part of xhr-polling.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:00 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 8d5cdcc70a706f8dd9eec5764caff7c32a66d9da
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Wed Aug 3 14:54:46 2011 +0100

    Treat xhr-streaming as a specialized part of xhr-polling.
---
 lib/sockjs.js            |  6 +++++-
 lib/trans-xhr-polling.js | 46 ++++++++++++++++++++++++++++++++--------------
 lib/utils.js             |  2 +-
 3 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/lib/sockjs.js b/lib/sockjs.js
index c26d9d5..a55aaf5 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -104,22 +104,26 @@ SockJS.prototype._didClose = function(status, reason) {
 SockJS.prototype._didMessage = function(data) {
     var that = this;
     var type = data.slice(0, 1);
-    var payload = JSON.parse(data.slice(1) || 'null');
     switch(type) {
     case 'o':
         that._dispatchOpen();
         break;
     case 'a':
+        var payload = JSON.parse(data.slice(1) || '[]');
         for(var i=0; i < payload.length; i++){
             that._dispatchMessage(payload[i]);
         }
         break;
     case 'm':
+        var payload = JSON.parse(data.slice(1) || 'null');
         that._dispatchMessage(payload);
         break;
     case 'c':
+        var payload = JSON.parse(data.slice(1) || '[]');
         that._didClose(payload[0], payload[1]);
         break;
+    case 'h':// heartbeat, ignore
+        break;
     }
 };
 
diff --git a/lib/trans-xhr-polling.js b/lib/trans-xhr-polling.js
index 3b52207..000c54a 100644
--- a/lib/trans-xhr-polling.js
+++ b/lib/trans-xhr-polling.js
@@ -1,6 +1,6 @@
 // Requires CORS-enabled browser.
 
-var XhrTransport = SockJS['xhr-polling'] = function(ri, trans_url){
+var XhrTransport = SockJS['xhr-polling'] = function(ri, trans_url) {
     var that = this;
     that.ri = ri;
     that.trans_url = trans_url;
@@ -12,26 +12,29 @@ XhrTransport.prototype = new BufferedSender();
 
 XhrTransport.prototype._schedule_recv = function() {
     var that = this;
-    var callback = function (xhr, abort_reason) {
+    var message_callback = function (xhr, messages) {
+        if (xhr.status === 200) {
+            for(var i=0; i<messages.length; i++)
+                that.ri._didMessage(messages[i]);
+            if (messages.length === 1 && messages[0] === 'o') {
+                that._streaming = true;
+                utils.log("Upgrading to streaming");
+            }
+        }
+    };
+    var end_callback = function (xhr, abort_reason) {
         that._recv_stop = null;
         if (abort_reason) return;
         if (xhr.status === 200) {
-            var data = xhr.responseText;
-            if (data) {
-                // no data - heartbeat;
-                if (!that._is_closing) {
-                    that.ri._didMessage(data);
-                }
-            }
-            // The message can be a close message, and change is_closing state.
             if (!that._is_closing) {
                 that._schedule_recv();
             }
         } else {
-            that.ri._didClose(1006, "XHR error");
+            that.ri._didClose(1006, "XHR error (" + xhr.status + ")");
         }
     };
-    that._recv_stop = xhrPoll(that.trans_url + '/xhr', callback);
+    var postfix = that._streaming ? '/xhr_streaming' : '/xhr';
+    that._recv_stop = xhrPoll(that.trans_url + postfix, message_callback, end_callback);
 };
 
 XhrTransport.prototype.doCleanup = function() {
@@ -44,10 +47,24 @@ XhrTransport.prototype.doCleanup = function() {
     that.send_destructor();
 };
 
-var xhrPoll = function(url, user_callback) {
+var xhrPoll = function(url, message_callback, end_callback) {
+    var buf_pos = 0;
     var orsc = function (xhr, e, abort_reason) {
+        if ((xhr.readyState === 3 || xhr.readyState === 4) && xhr.responseText) {
+            // utils.log('responseText=', escape(xhr.responseText), 'buf_pos=',buf_pos);
+            var msgs = [];
+            while (1) {
+                var buf = xhr.responseText.slice(buf_pos);
+                var p = buf.indexOf('\x00');
+                if (p === -1) break;
+                buf_pos += p+1;
+                msgs.push( buf.slice(0, p) );
+            }
+            if (msgs.length)
+                message_callback(xhr, msgs);
+        }
         if (xhr.readyState === 4 || abort_reason) {
-            user_callback(xhr, abort_reason);
+            end_callback(xhr, abort_reason);
         }
     };
     // Using POST can save us from caching issues.
@@ -55,6 +72,7 @@ var xhrPoll = function(url, user_callback) {
     return fun('POST', url, null, orsc);
 };
 
+
 // According to:
 //   http://stackoverflow.com/questions/1641507/detect-browser-support-for-cross-domain-xmlhttprequests
 //   http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
diff --git a/lib/utils.js b/lib/utils.js
index 9d6b4ba..69c009d 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -173,7 +173,7 @@ utils.createXHR = function(method, url, payload, callback) {
     xhr.onreadystatechange = function (e) {
         if (xhr && callback) {
             callback(xhr, e);
-            if (xhr.readyState === 4) {
+            if (xhr && xhr.readyState === 4) {
                 cleanup();
             }
         }

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