[Pkg-javascript-commits] [sockjs-client] 108/434: Unified all XHR/XDR transports.
Tonnerre Lombard
tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:07 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 dc7d7cace07394e044d03d9af252eb632aa9c8d7
Author: Marek Majkowski <majek04 at gmail.com>
Date: Thu Aug 25 17:14:12 2011 +0100
Unified all XHR/XDR transports.
---
lib/index.js | 2 +-
lib/trans-iframe-xhr-polling.js | 20 ++++++----
lib/trans-xhr-polling.js | 82 +++++------------------------------------
lib/trans-xhr-streaming.js | 2 +-
4 files changed, 24 insertions(+), 82 deletions(-)
diff --git a/lib/index.js b/lib/index.js
index 1778a80..ad1b2f2 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -10,8 +10,8 @@ SockJS = (function(){
<!-- include lib/trans-jsonp-sender.js -->
<!-- include lib/trans-jsonp-receiver.js -->
<!-- include lib/trans-jsonp-polling.js -->
-<!-- include lib/trans-xhr-polling.js -->
<!-- include lib/trans-xhr-streaming.js -->
+<!-- include lib/trans-xhr-polling.js -->
<!-- include lib/trans-iframe.js -->
<!-- include lib/trans-iframe-within.js -->
<!-- include lib/trans-iframe-eventsource.js -->
diff --git a/lib/trans-iframe-xhr-polling.js b/lib/trans-iframe-xhr-polling.js
index ba3dac0..06ec114 100644
--- a/lib/trans-iframe-xhr-polling.js
+++ b/lib/trans-iframe-xhr-polling.js
@@ -12,15 +12,21 @@ XhrPollingIframeTransport.enabled = function () {
};
-// Exactly the same as xhr-polling, but with different `enabled`
-var XhrPollingITransport = FacadeJS['w-iframe-xhr-polling'] = function () {
- XhrTransport.apply(this, arguments);
+var XhrPollingITransport = FacadeJS['w-iframe-xhr-polling'] = function (ri, trans_url) {
+ var that = this;
+ that.trans_url = trans_url;
+ that.send_constructor(ajaxSender);
+ that.poll = new Polling(ri, XhrReceiver, trans_url + '/xhr', {cors: false});
};
+
// Inheritnace
XhrPollingITransport.prototype = new BufferedSender();
-XhrPollingITransport.prototype._schedule_recv =
- XhrTransport.prototype._schedule_recv;
-XhrPollingITransport.prototype.doCleanup =
- XhrTransport.prototype.doCleanup;
+XhrPollingITransport.prototype.doCleanup = function() {
+ var that = this;
+ if (that.poll) {
+ that.poll.abort();
+ that.poll = null;
+ }
+};
diff --git a/lib/trans-xhr-polling.js b/lib/trans-xhr-polling.js
index 69fa231..f5070ad 100644
--- a/lib/trans-xhr-polling.js
+++ b/lib/trans-xhr-polling.js
@@ -1,85 +1,21 @@
-// Requires CORS-enabled browser.
-
-var XhrTransport = SockJS['xhr-polling'] = function(ri, trans_url) {
+var XhrPollingTransport = SockJS['xhr-polling'] = function (ri, trans_url) {
var that = this;
that.ri = ri;
that.trans_url = trans_url;
that.send_constructor(xdrSender);
- that._schedule_recv();
+ that.poll = new Polling(ri, XhrReceiver, trans_url + '/xhr', {cors: true});
};
-XhrTransport.prototype = new BufferedSender();
-
-XhrTransport.prototype._schedule_recv = function() {
- var that = this;
- 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 from "xhr-polling" to "xhr-streaming"');
- }
- }
- };
- var end_callback = function (xhr, abort_reason) {
- that._recv_stop = null;
- if (abort_reason) return;
- if (xhr.status === 200) {
- if (!that._is_closing) {
- that._schedule_recv();
- }
- } else {
- that.ri._didClose(1006, "XHR error (" + xhr.status + ")");
- }
- };
- var postfix = that._streaming ? '/xhr_streaming' : '/xhr';
- that._recv_stop = xhrPoll(that.trans_url + postfix, message_callback, end_callback);
-};
+// Inheritnace
+XhrPollingTransport.prototype = new BufferedSender();
-XhrTransport.prototype.doCleanup = function() {
+XhrPollingTransport.prototype.doCleanup = function() {
var that = this;
- that._is_closing = true;
- if (that._recv_stop) {
- that._recv_stop();
+ if (that.poll) {
+ that.poll.abort();
+ that.poll = null;
}
- that.ri = that._recv_stop = null;
- that.send_destructor();
};
-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('\n');
- 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) {
- end_callback(xhr, abort_reason);
- }
- };
- // Using POST can save us from caching issues.
- var fun = window.XDomainRequest ? utils.createXDR : utils.createXHR;
- 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/
-XhrTransport.enabled = function() {
- if (window.XDomainRequest) return true;
- if (window.XMLHttpRequest &&
- 'withCredentials' in new XMLHttpRequest()) return true;
- return false;
-};
+XhrPollingTransport.enabled = XhrStreamingTransport.enabled;
diff --git a/lib/trans-xhr-streaming.js b/lib/trans-xhr-streaming.js
index c38db7b..3e4f17e 100644
--- a/lib/trans-xhr-streaming.js
+++ b/lib/trans-xhr-streaming.js
@@ -3,7 +3,7 @@ var XhrStreamingTransport = SockJS['xhr-streaming'] = function (ri, trans_url) {
that.ri = ri;
that.trans_url = trans_url;
that.send_constructor(xdrSender);
- that.poll = new Polling(ri, XhrCorsReceiver, trans_url + '/xhr_streaming');
+ that.poll = new Polling(ri, XhrReceiver, trans_url + '/xhr_streaming', {cors: true});
};
// Inheritnace
--
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