[Pkg-javascript-commits] [sockjs-client] 99/434: New generic infrastructure to handle polling transports. Htmlfile transport refactored.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:06 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 c465aa88e6809576acc33a8d3fceef3ce2f4e1c9
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Thu Aug 25 14:57:14 2011 +0100

    New generic infrastructure to handle polling transports. Htmlfile transport refactored.
---
 lib/index.js                      |  4 +++
 lib/trans-iframe-htmlfile.js      | 37 +++----------------------
 lib/trans-polling.js              | 36 +++++++++++++++++++++++++
 lib/trans-receiver-eventsource.js | 29 ++++++++++++++++++++
 lib/trans-receiver-htmlfile.js    | 57 +++++++++++++++++++++++++++++++++++++++
 lib/trans-receiver-xhr.js         | 36 +++++++++++++++++++++++++
 6 files changed, 166 insertions(+), 33 deletions(-)

diff --git a/lib/index.js b/lib/index.js
index 6d378f1..d61dfae 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -16,6 +16,10 @@ SockJS = (function(){
 <!-- include lib/trans-iframe-eventsource.js -->
 <!-- include lib/trans-iframe-xhr-polling.js -->
 <!-- include lib/trans-iframe-htmlfile.js -->
+<!-- include lib/trans-polling.js -->
+<!-- include lib/trans-receiver-eventsource.js -->
+<!-- include lib/trans-receiver-htmlfile.js -->
+<!-- include lib/trans-receiver-xhr.js -->
                   return SockJS;
           })();
 if ('_sockjs_onload' in window) setTimeout(_sockjs_onload, 1);
