Commit 684ca330 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

adding a timeout onerror callback to non JSONP long polling connections

parent b98f25ef
...@@ -151,16 +151,20 @@ ...@@ -151,16 +151,20 @@
_send : function(settings, post) { _send : function(settings, post) {
settings = settings || {}; settings = settings || {};
settings.timeout = settings.timeout || 30000;
var xhr = Ajax._getXHRObject(); var xhr = Ajax._getXHRObject();
if (!xhr||!settings.url) return; if (!xhr||!settings.url) return;
Ajax.clear(settings);
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState == 4) { if (xhr.readyState == 4) {
Ajax.clear(settings);
if (settings.afterReceive) settings.afterReceive(xhr); if (settings.afterReceive) settings.afterReceive(xhr);
if(xhr.status == 200) { if(xhr.status == 200) {
if (settings.success) settings.success(xhr.responseText); if (settings.success) settings.success(xhr.responseText);
} else { } else {
if (settings.error) settings.error(xhr.status); if (settings.error) settings.error(xhr.status || 304);
} }
} }
} }
...@@ -182,10 +186,19 @@ ...@@ -182,10 +186,19 @@
if (settings.beforeSend) settings.beforeSend(xhr); if (settings.beforeSend) settings.beforeSend(xhr);
var onerror = function() {
try { xhr.abort(); } catch (e) { /* ignore error on closing */ }
Ajax.clear(settings);
settings.error(304);
};
if (post) { if (post) {
xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
} else {
settings.timeoutId = window.setTimeout(onerror, settings.timeout + 2000);
} }
xhr.send(body); xhr.send(body);
return xhr; return xhr;
}, },
...@@ -198,16 +211,20 @@ ...@@ -198,16 +211,20 @@
} }
}, },
clear : function(settings) { _clear_timeout : function(settings) {
if (settings.timeoutId) { if (settings.timeoutId) {
settings.timeoutId = window.clearTimeout(settings.timeoutId); settings.timeoutId = window.clearTimeout(settings.timeoutId);
} }
},
clear : function(settings) {
Ajax._clear_timeout(settings);
Ajax._clear_script(document.getElementById(settings.scriptId)); Ajax._clear_script(document.getElementById(settings.scriptId));
}, },
jsonp : function(settings) { jsonp : function(settings) {
settings.timeout = settings.timeout || 30000; settings.timeout = settings.timeout || 30000;
Ajax.clear(settings);
var head = document.head || document.getElementsByTagName("head")[0]; var head = document.head || document.getElementsByTagName("head")[0];
var script = document.createElement("script"); var script = document.createElement("script");
...@@ -232,7 +249,7 @@ ...@@ -232,7 +249,7 @@
if (settings.beforeOpen) settings.beforeOpen({}); if (settings.beforeOpen) settings.beforeOpen({});
if (settings.beforeSend) settings.beforeSend({}); if (settings.beforeSend) settings.beforeSend({});
settings.timeoutId = window.setTimeout(onerror, settings.timeout + 10); settings.timeoutId = window.setTimeout(onerror, settings.timeout + 2000);
settings.scriptId = settings.scriptId || new Date().getTime(); settings.scriptId = settings.scriptId || new Date().getTime();
script.setAttribute("src", addTimestampToUrl(settings.url) + '&' + Ajax._parse_data(settings)); script.setAttribute("src", addTimestampToUrl(settings.url) + '&' + Ajax._parse_data(settings));
...@@ -515,7 +532,7 @@ ...@@ -515,7 +532,7 @@
ifr.onload = linker(onerrorCallback, this); ifr.onload = linker(onerrorCallback, this);
this.connection = ifr; this.connection = ifr;
} }
this.frameloadtimer = setTimeout(linker(onerrorCallback, this), this.pushstream.timeout); this.frameloadtimer = window.setTimeout(linker(onerrorCallback, this), this.pushstream.timeout);
}, },
register: function(iframeWindow) { register: function(iframeWindow) {
...@@ -542,7 +559,7 @@ ...@@ -542,7 +559,7 @@
setPingTimer: function() { setPingTimer: function() {
if (this.pingtimer) clearTimer(this.pingtimer); if (this.pingtimer) clearTimer(this.pingtimer);
this.pingtimer = setTimeout(linker(onerrorCallback, this), this.pushstream.pingtimeout); this.pingtimer = window.setTimeout(linker(onerrorCallback, this), this.pushstream.pingtimeout);
} }
}; };
...@@ -586,19 +603,18 @@ ...@@ -586,19 +603,18 @@
this.xhrSettings.data.callback = "PushStreamManager[" + this.pushstream.id + "].wrapper.onmessage"; this.xhrSettings.data.callback = "PushStreamManager[" + this.pushstream.id + "].wrapper.onmessage";
} }
this._internalListen(); this._internalListen();
this.opentimer = setTimeout(linker(onopenCallback, this), 5000); this.opentimer = window.setTimeout(linker(onopenCallback, this), 5000);
Log4js.info("[LongPolling] connecting to:", this.xhrSettings.url); Log4js.info("[LongPolling] connecting to:", this.xhrSettings.url);
}, },
_listen: function() { _listen: function() {
if (this._internalListenTimeout) clearTimer(this._internalListenTimeout); if (this._internalListenTimeout) clearTimer(this._internalListenTimeout);
this._internalListenTimeout = setTimeout(this._linkedInternalListen, this.pushstream.longPollingInterval); this._internalListenTimeout = window.setTimeout(this._linkedInternalListen, this.pushstream.longPollingInterval);
}, },
_internalListen: function() { _internalListen: function() {
if (this.connectionEnabled) { if (this.connectionEnabled) {
if (this.useJSONP) { if (this.useJSONP) {
Ajax.clear(this.xhrSettings);
Ajax.jsonp(this.xhrSettings); Ajax.jsonp(this.xhrSettings);
} else if (!this.connection) { } else if (!this.connection) {
this.connection = Ajax.load(this.xhrSettings); this.connection = Ajax.load(this.xhrSettings);
...@@ -890,7 +906,7 @@ ...@@ -890,7 +906,7 @@
_reconnect: function(timeout) { _reconnect: function(timeout) {
if (this._keepConnected && !this.reconnecttimer && (this.readyState != PushStream.CONNECTING)) { if (this._keepConnected && !this.reconnecttimer && (this.readyState != PushStream.CONNECTING)) {
Log4js.info("trying to reconnect in", timeout); Log4js.info("trying to reconnect in", timeout);
this.reconnecttimer = setTimeout(linker(this._connect, this), timeout); this.reconnecttimer = window.setTimeout(linker(this._connect, this), timeout);
} }
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment