public
Created

Faye CORS transport with a single XDomainRequest

  • Download Gist
gistfile1.darcspatch
Darcs Patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
diff --git a/javascript/transport/cors.js b/javascript/transport/cors.js
index e3af40d..074c1f7 100644
--- a/javascript/transport/cors.js
+++ b/javascript/transport/cors.js
@@ -1,26 +1,29 @@
Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
+ xhr: null,
request: function(message, timeout) {
var xhrClass = Faye.ENV.XDomainRequest ? XDomainRequest : XMLHttpRequest,
- xhr = new xhrClass(),
retry = this.retry(message, timeout),
self = this;
+
+ if (!this.xhr) {
+ this.xhr = new xhrClass();
+ this.xhr.timeout = 100000;
+ } else {
+ try { this.xhr.abort(); } catch(e){}
+ }
 
- xhr.open('POST', this._endpoint, true);
-
- xhr.onload = function() {
+ this.xhr.open('POST', this._endpoint, true);
+ this.xhr.onload = function() {
try {
- self.receive(JSON.parse(xhr.responseText));
+ self.receive(JSON.parse(self.xhr.responseText));
} catch(e) {
retry();
- } finally {
- xhr.onload = xhr.onerror = null;
- xhr = null;
}
};
- xhr.onerror = retry;
- xhr.ontimeout = retry;
- xhr.onprogress = function() {};
- xhr.send('message=' + encodeURIComponent(Faye.toJSON(message)));
+ this.xhr.onerror = retry;
+ this.xhr.ontimeout = retry;
+ this.xhr.onprogress = function() {};
+ this.xhr.send('message=' + encodeURIComponent(Faye.toJSON(message)));
}
}), {
isUsable: function(endpoint, callback, scope) {

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.