[Pkg-javascript-commits] [sockjs-client] 20/434: Htmlfile transport.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:46:59 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 5342942e6d2d77fc977a49bcc387c11aede49886
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Mon Jul 25 14:08:50 2011 +0100

    Htmlfile transport.
---
 lib/main.js                     |  1 +
 lib/sockjs.js                   |  2 +-
 lib/trans-iframe-eventsource.js |  9 +++--
 lib/trans-iframe-htmlfile.js    | 88 +++++++++++++++++++++++++++++++++++++++++
 lib/trans-iframe-within.js      | 10 ++++-
 lib/trans-iframe.js             | 17 +++++---
 lib/trans-jsonp-sender.js       | 17 ++++++--
 lib/trans-jsonp.js              | 12 +++---
 lib/utils.js                    | 68 ++++++++++++++++++++++++++++++-
 tests-src/test-run.coffee       |  2 +-
 10 files changed, 201 insertions(+), 25 deletions(-)

diff --git a/lib/main.js b/lib/main.js
index 9a77b48..e617bbf 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -12,6 +12,7 @@ SockJS = (function(){
 <!-- include lib/trans-iframe.js -->
 <!-- include lib/trans-iframe-within.js -->
 <!-- include lib/trans-iframe-eventsource.js -->
+<!-- include lib/trans-iframe-htmlfile.js -->
                   return SockJS;
           })();
 if ('_sockjs_onload' in window) setTimeout(function(){_sockjs_onload();}, 0);
