/** * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ jQuery.atmosphere = function() { var activeRequest; $(window).unload(function() { if (activeRequest) activeRequest.abort(); }); return { version : 0.7, response : { status: 200, responseBody : '', headers : [], state : "messageReceived", transport : "polling", push : [], error: null, id : 0 }, request : {}, logLevel : 'info', callbacks: [], activeTransport : null, websocket : null, killHiddenIFrame : null, subscribe: function(url, callback, request) { jQuery.atmosphere.request = jQuery.extend({ timeout: 300000, method: 'GET', headers: {}, contentType : "text/html;charset=ISO-8859-1", cache: true, async: true, ifModified: false, callback: null, dataType: '', url : url, data : '', suspend : true, maxRequest : 60, lastIndex : 0, logLevel : 'info', requestCount : 0, fallbackTransport : 'streaming', transport : 'long-polling' }, request); logLevel = jQuery.atmosphere.request.logLevel || 'info'; if (callback != null) { jQuery.atmosphere.addCallback(callback); jQuery.atmosphere.request.callback = callback; } if (jQuery.atmosphere.request.transport != jQuery.atmosphere.activeTransport) { jQuery.atmosphere.closeSuspendedConnection(); } jQuery.atmosphere.activeTransport = jQuery.atmosphere.request.transport; if (jQuery.atmosphere.request.transport != 'websocket') { jQuery.atmosphere.executeRequest(); } else if (jQuery.atmosphere.request.transport == 'websocket') { if (!window.WebSocket) { jQuery.atmosphere.log(logLevel, ["Websocket is not supported, using request.fallbackTransport"]); jQuery.atmosphere.request.transport = jQuery.atmosphere.request.fallbackTransport; jQuery.atmosphere.executeRequest(); } else { jQuery.atmosphere.executeWebSocket(); } } }, /** * Always make sure one transport is used, not two at the same time except for Websocket. */ closeSuspendedConnection : function () { if (activeRequest != null) { activeRequest.abort(); } if (jQuery.atmosphere.websocket != null) { jQuery.atmosphere.websocket.close(); jQuery.atmosphere.websocket = null; } }, executeRequest: function() { if (jQuery.atmosphere.request.transport == 'streaming') { if ($.browser.msie) { jQuery.atmosphere.ieStreaming(); return; } else if ((typeof window.addEventStream) == 'function') { jQuery.atmosphere.operaStreaming(); return; } } if (jQuery.atmosphere.request.requestCount++ < jQuery.atmosphere.request.maxRequest) { jQuery.atmosphere.response.push = function (url) { jQuery.atmosphere.request.callback = null; jQuery.atmosphere.publish(url, null, jQuery.atmosphere.request); }; var request = jQuery.atmosphere.request; var response = jQuery.atmosphere.response; if (request.transport != 'polling') { response.transport = request.transport; } var ajaxRequest; var error = false; if ($.browser.msie) { var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] for (var i = 0; i < activexmodes.length; i++) { try { ajaxRequest = new ActiveXObject(activexmodes[i]) } catch(e) { } } } else if (window.XMLHttpRequest) { ajaxRequest = new XMLHttpRequest(); } if (request.suspend) { activeRequest = ajaxRequest; } ajaxRequest.open(request.method, request.url, true); ajaxRequest.setRequestHeader("X-Atmosphere-Framework", jQuery.atmosphere.version); ajaxRequest.setRequestHeader("X-Atmosphere-Transport", request.transport); ajaxRequest.setRequestHeader("X-Cache-Date", new Date()); if (!$.browser.msie) { ajaxRequest.onerror = function() { error = true; try { response.status = XMLHttpRequest.status; } catch(e) { response.status = 404; } response.state = "error"; jQuery.atmosphere.invokeCallback(response); ajaxRequest.abort(); activeRequest = null; } } ajaxRequest.onreadystatechange = function() { var junkForWebkit = false; var update = false; if (ajaxRequest.readyState == 4) { jQuery.atmosphere.request = request; if (request.suspend && ajaxRequest.status == 200) { jQuery.atmosphere.executeRequest(); } if ($.browser.msie) { update = true; } } else if (!$.browser.msie && ajaxRequest.readyState == 3 && ajaxRequest.status == 200) { update = true; } else { clearTimeout(request.id); } if (update) { if (request.transport == 'streaming') { response.responseBody = ajaxRequest.responseText.substring(request.lastIndex, ajaxRequest.responseText.length); request.lastIndex = ajaxRequest.responseText.length; if (response.responseBody.indexOf("