diff options
author | Artur Signell <artur@vaadin.com> | 2013-04-29 11:46:11 +0300 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2013-04-29 11:46:45 +0300 |
commit | 17a60afd6eaab10a24de5cde3bffaeaf23ba3cd2 (patch) | |
tree | 42404f34b9648c379d90f833509e190fc7e43a50 /WebContent | |
parent | f3f6ab0df5b525aca092a06613fade93c5b9ab44 (diff) | |
download | vaadin-framework-17a60afd6eaab10a24de5cde3bffaeaf23ba3cd2.tar.gz vaadin-framework-17a60afd6eaab10a24de5cde3bffaeaf23ba3cd2.zip |
Imported new Atmosphere JS from Atmosphere 1.0.x (#11642)
* Atmosphere change 2a6b6ce0f5fc1185df6d293d1c919840fabbadf9
Change-Id: I5460cc75045d78930c2a5e0ac0a0199cb1bea100
Diffstat (limited to 'WebContent')
-rw-r--r-- | WebContent/VAADIN/jquery.atmosphere.js | 349 |
1 files changed, 204 insertions, 145 deletions
diff --git a/WebContent/VAADIN/jquery.atmosphere.js b/WebContent/VAADIN/jquery.atmosphere.js index e9def6ae95..bea5d5f230 100644 --- a/WebContent/VAADIN/jquery.atmosphere.js +++ b/WebContent/VAADIN/jquery.atmosphere.js @@ -49,7 +49,7 @@ jQuery.atmosphere = function() { }; return { - version : "1.0.12", + version : "1.0.13", requests : [], callbacks : [], @@ -138,13 +138,16 @@ jQuery.atmosphere = function() { */ var _response = { status: 200, + reasonPhrase : "OK", responseBody : '', + messages : [], headers : [], state : "messageReceived", transport : "polling", error: null, request : null, partialMessage : "", + errorHandled: false, id : 0 }; @@ -682,19 +685,21 @@ jQuery.atmosphere = function() { _close(); }; - _response.request = request; - var prevState = _response.state; - _response.state = state; - _response.status = 200; - var prevTransport = _response.transport; - _response.transport = transport; + if (_response.error == null) { + _response.request = request; + var prevState = _response.state; + _response.state = state; + _response.status = 200; + var prevTransport = _response.transport; + _response.transport = transport; - var _body = _response.responseBody; - _invokeCallback(); - _response.responseBody = _body; + var _body = _response.responseBody; + _invokeCallback(); + _response.responseBody = _body; - _response.state = prevState; - _response.transport = prevTransport; + _response.state = prevState; + _response.transport = prevTransport; + } } /** @@ -729,41 +734,43 @@ jQuery.atmosphere = function() { type : rq.method, dataType: "jsonp", error : function(jqXHR, textStatus, errorThrown) { + _response.error = true; if (jqXHR.status < 300) { _reconnect(_jqxhr, rq); } else { - _prepareCallback(textStatus, "error", jqXHR.status, rq.transport); + _onError(jqXHR.status, errorThrown); } }, jsonp : "jsonpTransport", success: function(json) { + if (rq.reconnect) { + if (rq.maxRequest == -1 || rq.requestCount++ < rq.maxRequest) { + _readHeaders(_jqxhr, rq); - if (rq.reconnect && (rq.maxRequest == -1 || rq.requestCount++ < rq.maxRequest)) { - _readHeaders(_jqxhr, rq); - - if (!rq.executeCallbackBeforeReconnect) { - _reconnect(_jqxhr, rq); - } + if (!rq.executeCallbackBeforeReconnect) { + _reconnect(_jqxhr, rq); + } - var msg = json.message; - if (msg != null && typeof msg != 'string') { - try { - msg = jQuery.stringifyJSON(msg); - } catch (err) { - // The message was partial + var msg = json.message; + if (msg != null && typeof msg != 'string') { + try { + msg = jQuery.stringifyJSON(msg); + } catch (err) { + // The message was partial + } } - } - if (_handleProtocol(rq, msg)) { - _prepareCallback(msg, "messageReceived", 200, rq.transport); - } + if (_handleProtocol(rq, msg)) { + _prepareCallback(msg, "messageReceived", 200, rq.transport); + } - if (rq.executeCallbackBeforeReconnect) { - _reconnect(_jqxhr, rq); + if (rq.executeCallbackBeforeReconnect) { + _reconnect(_jqxhr, rq); + } + } else { + jQuery.atmosphere.log(_request.logLevel, ["JSONP reconnect maximum try reached " + _request.requestCount]); + _onError(0, "maxRequest reached"); } - } else { - jQuery.atmosphere.log(_request.logLevel, ["JSONP reconnect maximum try reached " + _request.requestCount]); - _onError(); } }, data : rq.data, @@ -802,29 +809,32 @@ jQuery.atmosphere = function() { url : url, type : rq.method, error : function(jqXHR, textStatus, errorThrown) { + _response.error = true; if (jqXHR.status < 300) { _reconnect(_jqxhr, rq); } else { - _prepareCallback(textStatus, "error", jqXHR.status, rq.transport); + _onError(jqXHR.status, errorThrown); } }, success: function(data, textStatus, jqXHR) { - if (rq.reconnect && (rq.maxRequest == -1 || rq.requestCount++ < rq.maxRequest)) { - if (!rq.executeCallbackBeforeReconnect) { - _reconnect(_jqxhr, rq); - } + if (rq.reconnect) { + if (rq.maxRequest == -1 || rq.requestCount++ < rq.maxRequest) { + if (!rq.executeCallbackBeforeReconnect) { + _reconnect(_jqxhr, rq); + } - if (_handleProtocol(rq, data)) { - _prepareCallback(data, "messageReceived", 200, rq.transport); - } + if (_handleProtocol(rq, data)) { + _prepareCallback(data, "messageReceived", 200, rq.transport); + } - if (rq.executeCallbackBeforeReconnect) { - _reconnect(_jqxhr, rq); + if (rq.executeCallbackBeforeReconnect) { + _reconnect(_jqxhr, rq); + } + } else { + jQuery.atmosphere.log(_request.logLevel, ["AJAX reconnect maximum try reached " + _request.requestCount]); + _onError(0, "maxRequest reached"); } - } else { - jQuery.atmosphere.log(_request.logLevel, ["AJAX reconnect maximum try reached " + _request.requestCount]); - _onError(); } }, beforeSend : function(jqXHR) { @@ -912,7 +922,14 @@ jQuery.atmosphere = function() { } return; } - _sse = new EventSource(location, {withCredentials: _request.withCredentials}); + + try { + _sse = new EventSource(location, {withCredentials: _request.withCredentials}); + } catch (e) { + _onError(0, e); + _reconnectWithFallbackTransport("SSE failed. Downgrading to fallback transport and resending"); + return; + } if (_request.connectTimeout > 0) { _request.id = setTimeout(function() { @@ -949,7 +966,7 @@ jQuery.atmosphere = function() { _response.state = 'messageReceived'; _response.status = 200; - var message = message.data; + message = message.data; var skipCallbackInvocation = _trackMessageSize(message, _request, _response); if (jQuery.trim(message).length == 0) { @@ -959,6 +976,7 @@ jQuery.atmosphere = function() { if (!skipCallbackInvocation) { _invokeCallback(); _response.responseBody = ''; + _response.messages = []; } }; @@ -967,6 +985,7 @@ jQuery.atmosphere = function() { clearTimeout(_request.id); _response.state = 'closed'; _response.responseBody = ""; + _response.messages = []; _response.status = !sseOpened ? 501 : 200; _invokeCallback(); _clearState(); @@ -981,9 +1000,10 @@ jQuery.atmosphere = function() { _executeSSE(true); }, _request.reconnectInterval); _response.responseBody = ""; + _response.messages = []; } else { jQuery.atmosphere.log(_request.logLevel, ["SSE reconnect maximum try reached " + _requestCount]); - _onError(); + _onError(0, "maxReconnectOnClose reached"); } } }; @@ -1025,7 +1045,6 @@ jQuery.atmosphere = function() { } _websocket = _getWebSocket(location); - if (_request.connectTimeout > 0) { _request.id = setTimeout(function() { if (!webSocketOpened) { @@ -1062,6 +1081,7 @@ jQuery.atmosphere = function() { } webSocketOpened = true; + _websocket.webSocketOpened = webSocketOpened; if (_request.method == 'POST') { _response.state = "messageReceived"; @@ -1089,6 +1109,7 @@ jQuery.atmosphere = function() { if (!skipCallbackInvocation) { _invokeCallback(); _response.responseBody = ''; + _response.messages = []; } }; @@ -1098,6 +1119,7 @@ jQuery.atmosphere = function() { _websocket.onclose = function(message) { if (closed) return + clearTimeout(_request.id); var reason = message.reason; if (reason === "") { @@ -1135,9 +1157,9 @@ jQuery.atmosphere = function() { _response.state = 'closed'; _response.responseBody = ""; + _response.messages = []; _response.status = !webSocketOpened ? 501 : 200; _invokeCallback(); - clearTimeout(_request.id); closed = true; @@ -1151,12 +1173,13 @@ jQuery.atmosphere = function() { if (_request.reconnect && _requestCount++ < _request.maxReconnectOnClose) { _request.id = setTimeout(function() { _response.responseBody = ""; + _response.messages = []; _executeWebSocket(true); }, _request.reconnectInterval); } else { jQuery.atmosphere.log(_request.logLevel, ["Websocket reconnect maximum try reached " + _requestCount]); jQuery.atmosphere.warn("Websocket error, reason: " + message.reason); - _onError(); + _onError(0, "maxReconnectOnClose reached"); } } }; @@ -1174,12 +1197,14 @@ jQuery.atmosphere = function() { return true; } - function _onError() { + function _onError(code, reason) { _clearState(); _response.state = 'error'; + _response.reasonPhrase = reason; _response.responseBody = ""; - _response.status = 500; + _response.messages = []; + _response.status = code; _invokeCallback(); } @@ -1220,8 +1245,11 @@ jQuery.atmosphere = function() { if (messages.length != 0) { response.responseBody = messages.join(request.messageDelimiter); + response.messages = messages; return false; } else { + response.responseBody = ""; + response.messages = []; return true; } } else { @@ -1246,15 +1274,15 @@ jQuery.atmosphere = function() { } _request.transport = _request.fallbackTransport; - var reconnect = _request.reconnect && _requestCount++ < _request.maxReconnectOnClose; - if (reconnect && _request.transport != 'none' || _request.transport == null) { + if (_request.reconnect && _request.transport != 'none' || _request.transport == null) { _request.method = _request.fallbackMethod; _response.transport = _request.fallbackTransport; + _request.fallbackTransport = 'none'; _request.id = setTimeout(function() { _execute(); }, _request.reconnectInterval); - } else if (!reconnect) { - _onError(); + } else { + _onError(500, "Unable to reconnect with fallback transport"); } } @@ -1357,6 +1385,9 @@ jQuery.atmosphere = function() { rq = request; } + rq.lastIndex = 0; + rq.readyState = 0; + // CORS fake using JSONP if ((rq.transport == 'jsonp') || ((rq.enableXDR) && (jQuery.atmosphere.checkCORSSupport()))) { _jsonp(rq); @@ -1392,32 +1423,34 @@ jQuery.atmosphere = function() { _response.transport = rq.transport; } - if (!jQuery.browser.msie) { - ajaxRequest.onerror = function() { - try { - _response.status = XMLHttpRequest.status; - } catch(e) { - _response.status = 500; - } + ajaxRequest.onerror = function() { + _response.error = true; + try { + _response.status = XMLHttpRequest.status; + } catch(e) { + _response.status = 500; + } - if (!_response.status) { - _response.status = 500; - } - _clearState(); + if (!_response.status) { + _response.status = 500; + } + _clearState(); - if (rq.reconnect) { + if (!_response.errorHandled) { + if (rq.reconnect && _requestCount++ < rq.maxReconnectOnClose) { _reconnect(ajaxRequest, rq, true); } else { - _onError(); + _onError(0, "maxReconnectOnClose reached"); } - }; - } + } + _response.errorHandled = true; + }; ajaxRequest.onreadystatechange = function() { if (_abordingConnection) { return; } - + _response.error = null; var skipCallbackInvocation = false; var update = false; @@ -1453,17 +1486,23 @@ jQuery.atmosphere = function() { } if (update) { - var responseText = ajaxRequest.responseText; + // MSIE 9 and lower status can be higher than 1000, Chrome can be 0 + if (ajaxRequest.status >= 300 || ajaxRequest.status == 0) { - // MSIE status can be higher than 1000, Chrome can be 0 - if (ajaxRequest.status >= 500 || ajaxRequest.status == 0) { - if (rq.reconnect) { + var status = ajaxRequest.status > 1000 ? ajaxRequest.status = 0 : ajaxRequest.status; + // Allow recovering from cached content. + clearTimeout(rq.id); + + // Prevent onerror callback to be called + _response.errorHandled = true; + if (status < 400 && _requestCount++ < _request.maxReconnectOnClose) { _reconnect(ajaxRequest, rq, true); } else { - _onError(); + _onError(status, "maxReconnectOnClose reached"); } return; } + var responseText = jQuery.trim(ajaxRequest.responseText); _readHeaders(ajaxRequest, _request); @@ -1521,33 +1560,28 @@ jQuery.atmosphere = function() { _response.status = 404; } _response.state = "messageReceived"; - _response.responseBody = ajaxRequest.responseText.substring(rq.lastIndex); + + var message = ajaxRequest.responseText.substring(rq.lastIndex); rq.lastIndex = ajaxRequest.responseText.length; - if (!_handleProtocol( _request, _response.responseBody)) { - _reconnect(ajaxRequest, rq, false); - return; - } - _invokeCallback(); - if ((rq.transport == 'streaming') && (ajaxRequest.responseText.length > rq.maxStreamingLength)) { - // Close and reopen connection on large data received - _clearState(); - _doRequest(_buildAjaxRequest(), rq, true); + if (_handleProtocol( _request, message)) { + skipCallbackInvocation = _trackMessageSize(message, rq, _response); + if (!skipCallbackInvocation) { + _invokeCallback(); + } + + _verifyStreamingLength(ajaxRequest, rq); } } }, 0); } - - if (skipCallbackInvocation) { - return; - } } else { if (!_handleProtocol( _request, responseText)) { _reconnect(ajaxRequest, rq, false); return; } - _trackMessageSize(responseText, rq, _response); + skipCallbackInvocation = _trackMessageSize(responseText, rq, _response); rq.lastIndex = responseText.length; } @@ -1575,17 +1609,15 @@ jQuery.atmosphere = function() { jQuery.atmosphere.log(rq.logLevel, ["parent.callback no longer supported with 0.8 version and up. Please upgrade"]); } - _invokeCallback(); + if (!skipCallbackInvocation) { + _invokeCallback(); + } if (rq.executeCallbackBeforeReconnect) { _reconnect(ajaxRequest, rq, false); } - if ((rq.transport == 'streaming') && (responseText.length > rq.maxStreamingLength)) { - // Close and reopen connection on large data received - _clearState(); - _doRequest(_buildAjaxRequest(), rq, true); - } + _verifyStreamingLength(ajaxRequest, rq); } }; ajaxRequest.send(rq.data); @@ -1606,7 +1638,7 @@ jQuery.atmosphere = function() { if (rq.logLevel == 'debug') { jQuery.atmosphere.log(rq.logLevel, ["Max re-connection reached."]); } - _onError(); + _onError(0, "maxRequest reached"); } } @@ -1651,13 +1683,13 @@ jQuery.atmosphere = function() { if (request.trackMessageLength) { ajaxRequest.setRequestHeader("X-Atmosphere-TrackMessageSize", "true") } - - if (request.contentType != '') { - ajaxRequest.setRequestHeader("Content-Type", request.contentType); - } ajaxRequest.setRequestHeader("X-Atmosphere-tracking-id", request.uuid); } + if (request.contentType != '') { + ajaxRequest.setRequestHeader("Content-Type", request.contentType); + } + jQuery.each(request.headers, function(name, value) { var h = jQuery.isFunction(value) ? value.call(this, ajaxRequest, request, create, _response) : value; if (h != null) { @@ -1667,17 +1699,17 @@ jQuery.atmosphere = function() { } function _reconnect(ajaxRequest, request, force) { - var reconnect = request.reconnect && _requestCount++ < request.maxReconnectOnClose; + if (force || request.transport != 'streaming') { + if ( request.reconnect || (request.suspend && _subscribed)) { + var status = ajaxRequest.status > 1000 ? ajaxRequest.status = 0 : ajaxRequest.status; + _response.status = status == 0 ? 204 : status; + _response.reason = status == 0 ? "Server resumed the connection or down." : "OK"; - if (reconnect && force || (request.suspend && ajaxRequest.status == 200 && request.transport != 'streaming' && _subscribed)) { - if (request.reconnect) { _open('re-opening', request.transport, request); request.id = setTimeout(function() { _executeRequest(); }, request.reconnectInterval); } - } else if (!reconnect) { - _onError(); } } @@ -1747,10 +1779,8 @@ jQuery.atmosphere = function() { xdr.onerror = function() { // If the server doesn't send anything back to XDR will fail with polling if (rq.transport != 'polling') { - _prepareCallback(xdr.responseText, "error", 500, transport); + _reconnect(xdr, rq, false); } - - _reconnect(xdr, rq, false); }; // Handles close event @@ -1918,6 +1948,7 @@ jQuery.atmosphere = function() { var text = readResponse(); if (text.length > rq.lastIndex) { _response.status = 200; + _response.error = null; // Empties response every time that it is handled res.innerText = ""; @@ -1938,12 +1969,13 @@ jQuery.atmosphere = function() { return false; } catch (err) { + _response.error = true; if (_requestCount++ < rq.maxReconnectOnClose) { rq.id = setTimeout(function() { _ieStreaming(rq); }, rq.reconnectInterval); } else { - _onError(); + _onError(0, "maxReconnectOnClose reached"); } doc.execCommand("Stop"); doc.close(); @@ -2095,6 +2127,7 @@ jQuery.atmosphere = function() { attachHeadersAsQueryString: true, enableXDR: _request.enableXDR, uuid : _request.uuid, + messageDelimiter : '|', enableProtocol : false, maxReconnectOnClose : _request.maxReconnectOnClose }; @@ -2147,11 +2180,11 @@ jQuery.atmosphere = function() { } function _prepareCallback(messageBody, state, errorCode, transport) { - _response.responseBody = messageBody; + if (state == "messageReceived") { - if (_trackMessageSize(messageBody, _request, _response)) { - return; - } + if (_trackMessageSize(messageBody, _request, _response)) return; + } else { + _response.responseBody = messageBody; } _response.transport = transport; @@ -2242,7 +2275,7 @@ jQuery.atmosphere = function() { _request.reconnect = _request.mrequest; var messages = (typeof(_response.responseBody) == 'string' && _request.trackMessageLength) ? - _response.responseBody.split(_request.messageDelimiter) : new Array(_response.responseBody); + (_response.messages.length>0 ? _response.messages : ['']) : new Array(_response.responseBody); for (var i = 0; i < messages.length; i++) { if (messages.length > 1 && messages[i].length == 0) { @@ -2250,13 +2283,8 @@ jQuery.atmosphere = function() { } _response.responseBody = jQuery.trim(messages[i]); - // Ugly see issue 400. - if (_response.responseBody.length == 0 && _response.transport == 'streaming' && _response.state == "messageReceived") { - var ua = navigator.userAgent.toLowerCase(); - var isAndroid = ua.indexOf("android") > -1; - if (isAndroid) { - continue; - } + if (_response.responseBody.length == 0 && _response.state == "messageReceived") { + continue; } _invokeFunction(_response); @@ -2289,16 +2317,51 @@ jQuery.atmosphere = function() { } /** + * + * @private + */ + function _verifyStreamingLength(ajaxRequest, rq){ + // Wait to be sure we have the full message before closing. + if (_response.partialMessage == "" && + (rq.transport == 'streaming') && + (ajaxRequest.responseText.length > rq.maxStreamingLength)) { + _response.messages = []; + _disconnect(); + _clearState(); + _reconnect(ajaxRequest, rq, true); + } + } + + /** + * Disconnect + * @private + */ + function _disconnect() { + if (_request.enableProtocol) { + var query = "X-Atmosphere-Transport=close&X-Atmosphere-tracking-id=" + _request.uuid; + var url = _request.url.replace(/([?&])_=[^&]*/, query); + url = url + (url === _request.url ? (/\?/.test(_request.url) ? "&" : "?") + query : ""); + + if (_request.connectTimeout > -1) { + jQuery.ajax({url: url, async: false, timeout: _request.connectTimeout}); + } else { + jQuery.ajax({url: url, async: false}); + } + } + } + + /** * Close request. * * @private */ function _close() { - _abordingConnection = true; _request.reconnect = false; + _abordingConnection = true; _response.request = _request; _response.state = 'unsubscribe'; _response.responseBody = ""; + _response.messages = []; _response.status = 408; _invokeCallback(); @@ -2319,7 +2382,9 @@ jQuery.atmosphere = function() { _activeRequest = null; } if (_websocket != null) { - _websocket.close(); + if (_websocket.webSocketOpened) { + _websocket.close(); + } _websocket = null; } if (_sse != null) { @@ -2343,7 +2408,7 @@ jQuery.atmosphere = function() { if (_localStorageService != null) { _localStorageService.close(); } - } + }; this.subscribe = function(options) { _subscribe(options); @@ -2362,6 +2427,10 @@ jQuery.atmosphere = function() { _close(); }; + this.disconnect = function () { + _disconnect(); + }; + this.getUrl = function() { return _request.url; }; @@ -2421,10 +2490,8 @@ jQuery.atmosphere = function() { var requestsClone = [].concat(jQuery.atmosphere.requests); for (var i = 0; i < requestsClone.length; i++) { var rq = requestsClone[i]; + rq.disconnect(); rq.close(); - if (rq.enableProtocol()) { - jQuery.ajax({url: this._closeUrl(rq), async:false}); - } clearTimeout(rq.response.request.id); } } @@ -2432,12 +2499,6 @@ jQuery.atmosphere = function() { jQuery.atmosphere.callbacks = []; }, - _closeUrl : function(rq) { - var query = "X-Atmosphere-Transport=close&X-Atmosphere-tracking-id=" + rq.getUUID(); - var url = rq.getUrl().replace(/([?&])_=[^&]*/, query); - return url + (url === rq.getUrl() ? (/\?/.test(rq.getUrl()) ? "&" : "?") + query : ""); - }, - unsubscribeUrl: function(url) { var idx = -1; if (jQuery.atmosphere.requests.length > 0) { @@ -2446,10 +2507,8 @@ jQuery.atmosphere = function() { // Suppose you can subscribe once to an url if (rq.getUrl() == url) { + rq.disconnect(); rq.close(); - if (rq.enableProtocol()) { - jQuery.ajax({url :this._closeUrl(rq), async:false}); - } clearTimeout(rq.response.request.id); idx = i; break; @@ -2643,8 +2702,8 @@ jQuery.atmosphere = function() { /* * jQuery stringifyJSON * http://github.com/flowersinthesand/jquery-stringifyJSON - * - * Copyright 2011, Donghwan Kim + * + * Copyright 2011, Donghwan Kim * Licensed under the Apache License, Version 2.0 * http://www.apache.org/licenses/LICENSE-2.0 */ @@ -2728,4 +2787,4 @@ jQuery.atmosphere = function() { return str("", {"": value}); }; -}(jQuery));
\ No newline at end of file +}(jQuery)); |