[Pkg-javascript-commits] [sockjs-client] 34/434: Xhr CORS polling transport.
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 607e1faa466d5434e29d88ffe383bc6d38be35ac
Author: Marek Majkowski <majek04 at gmail.com>
Date: Mon Aug 1 13:48:02 2011 +0100
Xhr CORS polling transport.
---
example-latency.html | 2 +-
lib/main.js | 1 +
lib/sockjs.js | 5 ++--
lib/trans-iframe-eventsource.js | 1 +
lib/trans-jsonp-sender.js | 13 ++++-----
lib/trans-xhr.js | 24 ----------------
lib/trans-xhrpolling.js | 64 +++++++++++++++++++++++++++++++++++++++++
lib/utils.js | 38 +++++++++++++++---------
8 files changed, 100 insertions(+), 48 deletions(-)
diff --git a/example-latency.html b/example-latency.html
index 2978b7f..71486d2 100644
--- a/example-latency.html
+++ b/example-latency.html
@@ -42,7 +42,7 @@
var i = 0;
- var sjs = new SockJS(sockjs_url, ['jsonp']);
+ var sjs = new SockJS(sockjs_url, ['xhrpolling']);
var payload = Array(1024).join('x');
function do_send(){
sjs.send({t: (new Date()).getTime(), payload:payload})
diff --git a/lib/main.js b/lib/main.js
index fbfac9d..836007c 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -10,6 +10,7 @@ SockJS = (function(){
<!-- include lib/trans-jsonp-sender.js -->
<!-- include lib/trans-jsonp-receiver.js -->
<!-- include lib/trans-jsonp.js -->
+<!-- include lib/trans-xhrpolling.js -->
<!-- include lib/trans-iframe.js -->
<!-- include lib/trans-iframe-within.js -->
<!-- include lib/trans-iframe-eventsource.js -->
diff --git a/lib/sockjs.js b/lib/sockjs.js
index 01f5f1c..1e56e52 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -16,7 +16,7 @@ var SockJS = function(url, protocols, options) {
}
that.protocol = null;
that.readyState = SockJS.CONNECTING;
- that._try_next_protocol();
+ that._didClose();
};
// Inheritance
SockJS.prototype = new REventTarget();
@@ -55,7 +55,8 @@ SockJS.prototype._didClose = function(status, reason) {
that.readyState !== SockJS.OPEN &&
that.readyState !== SockJS.CLOSING)
throw new Error('INVALID_STATE_ERR');
- that._transport.doCleanup();
+ if (that._transport)
+ that._transport.doCleanup();
that._transport = null;
var close_event = new SimpleEvent("close", {status: status, reason: reason});
diff --git a/lib/trans-iframe-eventsource.js b/lib/trans-iframe-eventsource.js
index d42d04e..762b806 100644
--- a/lib/trans-iframe-eventsource.js
+++ b/lib/trans-iframe-eventsource.js
@@ -26,6 +26,7 @@ var EventSourceTransport = FacadeJS['w-iframe-eventsource'] = function (ri, tran
};
that.send_constructor(ajaxSender);
};
+
// Inheritnace
EventSourceTransport.prototype = new BufferedSender();
diff --git a/lib/trans-jsonp-sender.js b/lib/trans-jsonp-sender.js
index 95c29d3..d0e2e82 100644
--- a/lib/trans-jsonp-sender.js
+++ b/lib/trans-jsonp-sender.js
@@ -16,7 +16,7 @@ BufferedSender.prototype.send_schedule = function(message) {
var that = this;
if (that.send_buffer.length > 0) {
var payload = '[' + that.send_buffer.join(',') + ']';
- that.send_stop = that.sender(that.trans_url+'/send',
+ that.send_stop = that.sender(that.trans_url,
payload,
function() {
that.send_stop = undefined;
@@ -51,7 +51,7 @@ var jsonPGenericSender = function(url, payload, callback) {
var form = that._send_form;
var id = 'a' + utils.random_string(8);
form.target = id;
- form.action = url + '?i=' + id;
+ form.action = url + '/jsonp_send?i=' + id;
var iframe;
try {
@@ -83,12 +83,11 @@ var jsonPGenericSender = function(url, payload, callback) {
};
var ajaxSender = function(url, payload, callback) {
- var completed = function (xhr) {
- if(xhr.readyState === 4) {
- callback();
+ var orsc = function (xhr, e, abort_reason) {
+ if(xhr.readyState === 4 || abort_reason) {
+ callback(xhr.status, abort_reason);
}
};
- utils.createXHR('POST', url, payload, completed);
- return completed;
+ return utils.createXHR('POST', url + '/xhr_send', payload, orsc);
};
diff --git a/lib/trans-xhr.js b/lib/trans-xhr.js
deleted file mode 100644
index 453a9d7..0000000
--- a/lib/trans-xhr.js
+++ /dev/null
@@ -1,24 +0,0 @@
-
-
-var XhrTransport = SockJS.xhrpolling = function(ri, trans_url){
- var that = this;
- that.ri = ri;
- that.trans_url = trans_url;
- that.send_constructor(xhrSender);
- that._schedule_recv();
-};
-
-XhrTransport.prototype = new BufferedSender();
-
-XhrTransport.prototype._schedule_recv = function() {
- var that = this;
-};
-
-
-// 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/
-XhrTransport.enabled = function() {
- if ('XMLHttpRequest' in window) return false;
- return ('withCredentials' in new XMLHttpRequest());
-};
\ No newline at end of file
diff --git a/lib/trans-xhrpolling.js b/lib/trans-xhrpolling.js
new file mode 100644
index 0000000..5590edb
--- /dev/null
+++ b/lib/trans-xhrpolling.js
@@ -0,0 +1,64 @@
+// Requires CORS-enabled browser.
+
+var XhrTransport = SockJS.xhrpolling = function(ri, trans_url){
+ var that = this;
+ that.ri = ri;
+ that.trans_url = trans_url;
+ that.send_constructor(ajaxSender);
+ that._schedule_recv();
+};
+
+XhrTransport.prototype = new BufferedSender();
+
+XhrTransport.prototype._schedule_recv = function() {
+ var that = this;
+ var 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._recv_stop = xhrPoll(that.trans_url + '/xhr', callback);
+};
+
+XhrTransport.prototype.doCleanup = function() {
+ var that = this;
+ that._is_closing = true;
+ if (that._recv_stop) {
+ that._recv_stop();
+ }
+ that.ri = that._recv_stop = null;
+ that.send_destructor();
+};
+
+var xhrPoll = function(url, user_callback) {
+ var orsc = function (xhr, e, abort_reason) {
+ if (xhr.readyState === 4 || abort_reason) {
+ user_callback(xhr, abort_reason);
+ }
+ };
+ // Using POST can save us from caching issues.
+ return utils.createXHR('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/
+XhrTransport.enabled = function() {
+ if (!('XMLHttpRequest' in window)) return false;
+ return ('withCredentials' in new XMLHttpRequest());
+};
+
diff --git a/lib/utils.js b/lib/utils.js
index ed3bf99..d81ec54 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -99,27 +99,37 @@ utils.createXHR = function(method, url, payload, callback) {
delete xhrDefaultHeaders[k];
}
}
+
+ var cleanup = function() {
+ // IE needs this field to be a function
+ try{
+ xhr.onreadystatechange = null;
+ } 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) {};
+ callback = xhr = null;
+ };
+
xhr.onreadystatechange = function (e) {
- if(xhr){
+ if (xhr && callback) {
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;
+ cleanup();
}
}
};
xhr.send(payload);
- return xhr;
+ return function (abort_reason) {
+ if (callback) {
+ callback(xhr, null, abort_reason);
+ cleanup();
+ }
+ };
};
var WPrefix = '_jp';
--
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