diff --git a/lib/sockjs.js b/lib/sockjs.js
index 3946a7a..a95a164 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -8,7 +8,7 @@ var SockJS = function(url, protocols, options) {
     this._server = this._options.server || utils.random_number_string(1000);
     this._connid = utils.random_string(8);
     this._trans_url = this._base_url + '/' + this._server + '/' + this._connid;
-    this._protocols = ['websocket', 'iframe-eventsource', 'jsonp'];
+    this._protocols = ['websocket', 'iframe-eventsource', 'iframe-htmlfile', 'jsonp'];
     switch(typeof protocols) {
     case 'undefined': break;
     case 'string': this._protocols = [protocols]; break;
diff --git a/lib/trans-iframe-eventsource.js b/lib/trans-iframe-eventsource.js
index 9539f05..bf5b782 100644
--- a/lib/trans-iframe-eventsource.js
+++ b/lib/trans-iframe-eventsource.js
@@ -23,15 +23,16 @@ var EventSourceTransport = FacadeJS['w-iframe-eventsource'] = function (ri, tran
         that.cleanup();
         that.ri._didClose(1001, "Socket closed.");
     };
-    that.send_constructor(jsonPGenericSender);
+    that.send_constructor(ajaxSender);
 };
 // Inheritnace
 EventSourceTransport.prototype = new BufferedSender();
 
 EventSourceTransport.prototype.cleanup = function() {
-    this.es.onopen = this.es.onmessage = this.es.onerror = null;
-    this.es.close();
-    this.es = null;
+    var that = this;
+    that.es.onopen = that.es.onmessage = that.es.onerror = null;
+    that.es.close();
+    that.es = null;
 };
 
 EventSourceTransport.prototype.doClose = function(data) {
diff --git a/lib/trans-iframe-htmlfile.js b/lib/trans-iframe-htmlfile.js
new file mode 100644
index 0000000..af61a34
--- /dev/null
+++ b/lib/trans-iframe-htmlfile.js
@@ -0,0 +1,88 @@
+var HtmlFileIframeTransport = SockJS['iframe-htmlfile'] = function () {
+    var that = this;
+    that.protocol = 'w-iframe-htmlfile';
+    that.i_constructor.apply(this, arguments);
+};
+// Inheritance.
+HtmlFileIframeTransport.prototype = new IframeTransport();
+HtmlFileIframeTransport.enabled = function () {
+    return true && IframeTransport.enabled();
+};
+
+
+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.id = 'a' + utils.random_string(6, 26);
+    var iframe_url = trans_url + '/htmlfile?c=' + escape(WPrefix + '.' + that.id);
+    var callback = function(e, t) {
+        // alert('cb' + e + ' ' + t);
+        // if (window.console)
+        //     console.log('cb', e, t, typeof t);
+        switch(t) {
+        case 'open':
+            // should get a proper messsage from now on.
+            try{
+                that.iframe.onload = undefined;
+            } catch (x) {}
+            that.iframe.onerror = undefined;
+            that.ri._didOpen();
+            break;
+        case 'close':
+            that.cleanup();
+            if (e) {
+                that.ri._didClose(e.status, e.reason);
+            } else {
+                that.ri._didClose(1001, "Server closed iframe");
+            }
+            break;
+        }
+        if (typeof t === 'undefined') {
+            that.ri._didMessage(e);
+        }
+    };
+    _window[WPrefix][that.id] = callback;
+
+    var iframe = that.iframe = _document.createElement('iframe');
+    iframe.src = iframe_url;
+    iframe.style.display = 'none';
+    iframe.style.position = 'absolute';
+    iframe.onerror = function() {
+        that.cleanup();
+        that.ri._didClose(1001, "Can't load iframe");
+    };
+    iframe.onload = function() {
+        // `onload` is triggered before scripts on the iframe are
+        // executed. Give it few seconds to actually load stuff.
+        setTimeout(function() {
+                       if (that.iframe && that.iframe.onerror) that.iframe.onerror();
+                   }, 5000);
+    };
+    _document.body.appendChild(iframe);
+};
+// Inheritnace
+HtmlFileTransport.prototype = new BufferedSender();
+
+HtmlFileTransport.prototype.cleanup = function() {
+    var that = this;
+    if (that.iframe) {
+        that.iframe.onload = that.iframe.onerror = null;
+        that.iframe.parentNode.removeChild(that.iframe);
+        that.iframe = undefined;
+        delete _window[WPrefix][that.id];
+    }
+};
+
+HtmlFileTransport.prototype.doClose = function(data) {
+    var that = this;
+    that.cleanup();
+    // Send didClose out of band.
+    setTimeout(function(){that.ri._didClose(1001, "Socket closed.");}, 0);
+};
+
+
diff --git a/lib/trans-iframe-within.js b/lib/trans-iframe-within.js
index 866c913..0997b93 100644
--- a/lib/trans-iframe-within.js
+++ b/lib/trans-iframe-within.js
@@ -1,6 +1,8 @@
 var postMessage = function (type, messages) {
     var msg = JSON.stringify(messages);
-    if(parent !== _window) parent.postMessage(type + msg, '*');
+    if(parent !== window) {
+        parent.postMessage(type + msg, '*');
+    }
 };
 
 var FacadeJS = function() {};
@@ -51,9 +53,13 @@ SockJS.bootstrap_iframe = function() {
             break;
         }
     };
+
+    // alert('test ticker');
+    // facade = new FacadeJS();
+    // facade._transport = new FacadeJS['w-iframe-htmlfile'](facade, 'http://172.16.173.128:9999/ticker/12/basd');
+
     utils.attachMessage(onMessage);
 
     // Start
     postMessage('s', []);
 };