diff --git a/lib/trans-iframe-htmlfile.js b/lib/trans-iframe-htmlfile.js
index 5b7c025..225b410 100644
--- a/lib/trans-iframe-htmlfile.js
+++ b/lib/trans-iframe-htmlfile.js
@@ -12,15 +12,6 @@ var HtmlFileIframeTransport = SockJS['iframe-htmlfile'] = function () {
 // Inheritance.
 HtmlFileIframeTransport.prototype = new IframeTransport();
 
-var isIeHtmlfileCapable = function() {
-    if ('ActiveXObject' in window) {
-        try {
-            return !!new ActiveXObject('htmlfile');
-        } catch (x) {}
-    }
-    return false;
-};
-
 HtmlFileIframeTransport.enabled = function (options) {
     // Development or IE  _and_  iframe postWindow working.
     var ie = isIeHtmlfileCapable();
@@ -32,28 +23,9 @@ var HtmlFileTransport = FacadeJS['w-iframe-htmlfile'] = function (ri, trans_url)
     utils.polluteGlobalNamespace();
 
     var that = this;
-    that.ri = ri;
     that.trans_url = trans_url;
     that.send_constructor(ajaxSender);
-    that.ie = isIeHtmlfileCapable();
-
-    that.id = 'a' + utils.random_string(6, 26);
-    var iframe_url = trans_url + '/htmlfile?c=' + escape(WPrefix + '.' + that.id);
-    that.is_loaded = false;
-    var callback = function(data) {
-        if (!that.is_loaded) {
-            that.is_loaded = true;
-            that.iframeObj.loaded();
-        }
-        that.ri._didMessage(data);
-    };
-    _window[WPrefix][that.id] = callback;
-
-    var constructor = that.ie ? utils.createHtmlfile : utils.createIframe;
-    that.iframeObj = constructor(iframe_url, function(e) {
-                                     that.doCleanup();
-                                     that.ri._didClose(1001, "Can't load htmlfile iframe (" + e + ")");
-                                 });
+    that.poll = new Polling(ri, HtmlfileReceiver, trans_url + '/htmlfile');
 };
 
 // Inheritnace
@@ -61,9 +33,8 @@ HtmlFileTransport.prototype = new BufferedSender();
 
 HtmlFileTransport.prototype.doCleanup = function() {
     var that = this;
-    if (that.iframeObj) {
-        that.iframeObj.cleanup();
-        delete _window[WPrefix][that.id];
+    if (that.poll) {
+        that.poll.abort();
+        that.poll = null;
     }
-    that.send_destructor();
 };
diff --git a/lib/trans-polling.js b/lib/trans-polling.js
new file mode 100644
index 0000000..a86adfc
--- /dev/null
+++ b/lib/trans-polling.js
@@ -0,0 +1,36 @@
+
+var Polling = function(ri, Receiver, recv_url) {
+    var that = this;
+    that.ri = ri;
+    that.Receiver = Receiver;
+    that.recv_url = recv_url;
+    that._scheduleRecv();
+};
+
+Polling.prototype._scheduleRecv = function() {
+    var that = this;
+    var poll = that.poll = new that.Receiver(that.recv_url);
+    var msg_counter = 0;
+    poll.onmessage = function(e) {
+        msg_counter += 1;
+        that.ri._didMessage(e.data);
+    };
+    poll.onclose = function(e) {
+        that.poll = poll = poll.onmessage = poll.onclose = null;
+        if (!that.poll_is_closing) {
+            if (e.reason === 'permanent') {
+                that.ri._didClose(1006, 'Polling error (' + e.reason + ')');
+            } else {
+                that._scheduleRecv();
+            }
+        }
+    };
+};
+
+Polling.prototype.abort = function() {
+    var that = this;
+    that.poll_is_closing = true;
+    if (that.poll) {
+        that.poll.abort();
+    }
+};
diff --git a/lib/trans-receiver-eventsource.js b/lib/trans-receiver-eventsource.js
new file mode 100644
index 0000000..16e4087
--- /dev/null
+++ b/lib/trans-receiver-eventsource.js
@@ -0,0 +1,29 @@
+
+var EventSourceReceiver = function(url) {
+    var that = this;
+    var es = new EventSource(url);
+    es.onmessage = function(e) {
+        that.dispatchEvent(new SimpleEvent('message',
+                                           {'data': unescape(e.data)}));
+    };
+    that.es_close = es.onerror = function(e, abort_reason) {
+        // ES on reconnection has readyState = OPEN = 1.
+        // on network error it's CLOSED = 2
+        var reason = abort_reason ? 'user' :
+            (es.readyState === 1 ? 'network' : 'permanent');
+        that.es_close = es.onmessage = es.onerror = null;
+        // EventSource reconnects automatically.
+        es.close();
+        es = null;
+        that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
+    };
+};
+
+EventSourceReceiver.prototype = new REventTarget();
+
+EventSourceReceiver.prototype.abort = function() {
+    var that = this;
+    if (that.es_close) {
+        that.es_close({}, true);
+    }
+};
diff --git a/lib/trans-receiver-htmlfile.js b/lib/trans-receiver-htmlfile.js
new file mode 100644
index 0000000..be70ccd
--- /dev/null
+++ b/lib/trans-receiver-htmlfile.js
@@ -0,0 +1,57 @@
+var _is_ie_htmlfile_capable;
+var isIeHtmlfileCapable = function() {
+    if (_is_ie_htmlfile_capable === undefined) {
+        if ('ActiveXObject' in window) {
+            try {
+                _is_ie_htmlfile_capable = !!new ActiveXObject('htmlfile');
+            } catch (x) {}
+        } else {
+            _is_ie_htmlfile_capable = false;
+        }
+    }
+    return _is_ie_htmlfile_capable;
+};
+
+
+var HtmlfileReceiver = function(url) {
+    var that = this;
+    utils.polluteGlobalNamespace();
+
+    that.id = 'a' + utils.random_string(6, 26);
+    url += ((url.indexOf('?') === -1) ? '?' : '&') +
+        'c=' + escape(WPrefix + '.' + that.id);
+
+    var constructor = isIeHtmlfileCapable() ?
+        utils.createHtmlfile : utils.createIframe;
+
+    var iframeObj;
+    _window[WPrefix][that.id] = {
+        start: function () {
+            iframeObj.loaded();
+        },
+        message: function (data) {
+            that.dispatchEvent(new SimpleEvent('message', {'data': data}));
+        },
+        stop: function () {
+            that.iframe_close({}, 'network');
+        }
+    };
+    that.iframe_close = function(e, abort_reason) {
+        iframeObj.cleanup();
+        that.iframe_close = iframeObj = null;
+        delete _window[WPrefix][that.id];
+        that.dispatchEvent(new SimpleEvent('close', {reason: abort_reason}));
+    };
+    iframeObj = constructor(url, function(e) {
+                                that.iframe_close({}, 'permanent');
+                            });
+};
+
+HtmlfileReceiver.prototype = new REventTarget();
+
+HtmlfileReceiver.prototype.abort = function() {
+    var that = this;
+    if (that.iframe_close) {
+        that.iframe_close({}, 'user');
+    }
+};
diff --git a/lib/trans-receiver-xhr.js b/lib/trans-receiver-xhr.js
new file mode 100644
index 0000000..6706375
--- /dev/null
+++ b/lib/trans-receiver-xhr.js
@@ -0,0 +1,36 @@
+
+var XhrCorsReceiver = function(url) {
+    var that = this;
+    var buf_pos = 0;
+    var orsc = function (xhr, e, abort_reason) {
+        if ((xhr.readyState === 3 || xhr.readyState === 4) &&
+            xhr.responseText && xhr.status === 200) {
+            var msgs = [];
+            while (1) {
+                var buf = xhr.responseText.slice(buf_pos);
+                var p = buf.indexOf('\n');
+                if (p === -1) break;
+                buf_pos += p+1;
+                var msg = buf.slice(0, p);
+                that.dispatchEvent(new SimpleEvent('message', {'data': msg}));
+            }
+        }
+        if (xhr.readyState === 4 || abort_reason) {
+            var reason = abort_reason ? 'user' :
+                (xhr.status === 200 ? 'network' : 'permanent');
+            that.xhr_close = null;
+            that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
+        }
+    };
+    var corsXhr = window.XDomainRequest ? utils.createXDR : utils.createXHR;
+    that.xhr_close = corsXhr('POST', url, null, orsc);
+};
+
+XhrCorsReceiver.prototype = new REventTarget();
+
+XhrCorsReceiver.prototype.abort = function() {
+    var that = this;
+    if (that.xhr_close) {
+        that.xhr_close(true);
+    }
+};

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