-
diff --git a/lib/trans-iframe.js b/lib/trans-iframe.js
index 577b68c..85507b4 100644
--- a/lib/trans-iframe.js
+++ b/lib/trans-iframe.js
@@ -15,6 +15,9 @@ IframeTransport.prototype.i_constructor = function(ri, trans_url, base_url) {
     that.trans_url = trans_url;
 
     var iframe_url = base_url + '/iframe-' + SockJS.version + '.html';
+    if (that.ri._options.devel) {
+        iframe_url += '?t=' + (+new Date);
+    }
 
     var iframe = that.iframe = _document.createElement('iframe');
     iframe.src = iframe_url;
@@ -25,6 +28,8 @@ IframeTransport.prototype.i_constructor = function(ri, trans_url, base_url) {
         that.ri._didClose(1001, "Can't load iframe");
     };
     iframe.onload = function() {
+        utils.detachMessage(that.onmessage_cb);
+        utils.attachMessage(that.onmessage_cb);
         // `onload` is triggered before scripts on the iframe are
         // executed. Give it few seconds to actually load stuff.
         setTimeout(function() {
@@ -48,14 +53,10 @@ IframeTransport.prototype.cleanup = function() {
 
 IframeTransport.prototype.onmessage = function(e) {
     var that = this;
-    if (e.origin !== that.origin ||
-        e.source !== that.iframe.contentWindow) return;
+    if (e.origin !== that.origin) return;
     var type = e.data.slice(0, 1);
     var msg = JSON.parse(e.data.slice(1));
     switch(type) {
-    case 'x':
-        that.iframe.onload = that.iframe.onerror = null;
-        break;
     case 's':
         that.iframe.onload = that.iframe.onerror = null;
         that.postMessage('s', [SockJS.version, that.protocol, that.trans_url]);
@@ -87,5 +88,9 @@ IframeTransport.prototype.doClose = function () {
 };
 
 IframeTransport.enabled = function() {
-    return (typeof _window.postMessage === 'function');
+    // postMessage misbehaves in konqueror 4.6.5 - the messages are delivered with
+    // huge delay, or not at all.
+    var konqueror = navigator && navigator.userAgent && navigator.userAgent.indexOf('Konqueror') !== -1;
+    return ((typeof _window.postMessage === 'function' ||
+            typeof _window.postMessage === 'object') && (!konqueror));
 };
diff --git a/lib/trans-jsonp-sender.js b/lib/trans-jsonp-sender.js
index e3ce6b0..d6664ea 100644
--- a/lib/trans-jsonp-sender.js
+++ b/lib/trans-jsonp-sender.js
@@ -15,7 +15,7 @@ BufferedSender.prototype.doSend = function(message) {
 BufferedSender.prototype.send_schedule = function(message) {
     var that = this;
     if (that.send_buffer.length > 0) {
-        that.send_stop = that.sender(that.trans_url+'/send', that.send_buffer,
+        that.send_stop = that.sender(that.trans_url+'/send', JSON.stringify(that.send_buffer),
                                      function() {
                                          that.send_stop = undefined;
                                          that.send_schedule();
@@ -24,7 +24,7 @@ BufferedSender.prototype.send_schedule = function(message) {
     }
 };
 
-var jsonPGenericSender = function(url, messages, callback) {
+var jsonPGenericSender = function(url, payload, callback) {
     var that = this;
     if (!('_send_form' in that)) {
         var form = that._send_form = _document.createElement('form');
@@ -53,7 +53,7 @@ var jsonPGenericSender = function(url, messages, callback) {
     }
     iframe.id = id;
     form.appendChild(iframe);
-    form.d.value = JSON.stringify(messages);
+    form.d.value = payload;
     form.submit();
 
     var completed = function() {
@@ -71,3 +71,14 @@ var jsonPGenericSender = function(url, messages, callback) {
     };
     return completed;
 };
+
+var ajaxSender = function(url, payload, callback) {
+    var completed = function (xhr) {
+        if(xhr.readyState === 4) {
+            callback();
+        }
+    };
+    utils.createXHR('POST', url, payload, completed);
+    return completed;
+};
+
diff --git a/lib/trans-jsonp.js b/lib/trans-jsonp.js
index def66cd..ce53c59 100644
--- a/lib/trans-jsonp.js
+++ b/lib/trans-jsonp.js
@@ -6,11 +6,9 @@
 //   o you will get a spinning cursor
 //   o for Konqueror a dumb timer is needed to detect network error
 
-var JsonPrefix = '_jp';
 
 var JsonPTransport = SockJS.jsonp = function(ri, trans_url){
-    // Unavoidable namespace pollution.
-    if (!(JsonPrefix in _window)) {_window[JsonPrefix] = {};}
+    utils.polluteGlobalNamespace();
     var that = this;
     that.ri = ri;
     that.trans_url = trans_url;
@@ -73,16 +71,16 @@ JsonPTransport.enabled = function() {
 
 var jsonPReceiverWrapper = function(url, constructReceiver, user_callback) {
     var id = 'a' + utils.random_string(6);
-    var url_id = url + '?c=' + escape(JsonPrefix + '.' + id);
+    var url_id = url + '?c=' + escape(WPrefix + '.' + id);
     var callback = function(e, t) {
-        delete _window[JsonPrefix][id];
+        delete _window[WPrefix][id];
         user_callback(e, t);
     };
 
     var close_script = constructReceiver(url_id, callback);
-    _window[JsonPrefix][id] = close_script;
+    _window[WPrefix][id] = close_script;
     var stop = function() {
-        if (_window[JsonPrefix][id]) {
+        if (_window[WPrefix][id]) {
             close_script({status:1000, reson:"Normal closure"}, 'stop');
         }
     };
diff --git a/lib/utils.js b/lib/utils.js
index 2236efa..fff0870 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -28,6 +28,8 @@ utils.attachMessage = function(listener) {
         // According to: http://stevesouders.com/misc/test-postmessage.php
         // the message gets delivered only to 'document', not 'window'.
 	    _document.attachEvent("onmessage", listener);
+        // I get 'window' for ie8.
+	    _window.attachEvent("onmessage", listener);
     }
 };
 
@@ -35,7 +37,8 @@ utils.detachMessage = function(listener) {
     if (typeof _window.addEventListener !== 'undefined') {
         _window.removeEventListener("message", listener, false);
     } else {
-	    _document.removeEvent("onmessage", listener);
+        _document.detachEvent("onmessage", listener);
+	    _window.detachEvent("onmessage", listener);
     }
 };
 
@@ -54,3 +57,66 @@ utils.objectExtend = function(dst, src) {
     }
     return dst;
 };
+
+var xhrDefaultHeaders = {
+   // "User-Agent": '',
+   // "Accept-Encoding": '',
+   // "Accept-Charset": '',
+   // "Connection": "keep-alive",
+   // "Keep-Alive": '',
+    "Accept": '',
+    "Accept-Language": '',
+    "Content-Type": "T"
+};
+
+utils.createXHR = function(method, url, payload, callback) {
+    var xhr;
+    if (_window.ActiveXObject) {
+        try {
+            xhr = new ActiveXObject('Microsoft.XMLHTTP');
+        } catch(x) {}
+    }
+    if (!xhr) {
+        xhr = new XMLHttpRequest();
+    }
+    xhr.open(method, url, true);
+
+    // Try to clear some headers, in order to save bandwidth.
+    // See http://blog.mibbit.com/?p=143
+    for (var k in xhrDefaultHeaders) {
+        try {
+            xhr.setRequestHeader(k, xhrDefaultHeaders[k]);
+        } catch(x) {
+            delete xhrDefaultHeaders[k];
+        }
+    }
+    xhr.onreadystatechange = function (e) {
+        if(xhr){
+            callback(xhr, e);
+            if (xhr.readyState === 4) {
+                // explorer needs this field to be function
+                try{
+                    xhr.onreadystatechange = undefined;
+                } catch (x) {
+                    xhr.onreadystatechange = function(){};
+                }
+                // Explorer tends to keep connection open, even after the
+                // tab is closed: http://bugs.jquery.com/ticket/5280
+                try {
+                    xhr.abort();
+                } catch(e) {};
+                xhr = null;
+            }
+        }
+    };
+    xhr.send(payload);
+    return xhr;
+};
+
+var WPrefix = '_jp';
+
+utils.polluteGlobalNamespace = function() {
+    if (!(WPrefix in _window)) {
+        _window[WPrefix] = {};
+    }
+};
diff --git a/tests-src/test-run.coffee b/tests-src/test-run.coffee
index a14f97a..1f0e6c1 100644
--- a/tests-src/test-run.coffee
+++ b/tests-src/test-run.coffee
@@ -18,5 +18,5 @@ test_protocol = (protocol) ->
         asyncTest("invalid url port", test_invalid_url_port(protocol))
 
 
-for protocol in ['websocket', 'jsonp', 'iframe-eventsource']
+for protocol in ['websocket', 'jsonp', 'iframe-eventsource', 'iframe-htmlfile']
     test_protocol(protocol)

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