1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731 |
- /**
- * Copyright 2012 Jeanfrancois Arcand
- *
- * 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.
- */
- /*
- * IE streaming/XDR supports is copied/highly inspired by http://code.google.com/p/jquery-stream/
- *
- * Copyright 2011, Donghwan Kim
- * Licensed under the Apache License, Version 2.0
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * LocalStorage supports is copied/highly inspired by https://github.com/flowersinthesand/jquery-socket
- * Copyright 2011, Donghwan Kim
- * Licensed under the Apache License, Version 2.0
- * http://www.apache.org/licenses/LICENSE-2.0
- * */
- /**
- * Official documentation of this library: https://github.com/Atmosphere/atmosphere/wiki/jQuery.atmosphere.js-API
- */
- jQuery.atmosphere = function() {
- jQuery(window).bind("unload.atmosphere", function() {
- jQuery.atmosphere.unsubscribe();
- });
-
- // Prevent ESC to kill the connection from Firefox.
- jQuery(window).keypress(function(e){
- if(e.keyCode == 27){
- e.preventDefault();
- }
- });
-
- var parseHeaders = function(headerString) {
- var match, rheaders = /^(.*?):[ \t]*([^\r\n]*)\r?$/mg, headers = {};
- while (match = rheaders.exec(headerString)) {
- headers[match[1]] = match[2];
- }
- return headers;
- };
-
- return {
- version : "1.0.12",
- requests : [],
- callbacks : [],
-
- onError : function(response) {
- },
- onClose : function(response) {
- },
- onOpen : function(response) {
- },
- onMessage : function(response) {
- },
- onReconnect : function(request, response) {
- },
- onMessagePublished : function(response) {
- },
- onTransportFailure : function (reason, request) {
- },
- onLocalMessage : function (response) {
- },
-
- AtmosphereRequest : function(options) {
-
- /**
- * {Object} Request parameters.
- * @private
- */
- var _request = {
- timeout: 300000,
- method: 'GET',
- headers: {},
- contentType : '',
- callback: null,
- url : '',
- data : '',
- suspend : true,
- maxRequest : -1,
- reconnect : true,
- maxStreamingLength : 10000000,
- lastIndex : 0,
- logLevel : 'info',
- requestCount : 0,
- fallbackMethod: 'GET',
- fallbackTransport : 'streaming',
- transport : 'long-polling',
- webSocketImpl: null,
- webSocketUrl: null,
- webSocketPathDelimiter: "@@",
- enableXDR : false,
- rewriteURL : false,
- attachHeadersAsQueryString : true,
- executeCallbackBeforeReconnect : false,
- readyState : 0,
- lastTimestamp : 0,
- withCredentials : false,
- trackMessageLength : false ,
- messageDelimiter : '|',
- connectTimeout : -1,
- reconnectInterval : 0,
- dropAtmosphereHeaders : true,
- uuid : 0,
- shared : false,
- readResponsesHeaders : true,
- maxReconnectOnClose: 5,
- enableProtocol: false,
- onError : function(response) {
- },
- onClose : function(response) {
- },
- onOpen : function(response) {
- },
- onMessage : function(response) {
- },
- onReconnect : function(request, response) {
- },
- onMessagePublished : function(response) {
- },
- onTransportFailure : function (reason, request) {
- },
- onLocalMessage : function (request) {
- }
- };
-
- /**
- * {Object} Request's last response.
- * @private
- */
- var _response = {
- status: 200,
- responseBody : '',
- headers : [],
- state : "messageReceived",
- transport : "polling",
- error: null,
- request : null,
- partialMessage : "",
- id : 0
- };
-
- /**
- * {websocket} Opened web socket.
- *
- * @private
- */
- var _websocket = null;
-
- /**
- * {SSE} Opened SSE.
- *
- * @private
- */
- var _sse = null;
-
- /**
- * {XMLHttpRequest, ActiveXObject} Opened ajax request (in case of
- * http-streaming or long-polling)
- *
- * @private
- */
- var _activeRequest = null;
-
- /**
- * {Object} Object use for streaming with IE.
- *
- * @private
- */
- var _ieStream = null;
-
- /**
- * {Object} Object use for jsonp transport.
- *
- * @private
- */
- var _jqxhr = null;
-
- /**
- * {boolean} If request has been subscribed or not.
- *
- * @private
- */
- var _subscribed = true;
-
- /**
- * {number} Number of test reconnection.
- *
- * @private
- */
- var _requestCount = 0;
-
- /**
- * {boolean} If request is currently aborded.
- *
- * @private
- */
- var _abordingConnection = false;
-
- /**
- * A local "channel' of communication.
- * @private
- */
- var _localSocketF = null;
-
- /**
- * The storage used.
- * @private
- */
- var _storageService;
-
- /**
- * Local communication
- * @private
- */
- var _localStorageService = null;
-
- /**
- * A Unique ID
- * @private
- */
- var guid = jQuery.now();
-
- /** Trace time */
- var _traceTimer;
-
- // Automatic call to subscribe
- _subscribe(options);
-
- /**
- * Initialize atmosphere request object.
- *
- * @private
- */
- function _init() {
- _subscribed = true;
- _abordingConnection = false;
- _requestCount = 0;
-
- _websocket = null;
- _sse = null;
- _activeRequest = null;
- _ieStream = null;
- }
-
- /**
- * Re-initialize atmosphere object.
- * @private
- */
- function _reinit() {
- _clearState();
- _init();
- }
-
- /**
- * Subscribe request using request transport. <br>
- * If request is currently opened, this one will be closed.
- *
- * @param {Object}
- * Request parameters.
- * @private
- */
- function _subscribe(options) {
- _reinit();
-
- _request = jQuery.extend(_request, options);
- // Allow at least 1 request
- _request.mrequest = _request.reconnect;
- if (!_request.reconnect) {
- _request.reconnect = true;
- }
- }
-
- /**
- * Check if web socket is supported (check for custom implementation
- * provided by request object or browser implementation).
- *
- * @returns {boolean} True if web socket is supported, false
- * otherwise.
- * @private
- */
- function _supportWebsocket() {
- return _request.webSocketImpl != null || window.WebSocket || window.MozWebSocket;
- }
-
- /**
- * Check if server side events (SSE) is supported (check for custom implementation
- * provided by request object or browser implementation).
- *
- * @returns {boolean} True if web socket is supported, false
- * otherwise.
- * @private
- */
- function _supportSSE() {
- return window.EventSource;
- }
-
- /**
- * Open request using request transport. <br>
- * If request transport is 'websocket' but websocket can't be
- * opened, request will automatically reconnect using fallback
- * transport.
- *
- * @private
- */
- function _execute() {
- // Shared across multiple tabs/windows.
- if (_request.shared) {
- _localStorageService = _local(_request);
- if (_localStorageService != null) {
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("Storage service available. All communication will be local");
- }
-
- if (_localStorageService.open(_request)) {
- // Local connection.
- return;
- }
- }
-
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("No Storage service available.");
- }
- // Wasn't local or an error occurred
- _localStorageService = null;
- }
-
- // Protocol
- _request.firstMessage= true;
- _request.ctime = jQuery.now();
-
- if (_request.transport != 'websocket' && _request.transport != 'sse') {
- // Gives a chance to the connection to be established before calling the callback
- setTimeout(function() {
- _open('opening', _request.transport, _request);
- }, 500);
- _executeRequest();
-
- } else if (_request.transport == 'websocket') {
- if (!_supportWebsocket()) {
- _reconnectWithFallbackTransport("Websocket is not supported, using request.fallbackTransport (" + _request.fallbackTransport + ")");
- } else {
- _executeWebSocket(false);
- }
- } else if (_request.transport == 'sse') {
- if (!_supportSSE()) {
- _reconnectWithFallbackTransport("Server Side Events(SSE) is not supported, using request.fallbackTransport (" + _request.fallbackTransport + ")");
- } else {
- _executeSSE(false);
- }
- }
- }
-
- function _local(request) {
- var trace, connector, orphan, name = "atmosphere-" + request.url, connectors = {
- storage: function() {
- if (!jQuery.atmosphere.supportStorage()) {
- return;
- }
-
- var storage = window.localStorage,
- get = function(key) {
- return jQuery.parseJSON(storage.getItem(name + "-" + key));
- },
- set = function(key, value) {
- storage.setItem(name + "-" + key, jQuery.stringifyJSON(value));
- };
-
- return {
- init: function() {
- set("children", get("children").concat([guid]));
- jQuery(window).on("storage.socket", function(event) {
- event = event.originalEvent;
- if (event.key === name && event.newValue) {
- listener(event.newValue);
- }
- });
- return get("opened");
- },
- signal: function(type, data) {
- storage.setItem(name, jQuery.stringifyJSON({target: "p", type: type, data: data}));
- },
- close: function() {
- var index, children = get("children");
-
- jQuery(window).off("storage.socket");
- if (children) {
- index = jQuery.inArray(request.id, children);
- if (index > -1) {
- children.splice(index, 1);
- set("children", children);
- }
- }
- }
- };
- },
- windowref: function() {
- var win = window.open("", name.replace(/\W/g, ""));
-
- if (!win || win.closed || !win.callbacks) {
- return;
- }
-
- return {
- init: function() {
- win.callbacks.push(listener);
- win.children.push(guid);
- return win.opened;
- },
- signal: function(type, data) {
- if (!win.closed && win.fire) {
- win.fire(jQuery.stringifyJSON({target: "p", type: type, data: data}));
- }
- },
- close : function() {
- function remove(array, e) {
- var index = jQuery.inArray(e, array);
- if (index > -1) {
- array.splice(index, 1);
- }
- }
-
- // Removes traces only if the parent is alive
- if (!orphan) {
- remove(win.callbacks, listener);
- remove(win.children, guid);
- }
- }
-
- };
- }
- };
-
- // Receives open, close and message command from the parent
- function listener(string) {
- var command = jQuery.parseJSON(string), data = command.data;
-
- if (command.target === "c") {
- switch (command.type) {
- case "open":
- _open("opening", 'local', _request)
- break;
- case "close":
- if (!orphan) {
- orphan = true;
- if (data.reason === "aborted") {
- _close();
- } else {
- // Gives the heir some time to reconnect
- if (data.heir === guid) {
- _execute();
- } else {
- setTimeout(function() {
- _execute();
- }, 100);
- }
- }
- }
- break;
- case "message":
- _prepareCallback(data, "messageReceived", 200, request.transport);
- break;
- case "localMessage":
- _localMessage(data);
- break;
- }
- }
- }
-
- function findTrace() {
- var matcher = new RegExp("(?:^|; )(" + encodeURIComponent(name) + ")=([^;]*)").exec(document.cookie);
- if (matcher) {
- return jQuery.parseJSON(decodeURIComponent(matcher[2]));
- }
- }
-
- // Finds and validates the parent socket's trace from the cookie
- trace = findTrace();
- if (!trace || jQuery.now() - trace.ts > 1000) {
- return;
- }
-
- // Chooses a connector
- connector = connectors.storage() || connectors.windowref();
- if (!connector) {
- return;
- }
-
- return {
- open: function() {
- var parentOpened;
-
- // Checks the shared one is alive
- _traceTimer = setInterval(function() {
- var oldTrace = trace;
- trace = findTrace();
- if (!trace || oldTrace.ts === trace.ts) {
- // Simulates a close signal
- listener(jQuery.stringifyJSON({target: "c", type: "close", data: {reason: "error", heir: oldTrace.heir}}));
- }
- }, 1000);
-
- parentOpened = connector.init();
- if (parentOpened) {
- // Firing the open event without delay robs the user of the opportunity to bind connecting event handlers
- setTimeout(function() {
- _open("opening", 'local', request)
- }, 50);
- }
- return parentOpened;
- },
- send: function(event) {
- connector.signal("send", event);
- },
- localSend: function(event) {
- connector.signal("localSend", jQuery.stringifyJSON({id: guid , event: event}));
- },
- close: function() {
- // Do not signal the parent if this method is executed by the unload event handler
- if (!_abordingConnection) {
- clearInterval(_traceTimer);
- connector.signal("close");
- connector.close();
- }
- }
- };
- };
-
- function share() {
- var storageService, name = "atmosphere-" + _request.url, servers = {
- // Powered by the storage event and the localStorage
- // http://www.w3.org/TR/webstorage/#event-storage
- storage: function() {
- if (!jQuery.atmosphere.supportStorage()) {
- return;
- }
-
- var storage = window.localStorage;
-
- return {
- init: function() {
- // Handles the storage event
- jQuery(window).on("storage.socket", function(event) {
- event = event.originalEvent;
- // When a deletion, newValue initialized to null
- if (event.key === name && event.newValue) {
- listener(event.newValue);
- }
- });
- },
- signal: function(type, data) {
- storage.setItem(name, jQuery.stringifyJSON({target: "c", type: type, data: data}));
- },
- get: function(key) {
- return jQuery.parseJSON(storage.getItem(name + "-" + key));
- },
- set: function(key, value) {
- storage.setItem(name + "-" + key, jQuery.stringifyJSON(value));
- },
- close : function() {
- jQuery(window).off("storage.socket");
- storage.removeItem(name);
- storage.removeItem(name + "-opened");
- storage.removeItem(name + "-children");
- }
-
- };
- },
- // Powered by the window.open method
- // https://developer.mozilla.org/en/DOM/window.open
- windowref: function() {
- // Internet Explorer raises an invalid argument error
- // when calling the window.open method with the name containing non-word characters
- var neim = name.replace(/\W/g, ""), win = (jQuery('iframe[name="' + neim + '"]')[0]
- || jQuery('<iframe name="' + neim + '" />').hide().appendTo("body")[0]).contentWindow;
-
- return {
- init: function() {
- // Callbacks from different windows
- win.callbacks = [listener];
- // In IE 8 and less, only string argument can be safely passed to the function in other window
- win.fire = function(string) {
- var i;
-
- for (i = 0; i < win.callbacks.length; i++) {
- win.callbacks[i](string);
- }
- };
- },
- signal: function(type, data) {
- if (!win.closed && win.fire) {
- win.fire(jQuery.stringifyJSON({target: "c", type: type, data: data}));
- }
- },
- get: function(key) {
- return !win.closed ? win[key] : null;
- },
- set: function(key, value) {
- if (!win.closed) {
- win[key] = value;
- }
- },
- close : function() {}
- };
- }
- };
-
-
- // Receives send and close command from the children
- function listener(string) {
- var command = jQuery.parseJSON(string), data = command.data;
-
- if (command.target === "p") {
- switch (command.type) {
- case "send":
- _push(data);
- break;
- case "localSend":
- _localMessage(data);
- break;
- case "close":
- _close();
- break;
- }
- }
- }
-
- _localSocketF = function propagateMessageEvent(context) {
- storageService.signal("message", context);
- }
-
- function leaveTrace() {
- document.cookie = encodeURIComponent(name) + "=" +
- // Opera's JSON implementation ignores a number whose a last digit of 0 strangely
- // but has no problem with a number whose a last digit of 9 + 1
- encodeURIComponent(jQuery.stringifyJSON({ts: jQuery.now() + 1, heir: (storageService.get("children") || [])[0]}));
- }
-
- // Chooses a storageService
- storageService = servers.storage() || servers.windowref();
- storageService.init();
-
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("Installed StorageService " + storageService);
- }
-
- // List of children sockets
- storageService.set("children", []);
-
- if (storageService.get("opened") != null && !storageService.get("opened")) {
- // Flag indicating the parent socket is opened
- storageService.set("opened", false);
- }
- // Leaves traces
- leaveTrace();
- _traceTimer = setInterval(leaveTrace, 1000);
-
- _storageService = storageService;
- }
-
- /**
- * @private
- */
- function _open(state, transport, request) {
- if (_request.shared && transport != 'local') {
- share();
- }
-
- if (_storageService != null) {
- _storageService.set("opened", true);
- }
-
- request.close = function() {
- _close();
- };
-
- _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;
-
- _response.state = prevState;
- _response.transport = prevTransport;
- }
-
- /**
- * Execute request using jsonp transport.
- *
- * @param request
- * {Object} request Request parameters, if
- * undefined _request object will be used.
- * @private
- */
- function _jsonp(request) {
- // When CORS is enabled, make sure we force the proper transport.
- request.transport="jsonp";
-
- var rq = _request;
- if ((request != null) && (typeof(request) != 'undefined')) {
- rq = request;
- }
-
- var url = rq.url;
- var data = rq.data;
- if (rq.attachHeadersAsQueryString) {
- url = _attachHeaders(rq);
- if (data != '') {
- url += "&X-Atmosphere-Post-Body=" + encodeURIComponent(data);
- }
- data = '';
- }
-
- _jqxhr = jQuery.ajax({
- url : url,
- type : rq.method,
- dataType: "jsonp",
- error : function(jqXHR, textStatus, errorThrown) {
- if (jqXHR.status < 300) {
- _reconnect(_jqxhr, rq);
- } else {
- _prepareCallback(textStatus, "error", jqXHR.status, rq.transport);
- }
- },
- jsonp : "jsonpTransport",
- success: function(json) {
-
- if (rq.reconnect && (rq.maxRequest == -1 || rq.requestCount++ < rq.maxRequest)) {
- _readHeaders(_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
- }
- }
-
- if (_handleProtocol(rq, msg)) {
- _prepareCallback(msg, "messageReceived", 200, rq.transport);
- }
-
- if (rq.executeCallbackBeforeReconnect) {
- _reconnect(_jqxhr, rq);
- }
- } else {
- jQuery.atmosphere.log(_request.logLevel, ["JSONP reconnect maximum try reached " + _request.requestCount]);
- _onError();
- }
- },
- data : rq.data,
- beforeSend : function(jqXHR) {
- _doRequest(jqXHR, rq, false);
- }
- });
- }
-
- /**
- * Execute request using ajax transport.
- *
- * @param request
- * {Object} request Request parameters, if
- * undefined _request object will be used.
- * @private
- */
- function _ajax(request) {
- var rq = _request;
- if ((request != null) && (typeof(request) != 'undefined')) {
- rq = request;
- }
-
- var url = rq.url;
- var data = rq.data;
- if (rq.attachHeadersAsQueryString) {
- url = _attachHeaders(rq);
- if (data != '') {
- url += "&X-Atmosphere-Post-Body=" + encodeURIComponent(data);
- }
- data = '';
- }
-
- var async = typeof(rq.async) != 'undefined' ? rq.async : true;
- _jqxhr = jQuery.ajax({
- url : url,
- type : rq.method,
- error : function(jqXHR, textStatus, errorThrown) {
- if (jqXHR.status < 300) {
- _reconnect(_jqxhr, rq);
- } else {
- _prepareCallback(textStatus, "error", jqXHR.status, rq.transport);
- }
- },
- success: function(data, textStatus, jqXHR) {
-
- if (rq.reconnect && (rq.maxRequest == -1 || rq.requestCount++ < rq.maxRequest)) {
- if (!rq.executeCallbackBeforeReconnect) {
- _reconnect(_jqxhr, rq);
- }
-
- if (_handleProtocol(rq, data)) {
- _prepareCallback(data, "messageReceived", 200, rq.transport);
- }
-
- if (rq.executeCallbackBeforeReconnect) {
- _reconnect(_jqxhr, rq);
- }
- } else {
- jQuery.atmosphere.log(_request.logLevel, ["AJAX reconnect maximum try reached " + _request.requestCount]);
- _onError();
- }
- },
- beforeSend : function(jqXHR) {
- _doRequest(jqXHR, rq, false);
- },
- crossDomain : rq.enableXDR,
- async: async
- });
- }
-
- /**
- * Build websocket object.
- *
- * @param location
- * {string} Web socket url.
- * @returns {websocket} Web socket object.
- * @private
- */
- function _getWebSocket(location) {
- if (_request.webSocketImpl != null) {
- return _request.webSocketImpl;
- } else {
- if (window.WebSocket) {
- return new WebSocket(location);
- } else {
- return new MozWebSocket(location);
- }
- }
- }
-
- /**
- * Build web socket url from request url.
- *
- * @return {string} Web socket url (start with "ws" or "wss" for
- * secure web socket).
- * @private
- */
- function _buildWebSocketUrl() {
- var url = _attachHeaders(_request);
-
- return decodeURI(jQuery('<a href="' + url + '"/>')[0].href.replace(/^http/, "ws"));
- }
-
- /**
- * Build SSE url from request url.
- *
- * @return a url with Atmosphere's headers
- * @private
- */
- function _buildSSEUrl() {
- var url = _attachHeaders(_request);
- return url;
- }
-
- /**
- * Open SSE. <br>
- * Automatically use fallback transport if SSE can't be
- * opened.
- *
- * @private
- */
- function _executeSSE(sseOpened) {
-
- _response.transport = "sse";
-
- var location = _buildSSEUrl(_request.url);
-
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("Invoking executeSSE");
- jQuery.atmosphere.debug("Using URL: " + location);
- }
-
- if (sseOpened) {
- _open('re-opening', "sse", _request);
- }
-
- if (_request.enableProtocol && sseOpened) {
- var time = jQuery.now() - _request.ctime;
- _request.lastTimestamp = Number(_request.stime) + Number(time);
- }
-
- if (sseOpened && !_request.reconnect) {
- if (_sse != null) {
- _clearState();
- }
- return;
- }
- _sse = new EventSource(location, {withCredentials: _request.withCredentials});
-
- if (_request.connectTimeout > 0) {
- _request.id = setTimeout(function() {
- if (!sseOpened) {
- _clearState();
- }
- }, _request.connectTimeout);
- }
-
- _sse.onopen = function(event) {
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("SSE successfully opened");
- }
-
- if (!sseOpened) {
- _open('opening', "sse", _request);
- }
- sseOpened = true;
-
- if (_request.method == 'POST') {
- _response.state = "messageReceived";
- _sse.send(_request.data);
- }
- };
-
- _sse.onmessage = function(message) {
- if (message.origin != window.location.protocol + "//" + window.location.host) {
- jQuery.atmosphere.log(_request.logLevel, ["Origin was not " + window.location.protocol + "//" + window.location.host]);
- return;
- }
-
- if (!_handleProtocol(_request, message.data)) return;
-
- _response.state = 'messageReceived';
- _response.status = 200;
-
- var message = message.data;
- var skipCallbackInvocation = _trackMessageSize(message, _request, _response);
-
- if (jQuery.trim(message).length == 0) {
- skipCallbackInvocation = true;
- }
-
- if (!skipCallbackInvocation) {
- _invokeCallback();
- _response.responseBody = '';
- }
- };
-
- _sse.onerror = function(message) {
-
- clearTimeout(_request.id);
- _response.state = 'closed';
- _response.responseBody = "";
- _response.status = !sseOpened ? 501 : 200;
- _invokeCallback();
- _clearState();
-
- if (_abordingConnection) {
- jQuery.atmosphere.log(_request.logLevel, ["SSE closed normally"]);
- } else if (!sseOpened) {
- _reconnectWithFallbackTransport("SSE failed. Downgrading to fallback transport and resending");
- } else if (_request.reconnect && (_response.transport == 'sse')) {
- if (_requestCount++ < _request.maxReconnectOnClose) {
- _request.id = setTimeout(function() {
- _executeSSE(true);
- }, _request.reconnectInterval);
- _response.responseBody = "";
- } else {
- jQuery.atmosphere.log(_request.logLevel, ["SSE reconnect maximum try reached " + _requestCount]);
- _onError();
- }
- }
- };
- }
-
- /**
- * Open web socket. <br>
- * Automatically use fallback transport if web socket can't be
- * opened.
- *
- * @private
- */
- function _executeWebSocket(webSocketOpened) {
-
- _response.transport = "websocket";
-
- if (_request.enableProtocol && webSocketOpened) {
- var time = jQuery.now() - _request.ctime;
- _request.lastTimestamp = Number(_request.stime) + Number(time);
- }
-
- var location = _buildWebSocketUrl(_request.url);
- var closed = false;
-
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("Invoking executeWebSocket");
- jQuery.atmosphere.debug("Using URL: " + location);
- }
-
- if (webSocketOpened) {
- _open('re-opening', "websocket", _request);
- }
-
- if (webSocketOpened && !_request.reconnect) {
- if (_websocket != null) {
- _clearState();
- }
- return;
- }
-
- _websocket = _getWebSocket(location);
-
- if (_request.connectTimeout > 0) {
- _request.id = setTimeout(function() {
- if (!webSocketOpened) {
- var _message = {
- code : 1002,
- reason : "",
- wasClean : false
- };
- _websocket.onclose(_message);
- // Close it anyway
- try {
- _clearState();
- } catch (e) {
- }
- return;
- }
-
- }, _request.connectTimeout);
- }
-
- _request.id = setTimeout(function() {
- setTimeout(function () {
- _clearState();
- }, _request.reconnectInterval)
- }, _request.timeout);
-
- _websocket.onopen = function(message) {
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("Websocket successfully opened");
- }
-
- if (!webSocketOpened) {
- _open('opening', "websocket", _request);
- }
-
- webSocketOpened = true;
-
- if (_request.method == 'POST') {
- _response.state = "messageReceived";
- _websocket.send(_request.data);
- }
- };
-
- _websocket.onmessage = function(message) {
-
- clearTimeout(_request.id);
- _request.id = setTimeout(function() {
- setTimeout(function () {
- _clearState();
- }, _request.reconnectInterval)
- }, _request.timeout);
-
- if (!_handleProtocol(_request, message.data)) return;
-
- _response.state = 'messageReceived';
- _response.status = 200;
-
- var message = message.data;
- var skipCallbackInvocation = _trackMessageSize(message, _request, _response);
-
- if (!skipCallbackInvocation) {
- _invokeCallback();
- _response.responseBody = '';
- }
- };
-
- _websocket.onerror = function(message) {
- clearTimeout(_request.id)
- };
-
- _websocket.onclose = function(message) {
- if (closed) return
-
- var reason = message.reason;
- if (reason === "") {
- switch (message.code) {
- case 1000:
- reason = "Normal closure; the connection successfully completed whatever purpose for which " +
- "it was created.";
- break;
- case 1001:
- reason = "The endpoint is going away, either because of a server failure or because the " +
- "browser is navigating away from the page that opened the connection.";
- break;
- case 1002:
- reason = "The endpoint is terminating the connection due to a protocol error.";
- break;
- case 1003:
- reason = "The connection is being terminated because the endpoint received data of a type it " +
- "cannot accept (for example, a text-only endpoint received binary data).";
- break;
- case 1004:
- reason = "The endpoint is terminating the connection because a data frame was received that " +
- "is too large.";
- break;
- case 1005:
- reason = "Unknown: no status code was provided even though one was expected.";
- break;
- case 1006:
- reason = "Connection was closed abnormally (that is, with no close frame being sent).";
- break;
- }
- }
-
- jQuery.atmosphere.warn("Websocket closed, reason: " + reason);
- jQuery.atmosphere.warn("Websocket closed, wasClean: " + message.wasClean);
-
- _response.state = 'closed';
- _response.responseBody = "";
- _response.status = !webSocketOpened ? 501 : 200;
- _invokeCallback();
- clearTimeout(_request.id);
-
- closed = true;
-
- if (_abordingConnection) {
- jQuery.atmosphere.log(_request.logLevel, ["Websocket closed normally"]);
- } else if (!webSocketOpened) {
- _reconnectWithFallbackTransport("Websocket failed. Downgrading to Comet and resending");
-
- } else if (_request.reconnect && _response.transport == 'websocket') {
- _clearState();
- if (_request.reconnect && _requestCount++ < _request.maxReconnectOnClose) {
- _request.id = setTimeout(function() {
- _response.responseBody = "";
- _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();
- }
- }
- };
- }
-
- function _handleProtocol(request, message) {
- // The first messages is always the uuid.
- if (request.enableProtocol && request.firstMessage) {
- request.firstMessage = false;
- var messages = message.split(request.messageDelimiter);
- request.uuid = messages[0];
- request.stime = messages[1];
- return false;
- }
- return true;
- }
-
- function _onError() {
- _clearState();
-
- _response.state = 'error';
- _response.responseBody = "";
- _response.status = 500;
- _invokeCallback();
- }
-
- /**
- * Track received message and make sure callbacks/functions are only invoked when the complete message
- * has been received.
- *
- * @param message
- * @param request
- * @param response
- */
- function _trackMessageSize(message, request, response) {
- if (request.trackMessageLength) {
-
- // If we have found partial message, prepend them.
- if (response.partialMessage.length != 0) {
- message = response.partialMessage + message;
- }
-
- var messages = [];
- var messageLength = 0;
- var messageStart = message.indexOf(request.messageDelimiter);
- while (messageStart != -1) {
- messageLength = message.substring(messageLength, messageStart);
- message = message.substring(messageStart + request.messageDelimiter.length, message.length);
-
- if (message.length == 0 || message.length < messageLength) break;
-
- messageStart = message.indexOf(request.messageDelimiter);
- messages.push(message.substring(0, messageLength));
- }
-
- if (messages.length == 0 || (messageStart != -1 && message.length != 0 && messageLength != message.length)){
- response.partialMessage = messageLength + request.messageDelimiter + message ;
- } else {
- response.partialMessage = "";
- }
-
- if (messages.length != 0) {
- response.responseBody = messages.join(request.messageDelimiter);
- return false;
- } else {
- return true;
- }
- } else {
- response.responseBody = message;
- }
- return false;
- }
-
- /**
- * Reconnect request with fallback transport. <br>
- * Used in case websocket can't be opened.
- *
- * @private
- */
- function _reconnectWithFallbackTransport(errorMessage) {
- jQuery.atmosphere.log(_request.logLevel, [errorMessage]);
-
- if (typeof(_request.onTransportFailure) != 'undefined') {
- _request.onTransportFailure(errorMessage, _request);
- } else if (typeof(jQuery.atmosphere.onTransportFailure) != 'undefined') {
- jQuery.atmosphere.onTransportFailure(errorMessage, _request);
- }
-
- _request.transport = _request.fallbackTransport;
- var reconnect = _request.reconnect && _requestCount++ < _request.maxReconnectOnClose;
- if (reconnect && _request.transport != 'none' || _request.transport == null) {
- _request.method = _request.fallbackMethod;
- _response.transport = _request.fallbackTransport;
- _request.id = setTimeout(function() {
- _execute();
- }, _request.reconnectInterval);
- } else if (!reconnect) {
- _onError();
- }
- }
-
- /**
- * Get url from request and attach headers to it.
- *
- * @param request
- * {Object} request Request parameters, if
- * undefined _request object will be used.
- *
- * @returns {Object} Request object, if undefined,
- * _request object will be used.
- * @private
- */
- function _attachHeaders(request) {
- var rq = _request;
- if ((request != null) && (typeof(request) != 'undefined')) {
- rq = request;
- }
-
- var url = rq.url;
-
- // If not enabled
- if (!rq.attachHeadersAsQueryString) return url;
-
- // If already added
- if (url.indexOf("X-Atmosphere-Framework") != -1) {
- return url;
- }
-
- url += (url.indexOf('?') != -1) ? '&' : '?';
- url += "X-Atmosphere-tracking-id=" + rq.uuid;
- url += "&X-Atmosphere-Framework=" + jQuery.atmosphere.version;
- url += "&X-Atmosphere-Transport=" + rq.transport;
-
- if (rq.trackMessageLength) {
- url += "&X-Atmosphere-TrackMessageSize=" + "true";
- }
-
- if (rq.lastTimestamp != undefined) {
- url += "&X-Cache-Date=" + rq.lastTimestamp;
- } else {
- url += "&X-Cache-Date=" + 0;
- }
-
- if (rq.contentType != '') {
- url += "&Content-Type=" + rq.contentType;
- }
-
- if (rq.enableProtocol) {
- url += "&X-atmo-protocol=true";
- }
-
- jQuery.each(rq.headers, function(name, value) {
- var h = jQuery.isFunction(value) ? value.call(this, rq, request, _response) : value;
- if (h != null) {
- url += "&" + encodeURIComponent(name) + "=" + encodeURIComponent(h);
- }
- });
-
- return url;
- }
-
- /**
- * Build ajax request. <br>
- * Ajax Request is an XMLHttpRequest object, except for IE6 where
- * ajax request is an ActiveXObject.
- *
- * @return {XMLHttpRequest, ActiveXObject} Ajax request.
- * @private
- */
- function _buildAjaxRequest() {
- if (jQuery.browser.msie) {
- if (typeof XMLHttpRequest == "undefined")
- XMLHttpRequest = function () {
- try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
- catch (e) {}
- try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
- catch (e) {}
- try { return new ActiveXObject("Microsoft.XMLHTTP"); }
- catch (e) {}
- //Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
- throw new Error("This browser does not support XMLHttpRequest.");
- };
- }
- return new XMLHttpRequest();
- }
-
- /**
- * Execute ajax request. <br>
- *
- * @param request
- * {Object} request Request parameters, if
- * undefined _request object will be used.
- * @private
- */
- function _executeRequest(request) {
- var rq = _request;
- if ((request != null) || (typeof(request) != 'undefined')) {
- rq = request;
- }
-
- // CORS fake using JSONP
- if ((rq.transport == 'jsonp') || ((rq.enableXDR) && (jQuery.atmosphere.checkCORSSupport()))) {
- _jsonp(rq);
- return;
- }
-
- if (rq.transport == 'ajax') {
- _ajax(request);
- return;
- }
-
- if (jQuery.browser.msie && jQuery.browser.version < 10) {
- if ((rq.transport == 'streaming')) {
- rq.enableXDR && window.XDomainRequest ? _ieXDR(rq) : _ieStreaming(rq);
- return;
- }
-
- if ((rq.enableXDR) && (window.XDomainRequest)) {
- _ieXDR(rq);
- return;
- }
- }
-
- if (rq.reconnect && ( rq.maxRequest == -1 || rq.requestCount++ < rq.maxRequest)) {
- var ajaxRequest = _buildAjaxRequest();
- _doRequest(ajaxRequest, rq, true);
-
- if (rq.suspend) {
- _activeRequest = ajaxRequest;
- }
-
- if (rq.transport != 'polling') {
- _response.transport = rq.transport;
- }
-
- if (!jQuery.browser.msie) {
- ajaxRequest.onerror = function() {
- try {
- _response.status = XMLHttpRequest.status;
- } catch(e) {
- _response.status = 500;
- }
-
- if (!_response.status) {
- _response.status = 500;
- }
- _clearState();
-
- if (rq.reconnect) {
- _reconnect(ajaxRequest, rq, true);
- } else {
- _onError();
- }
- };
- }
-
- ajaxRequest.onreadystatechange = function() {
- if (_abordingConnection) {
- return;
- }
-
- var skipCallbackInvocation = false;
- var update = false;
-
- // Remote server disconnected us, reconnect.
- if (rq.transport == 'streaming'
- && rq.readyState > 2
- && ajaxRequest.readyState == 4) {
-
- rq.readyState = 0;
- rq.lastIndex = 0;
-
- _reconnect(ajaxRequest, rq, true);
- return;
- }
-
- rq.readyState = ajaxRequest.readyState;
-
- if (ajaxRequest.readyState == 4) {
- if (jQuery.browser.msie) {
- update = true;
- } else if (rq.transport == 'streaming') {
- update = true;
- } else if (rq.transport == 'long-polling') {
- update = true;
- clearTimeout(rq.id);
- }
- } else if (rq.transport == 'streaming' && jQuery.browser.msie && ajaxRequest.readyState >= 3) {
- update = true;
- } else if (!jQuery.browser.msie && ajaxRequest.readyState == 3 && ajaxRequest.status == 200 && rq.transport != 'long-polling') {
- update = true;
- } else {
- clearTimeout(rq.id);
- }
-
- if (update) {
- var responseText = ajaxRequest.responseText;
-
- // MSIE status can be higher than 1000, Chrome can be 0
- if (ajaxRequest.status >= 500 || ajaxRequest.status == 0) {
- if (rq.reconnect) {
- _reconnect(ajaxRequest, rq, true);
- } else {
- _onError();
- }
- return;
- }
-
- _readHeaders(ajaxRequest, _request);
-
- if (rq.transport == 'streaming') {
- var text = responseText.substring(rq.lastIndex, responseText.length);
- _response.isJunkEnded = true;
-
- //fix junk is comming in parts
- if (!_response.junkFull && (text.indexOf("<!-- Welcome to the Atmosphere Framework.") == -1 || text.indexOf("<!-- EOD -->") == -1)) {
- return;
- }
- _response.junkFull = true;
-
- //if it's the start and we see the junk start
- //fix for reconnecting on chrome - junk is comming in parts
- if (rq.lastIndex == 0 && text.indexOf("<!-- Welcome to the Atmosphere Framework.") != -1 && text.indexOf("<!-- EOD -->") != -1) {
- _response.isJunkEnded = false;
- }
-
- if (!_response.isJunkEnded) {
- var endOfJunk = "<!-- EOD -->";
- var endOfJunkLength = endOfJunk.length;
- var junkEnd = text.indexOf(endOfJunk) + endOfJunkLength;
-
- if (junkEnd > endOfJunkLength && junkEnd != text.length) {
- _response.responseBody = text.substring(junkEnd);
- rq.lastIndex = responseText.length;
- if (!_handleProtocol( _request, _response.responseBody)) {
- return;
- }
- skipCallbackInvocation = _trackMessageSize(_response.responseBody, rq, _response);
- } else {
- skipCallbackInvocation = true;
- }
- } else {
- var message = responseText.substring(rq.lastIndex, responseText.length);
- rq.lastIndex = responseText.length;
- if (!_handleProtocol( _request, message)) {
- return;
- }
- skipCallbackInvocation = _trackMessageSize(message, rq, _response);
- }
- rq.lastIndex = responseText.length;
-
- if (jQuery.browser.opera) {
- jQuery.atmosphere.iterate(function() {
- if (ajaxRequest.responseText.length > rq.lastIndex) {
- try {
- _response.status = ajaxRequest.status;
- _response.headers = parseHeaders(ajaxRequest.getAllResponseHeaders());
-
- _readHeaders(ajaxRequest, _request);
- }
- catch(e) {
- _response.status = 404;
- }
- _response.state = "messageReceived";
- _response.responseBody = 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);
- }
- }
- }, 0);
- }
-
- if (skipCallbackInvocation) {
- return;
- }
- } else {
- if (!_handleProtocol( _request, responseText)) {
- _reconnect(ajaxRequest, rq, false);
- return;
- }
-
- _trackMessageSize(responseText, rq, _response);
- rq.lastIndex = responseText.length;
- }
-
- try {
- _response.status = ajaxRequest.status;
- _response.headers = parseHeaders(ajaxRequest.getAllResponseHeaders());
-
- _readHeaders(ajaxRequest, rq);
- } catch(e) {
- _response.status = 404;
- }
-
- if (rq.suspend) {
- _response.state = _response.status == 0 ? "closed" : "messageReceived";
- } else {
- _response.state = "messagePublished";
- }
-
- if (!rq.executeCallbackBeforeReconnect) {
- _reconnect(ajaxRequest, rq, false);
- }
-
- // For backward compatibility with Atmosphere < 0.8
- if (_response.responseBody.indexOf("parent.callback") != -1) {
- jQuery.atmosphere.log(rq.logLevel, ["parent.callback no longer supported with 0.8 version and up. Please upgrade"]);
- }
-
- _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);
- }
- }
- };
- ajaxRequest.send(rq.data);
-
- if (rq.suspend) {
- rq.id = setTimeout(function() {
- if (_subscribed) {
- setTimeout(function () {
- _clearState();
- _executeRequest(rq);
- }, rq.reconnectInterval)
- }
- }, rq.timeout);
- }
- _subscribed = true;
-
- } else {
- if (rq.logLevel == 'debug') {
- jQuery.atmosphere.log(rq.logLevel, ["Max re-connection reached."]);
- }
- _onError();
- }
- }
-
- /**
- * Do ajax request.
- * @param ajaxRequest Ajax request.
- * @param request Request parameters.
- * @param create If ajax request has to be open.
- */
- function _doRequest(ajaxRequest, request, create) {
- // Prevent Android to cache request
- var url = _attachHeaders(request);
- url = jQuery.atmosphere.prepareURL(url);
-
- if (create) {
- ajaxRequest.open(request.method, url, true);
- if (request.connectTimeout > -1) {
- request.id = setTimeout(function() {
- if (request.requestCount == 0) {
- _clearState();
- _prepareCallback("Connect timeout", "closed", 200, request.transport);
- }
- }, request.connectTimeout);
- }
- }
-
- if (_request.withCredentials) {
- if ("withCredentials" in ajaxRequest) {
- ajaxRequest.withCredentials = true;
- }
- }
-
- if (!_request.dropAtmosphereHeaders) {
- ajaxRequest.setRequestHeader("X-Atmosphere-Framework", jQuery.atmosphere.version);
- ajaxRequest.setRequestHeader("X-Atmosphere-Transport", request.transport);
- if (request.lastTimestamp != undefined) {
- ajaxRequest.setRequestHeader("X-Cache-Date", request.lastTimestamp);
- } else {
- ajaxRequest.setRequestHeader("X-Cache-Date", 0);
- }
-
- 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);
- }
-
- jQuery.each(request.headers, function(name, value) {
- var h = jQuery.isFunction(value) ? value.call(this, ajaxRequest, request, create, _response) : value;
- if (h != null) {
- ajaxRequest.setRequestHeader(name, h);
- }
- });
- }
-
- function _reconnect(ajaxRequest, request, force) {
- var reconnect = request.reconnect && _requestCount++ < request.maxReconnectOnClose;
-
- 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();
- }
- }
-
- // From jquery-stream, which is APL2 licensed as well.
- function _ieXDR(request) {
- if (request.transport != "polling") {
- _ieStream = _configureXDR(request);
- _ieStream.open();
- } else {
- _configureXDR(request).open();
- }
- }
-
- // From jquery-stream
- function _configureXDR(request) {
- var rq = _request;
- if ((request != null) && (typeof(request) != 'undefined')) {
- rq = request;
- }
-
- var transport = rq.transport;
- var lastIndex = 0;
- var xdrCallback = function (xdr) {
- var responseBody = xdr.responseText;
- var isJunkEnded = false;
-
- if (responseBody.indexOf("<!-- Welcome to the Atmosphere Framework.") != -1) {
- isJunkEnded = true;
- }
-
- if (isJunkEnded) {
- var endOfJunk = "<!-- EOD -->";
- var endOfJunkLenght = endOfJunk.length;
- var junkEnd = responseBody.indexOf(endOfJunk);
- if (junkEnd !== -1) {
- responseBody = responseBody.substring(junkEnd + endOfJunkLenght + lastIndex);
- lastIndex += responseBody.length;
- }
- }
-
- if (!_handleProtocol(request, responseBody)) return;
-
- _prepareCallback(responseBody, "messageReceived", 200, transport);
- };
-
- var xdr = new window.XDomainRequest();
- var rewriteURL = rq.rewriteURL || function(url) {
- // Maintaining session by rewriting URL
- // http://stackoverflow.com/questions/6453779/maintaining-session-by-rewriting-url
- var match = /(?:^|;\s*)(JSESSIONID|PHPSESSID)=([^;]*)/.exec(document.cookie);
-
- switch (match && match[1]) {
- case "JSESSIONID":
- return url.replace(/;jsessionid=[^\?]*|(\?)|$/, ";jsessionid=" + match[2] + "$1");
- case "PHPSESSID":
- return url.replace(/\?PHPSESSID=[^&]*&?|\?|$/, "?PHPSESSID=" + match[2] + "&").replace(/&$/, "");
- }
- return url;
- };
-
- // Handles open and message event
- xdr.onprogress = function() {
- handle(xdr);
- };
-
- // Handles error event
- 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);
- };
-
- // Handles close event
- xdr.onload = function() {
- handle(xdr);
- };
-
- var handle = function (xdr) {
- // XDomain loop forever on itself without this.
- // TODO: Clearly I need to come with something better than that solution
- if (rq.lastMessage == xdr.responseText) return;
-
- if (rq.executeCallbackBeforeReconnect) {
- xdrCallback(xdr);
- }
-
- if (rq.transport == "long-polling" && (rq.reconnect && (rq.maxRequest == -1 || rq.requestCount++ < rq.maxRequest))) {
- xdr.status = 200;
- _reconnect(xdr, rq, false);
- }
-
- if (!rq.executeCallbackBeforeReconnect) {
- xdrCallback(xdr);
- }
- rq.lastMessage = xdr.responseText;
- };
-
- return {
- open: function() {
- if (rq.method == 'POST') {
- rq.attachHeadersAsQueryString = true;
- }
- var url = _attachHeaders(rq);
- if (rq.method == 'POST') {
- url += "&X-Atmosphere-Post-Body=" + encodeURIComponent(rq.data);
- }
- xdr.open(rq.method, rewriteURL(url));
- xdr.send();
- if (rq.connectTimeout > -1) {
- rq.id = setTimeout(function() {
- if (rq.requestCount == 0) {
- _clearState();
- _prepareCallback("Connect timeout", "closed", 200, rq.transport);
- }
- }, rq.connectTimeout);
- }
- },
- close: function() {
- xdr.abort();
- _prepareCallback(xdr.responseText, "closed", 200, transport);
- }
- };
- }
-
- // From jquery-stream, which is APL2 licensed as well.
- function _ieStreaming(request) {
- _ieStream = _configureIE(request);
- _ieStream.open();
- }
-
- function _configureIE(request) {
- var rq = _request;
- if ((request != null) && (typeof(request) != 'undefined')) {
- rq = request;
- }
-
- var stop;
- var doc = new window.ActiveXObject("htmlfile");
-
- doc.open();
- doc.close();
-
- var url = rq.url;
-
- if (rq.transport != 'polling') {
- _response.transport = rq.transport;
- }
-
- return {
- open: function() {
- var iframe = doc.createElement("iframe");
-
- url = _attachHeaders(rq);
- if (rq.data != '') {
- url += "&X-Atmosphere-Post-Body=" + encodeURIComponent(rq.data);
- }
-
- // Finally attach a timestamp to prevent Android and IE caching.
- url = jQuery.atmosphere.prepareURL(url);
-
- iframe.src = url;
- doc.body.appendChild(iframe);
-
- // For the server to respond in a consistent format regardless of user agent, we polls response text
- var cdoc = iframe.contentDocument || iframe.contentWindow.document;
-
- stop = jQuery.atmosphere.iterate(function() {
- try {
- if (!cdoc.firstChild) {
- return;
- }
-
- // Detects connection failure
- if (cdoc.readyState === "complete") {
- try {
- jQuery.noop(cdoc.fileSize);
- } catch(e) {
- _prepareCallback("Connection Failure", "error", 500, rq.transport);
- return false;
- }
- }
-
- var res = cdoc.body ? cdoc.body.lastChild : cdoc;
- var readResponse = function() {
- // Clones the element not to disturb the original one
- var clone = res.cloneNode(true);
-
- // If the last character is a carriage return or a line feed, IE ignores it in the innerText property
- // therefore, we add another non-newline character to preserve it
- clone.appendChild(cdoc.createTextNode("."));
-
- var text = clone.innerText;
- var isJunkEnded = true;
-
- if (text.indexOf("<!-- Welcome to the Atmosphere Framework.") == -1) {
- isJunkEnded = false;
- }
-
- if (isJunkEnded) {
- var endOfJunk = "<!-- EOD -->";
- var endOfJunkLength = endOfJunk.length;
- var junkEnd = text.indexOf(endOfJunk) + endOfJunkLength;
-
- text = text.substring(junkEnd);
- }
-
- text = text.substring(0, text.length - 1);
-
- _handleProtocol(rq, text);
- return text;
-
- };
-
- //To support text/html content type
- if (!jQuery.nodeName(res, "pre")) {
- // Injects a plaintext element which renders text without interpreting the HTML and cannot be stopped
- // it is deprecated in HTML5, but still works
- var head = cdoc.head || cdoc.getElementsByTagName("head")[0] || cdoc.documentElement || cdoc;
- var script = cdoc.createElement("script");
-
- script.text = "document.write('<plaintext>')";
-
- head.insertBefore(script, head.firstChild);
- head.removeChild(script);
-
- // The plaintext element will be the response container
- res = cdoc.body.lastChild;
- }
-
- // Handles open event
- _prepareCallback(readResponse(), "opening", 200, rq.transport);
-
- // Handles message and close event
- stop = jQuery.atmosphere.iterate(function() {
- var text = readResponse();
- if (text.length > rq.lastIndex) {
- _response.status = 200;
-
- // Empties response every time that it is handled
- res.innerText = "";
- _prepareCallback(text, "messageReceived", 200, rq.transport);
-
- rq.lastIndex = 0;
- }
-
- if (cdoc.readyState === "complete") {
- _prepareCallback("", "closed", 200, rq.transport);
- _prepareCallback("", "re-opening", 200, rq.transport);
- rq.id = setTimeout(function() {
- _ieStreaming(rq);
- }, rq.reconnectInterval);
- return false;
- }
- }, null);
-
- return false;
- } catch (err) {
- if (_requestCount++ < rq.maxReconnectOnClose) {
- rq.id = setTimeout(function() {
- _ieStreaming(rq);
- }, rq.reconnectInterval);
- } else {
- _onError();
- }
- doc.execCommand("Stop");
- doc.close();
- return false;
- }
- });
- },
-
- close: function() {
- if (stop) {
- stop();
- }
-
- doc.execCommand("Stop");
- _prepareCallback("", "closed", 200, rq.transport);
- }
- };
- }
-
- /*
- * Send message. <br>
- * Will be automatically dispatch to other connected.
- *
- * @param {Object,string} Message to send.
- * @private
- */
- function _push(message) {
-
- if (_response.status == 408) {
- _pushOnClose(message);
- } else if (_localStorageService != null) {
- _pushLocal(message);
- } else if (_activeRequest != null || _sse != null) {
- _pushAjaxMessage(message);
- } else if (_ieStream != null) {
- _pushIE(message);
- } else if (_jqxhr != null) {
- _pushJsonp(message);
- } else if (_websocket != null) {
- _pushWebSocket(message);
- }
- }
-
- function _pushOnClose(message) {
- var rq = _getPushRequest(message);
- rq.transport = "ajax";
- rq.method = "GET";
- rq.async = false;
- rq.reconnect = false;
- _executeRequest(rq);
- }
-
- function _pushLocal(message) {
- _localStorageService.send(message);
- }
-
- function _intraPush(message) {
- // IE 9 will crash if not.
- if (message.length == 0) return;
-
- try {
- if (_localStorageService) {
- _localStorageService.localSend(message);
- } else if (_storageService) {
- _storageService.signal("localMessage", jQuery.stringifyJSON({id: guid , event: message}));
- }
- } catch (err) {
- jQuery.atmosphere.error(err);
- }
- }
-
- /**
- * Send a message using currently opened ajax request (using
- * http-streaming or long-polling). <br>
- *
- * @param {string, Object} Message to send. This is an object, string
- * message is saved in data member.
- * @private
- */
- function _pushAjaxMessage(message) {
- var rq = _getPushRequest(message);
- _executeRequest(rq);
- }
-
- /**
- * Send a message using currently opened ie streaming (using
- * http-streaming or long-polling). <br>
- *
- * @param {string, Object} Message to send. This is an object, string
- * message is saved in data member.
- * @private
- */
- function _pushIE(message) {
- if (_request.enableXDR && jQuery.atmosphere.checkCORSSupport()) {
- var rq = _getPushRequest(message);
- // Do not reconnect since we are pushing.
- rq.reconnect = false;
- _jsonp(rq);
- } else {
- _pushAjaxMessage(message);
- }
- }
-
- /**
- * Send a message using jsonp transport. <br>
- *
- * @param {string, Object} Message to send. This is an object, string
- * message is saved in data member.
- * @private
- */
- function _pushJsonp(message) {
- _pushAjaxMessage(message);
- }
-
- function _getStringMessage(message) {
- var msg = message;
- if (typeof(msg) == 'object') {
- msg = message.data;
- }
- return msg;
- }
-
- /**
- * Build request use to push message using method 'POST' <br>.
- * Transport is defined as 'polling' and 'suspend' is set to false.
- *
- * @return {Object} Request object use to push message.
- * @private
- */
- function _getPushRequest(message) {
- var msg = _getStringMessage(message);
-
- var rq = {
- connected: false,
- timeout: 60000,
- method: 'POST',
- url: _request.url,
- contentType : _request.contentType,
- headers: {},
- reconnect : true,
- callback: null,
- data : msg,
- suspend : false,
- maxRequest : -1,
- logLevel : 'info',
- requestCount : 0,
- withCredentials : _request.withCredentials,
- transport: 'polling',
- attachHeadersAsQueryString: true,
- enableXDR: _request.enableXDR,
- uuid : _request.uuid,
- enableProtocol : false,
- maxReconnectOnClose : _request.maxReconnectOnClose
- };
-
- if (typeof(message) == 'object') {
- rq = jQuery.extend(rq, message);
- }
-
- return rq;
- }
-
- /**
- * Send a message using currently opened websocket. <br>
- *
- */
- function _pushWebSocket(message) {
- var msg = _getStringMessage(message);
- var data;
- try {
- if (_request.webSocketUrl != null) {
- data = _request.webSocketPathDelimiter
- + _request.webSocketUrl
- + _request.webSocketPathDelimiter
- + msg;
- } else {
- data = msg;
- }
-
- _websocket.send(data);
-
- } catch (e) {
- _websocket.onclose = function(message) {
- };
- _clearState();
-
- _reconnectWithFallbackTransport("Websocket failed. Downgrading to Comet and resending " + data);
- _pushAjaxMessage(message);
- }
- }
-
- function _localMessage(message) {
- var m = jQuery.parseJSON(message);
- if (m.id != guid) {
- if (typeof(_request.onLocalMessage) != 'undefined') {
- _request.onLocalMessage(m.event);
- } else if (typeof(jQuery.atmosphere.onLocalMessage) != 'undefined') {
- jQuery.atmosphere.onLocalMessage(m.event);
- }
- }
- }
-
- function _prepareCallback(messageBody, state, errorCode, transport) {
- _response.responseBody = messageBody;
- if (state == "messageReceived") {
- if (_trackMessageSize(messageBody, _request, _response)) {
- return;
- }
- }
-
- _response.transport = transport;
- _response.status = errorCode;
- _response.state = state;
-
- _invokeCallback();
- }
-
- function _readHeaders(xdr, request) {
- if (!request.readResponsesHeaders && !request.enableProtocol) {
- request.lastTimestamp = jQuery.now();
- request.uuid = jQuery.atmosphere.guid();
- return;
- }
-
- try {
- var tempDate = xdr.getResponseHeader('X-Cache-Date');
- if (tempDate && tempDate != null && tempDate.length > 0 ) {
- request.lastTimestamp = tempDate.split(" ").pop();
- }
-
- var tempUUID = xdr.getResponseHeader('X-Atmosphere-tracking-id');
- if (tempUUID && tempUUID != null) {
- request.uuid = tempUUID.split(" ").pop();
- }
-
- // HOTFIX for firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=608735
- if (request.headers) {
- jQuery.each(_request.headers, function (name) {
- var v = xdr.getResponseHeader(name);
- if (v) {
- _response.headers[name] = v;
- }
- });
- }
- } catch (e) {
- }
- }
-
- function _invokeFunction(response) {
- _f(response, _request);
- // Global
- _f(response, jQuery.atmosphere);
- }
-
- function _f(response, f) {
- switch (response.state) {
- case "messageReceived" :
- _requestCount = 0;
- if (typeof(f.onMessage) != 'undefined') f.onMessage(response);
- break;
- case "error" :
- if (typeof(f.onError) != 'undefined') f.onError(response);
- break;
- case "opening" :
- if (typeof(f.onOpen) != 'undefined') f.onOpen(response);
- break;
- case "messagePublished" :
- if (typeof(f.onMessagePublished) != 'undefined') f.onMessagePublished(response);
- break;
- case "re-opening" :
- if (typeof(f.onReconnect) != 'undefined') f.onReconnect(_request, response);
- break;
- case "unsubscribe" :
- case "closed" :
- var closed = typeof(_request.closed) != 'undefined' ? _request.closed : false;
- if (typeof(f.onClose) != 'undefined' && !closed) f.onClose(response);
- _request.closed = true;
- break;
- }
- }
-
- /**
- * Invoke request callbacks.
- *
- * @private
- */
- function _invokeCallback() {
- var call = function (index, func) {
- func(_response);
- };
-
- if (_localStorageService == null && _localSocketF != null) {
- _localSocketF(_response.responseBody);
- }
-
- _request.reconnect = _request.mrequest;
-
- var messages = (typeof(_response.responseBody) == 'string' && _request.trackMessageLength) ?
- _response.responseBody.split(_request.messageDelimiter) : new Array(_response.responseBody);
- for (var i = 0; i < messages.length; i++) {
-
- if (messages.length > 1 && messages[i].length == 0) {
- continue;
- }
- _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;
- }
- }
-
- _invokeFunction(_response);
-
- // Invoke global callbacks
- if (jQuery.atmosphere.callbacks.length > 0) {
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("Invoking " + jQuery.atmosphere.callbacks.length + " global callbacks: " + _response.state);
- }
- try {
- jQuery.each(jQuery.atmosphere.callbacks, call);
- } catch (e) {
- jQuery.atmosphere.log(_request.logLevel, ["Callback exception" + e]);
- }
- }
-
- // Invoke request callback
- if (typeof(_request.callback) == 'function') {
- if (_request.logLevel == 'debug') {
- jQuery.atmosphere.debug("Invoking request callbacks");
- }
- try {
- _request.callback(_response);
- } catch (e) {
- jQuery.atmosphere.log(_request.logLevel, ["Callback exception" + e]);
- }
- }
- }
-
- }
-
- /**
- * Close request.
- *
- * @private
- */
- function _close() {
- _abordingConnection = true;
- _request.reconnect = false;
- _response.request = _request;
- _response.state = 'unsubscribe';
- _response.responseBody = "";
- _response.status = 408;
- _invokeCallback();
-
- _clearState();
- }
-
- function _clearState() {
- if (_ieStream != null) {
- _ieStream.close();
- _ieStream = null;
- }
- if (_jqxhr != null) {
- _jqxhr.abort();
- _jqxhr = null;
- }
- if (_activeRequest != null) {
- _activeRequest.abort();
- _activeRequest = null;
- }
- if (_websocket != null) {
- _websocket.close();
- _websocket = null;
- }
- if (_sse != null) {
- _sse.close();
- _sse = null;
- }
- _clearStorage();
- }
-
- function _clearStorage() {
- // Stop sharing a connection
- if (_storageService != null) {
- // Clears trace timer
- clearInterval(_traceTimer);
- // Removes the trace
- document.cookie = encodeURIComponent("atmosphere-" + _request.url) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
- // The heir is the parent unless unloading
- _storageService.signal("close", {reason: "", heir: !_abordingConnection ? guid : (_storageService.get("children") || [])[0]});
- _storageService.close();
- }
- if (_localStorageService != null) {
- _localStorageService.close();
- }
- }
-
- this.subscribe = function(options) {
- _subscribe(options);
- _execute();
- };
-
- this.execute = function() {
- _execute();
- };
-
- this.invokeCallback = function() {
- _invokeCallback();
- };
-
- this.close = function() {
- _close();
- };
-
- this.getUrl = function() {
- return _request.url;
- };
-
- this.getUUID = function() {
- return _request.uuid;
- };
-
- this.push = function(message) {
- _push(message);
- };
-
- this.pushLocal = function(message) {
- _intraPush(message);
- };
-
- this.enableProtocol = function(message) {
- return _request.enableProtocol;
- };
-
- this.response = _response;
- },
-
- subscribe: function(url, callback, request) {
- if (typeof(callback) == 'function') {
- jQuery.atmosphere.addCallback(callback);
- }
-
- if (typeof(url) != "string") {
- request = url;
- } else {
- request.url = url;
- }
-
- var rq = new jQuery.atmosphere.AtmosphereRequest(request);
- rq.execute();
-
- jQuery.atmosphere.requests[jQuery.atmosphere.requests.length] = rq;
- return rq;
- },
-
- addCallback: function(func) {
- if (jQuery.inArray(func, jQuery.atmosphere.callbacks) == -1) {
- jQuery.atmosphere.callbacks.push(func);
- }
- },
-
- removeCallback: function(func) {
- var index = jQuery.inArray(func, jQuery.atmosphere.callbacks);
- if (index != -1) {
- jQuery.atmosphere.callbacks.splice(index, 1);
- }
- },
-
- unsubscribe : function() {
- if (jQuery.atmosphere.requests.length > 0) {
- var requestsClone = [].concat(jQuery.atmosphere.requests);
- for (var i = 0; i < requestsClone.length; i++) {
- var rq = requestsClone[i];
- rq.close();
- if (rq.enableProtocol()) {
- jQuery.ajax({url: this._closeUrl(rq), async:false});
- }
- clearTimeout(rq.response.request.id);
- }
- }
- jQuery.atmosphere.requests = [];
- 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) {
- for (var i = 0; i < jQuery.atmosphere.requests.length; i++) {
- var rq = jQuery.atmosphere.requests[i];
-
- // Suppose you can subscribe once to an url
- if (rq.getUrl() == url) {
- rq.close();
- if (rq.enableProtocol()) {
- jQuery.ajax({url :this._closeUrl(rq), async:false});
- }
- clearTimeout(rq.response.request.id);
- idx = i;
- break;
- }
- }
- }
- if (idx >= 0) {
- jQuery.atmosphere.requests.splice(idx, 1);
- }
- },
-
- publish: function(request) {
- if (typeof(request.callback) == 'function') {
- jQuery.atmosphere.addCallback(callback);
- }
- request.transport = "polling";
-
- var rq = new jQuery.atmosphere.AtmosphereRequest(request);
- jQuery.atmosphere.requests[jQuery.atmosphere.requests.length] = rq;
- return rq;
- },
-
- checkCORSSupport : function() {
- if (jQuery.browser.msie && !window.XDomainRequest) {
- return true;
- } else if (jQuery.browser.opera && jQuery.browser.version < 12.0) {
- return true;
- }
-
- // Force Android to use CORS as some version like 2.2.3 fail otherwise
- var ua = navigator.userAgent.toLowerCase();
- var isAndroid = ua.indexOf("android") > -1;
- if (isAndroid) {
- return true;
- }
- return false;
- },
-
- S4 : function() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
- },
-
- guid : function() {
- return (jQuery.atmosphere.S4() + jQuery.atmosphere.S4() + "-" + jQuery.atmosphere.S4() + "-" + jQuery.atmosphere.S4() + "-" + jQuery.atmosphere.S4() + "-" + jQuery.atmosphere.S4() + jQuery.atmosphere.S4() + jQuery.atmosphere.S4());
- },
-
- // From jQuery-Stream
- prepareURL: function(url) {
- // Attaches a time stamp to prevent caching
- var ts = jQuery.now();
- var ret = url.replace(/([?&])_=[^&]*/, "$1_=" + ts);
-
- return ret + (ret === url ? (/\?/.test(url) ? "&" : "?") + "_=" + ts : "");
- },
-
- // From jQuery-Stream
- param : function(data) {
- return jQuery.param(data, jQuery.ajaxSettings.traditional);
- },
-
- supportStorage : function() {
- var storage = window.localStorage;
- if (storage) {
- try {
- storage.setItem("t", "t");
- storage.removeItem("t");
- // The storage event of Internet Explorer and Firefox 3 works strangely
- return window.StorageEvent && !jQuery.browser.msie && !(jQuery.browser.mozilla && jQuery.browser.version.split(".")[0] === "1");
- } catch (e) {
- }
- }
-
- return false;
- },
-
- iterate : function (fn, interval) {
- var timeoutId;
-
- // Though the interval is 0 for real-time application, there is a delay between setTimeout calls
- // For detail, see https://developer.mozilla.org/en/window.setTimeout#Minimum_delay_and_timeout_nesting
- interval = interval || 0;
-
- (function loop() {
- timeoutId = setTimeout(function() {
- if (fn() === false) {
- return;
- }
-
- loop();
- }, interval);
- })();
-
- return function() {
- clearTimeout(timeoutId);
- };
- },
-
- log: function (level, args) {
- if (window.console) {
- var logger = window.console[level];
- if (typeof logger == 'function') {
- logger.apply(window.console, args);
- }
- }
- },
-
- warn: function() {
- jQuery.atmosphere.log('warn', arguments);
- },
-
- info :function() {
- jQuery.atmosphere.log('info', arguments);
- },
-
- debug: function() {
- jQuery.atmosphere.log('debug', arguments);
- },
-
- error: function() {
- jQuery.atmosphere.log('error', arguments);
- }
- };
- }();
-
- // http://stackoverflow.com/questions/9645803/whats-the-replacement-for-browser
- // Limit scope pollution from any deprecated API
- (function () {
-
- var matched, browser;
-
- // Use of jQuery.browser is frowned upon.
- // More details: http://api.jquery.com/jQuery.browser
- // jQuery.uaMatch maintained for back-compat
- jQuery.uaMatch = function (ua) {
- ua = ua.toLowerCase();
-
- var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
- /(webkit)[ \/]([\w.]+)/.exec(ua) ||
- /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
- /(msie) ([\w.]+)/.exec(ua) ||
- ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
- [];
-
- return {
- browser: match[ 1 ] || "",
- version: match[ 2 ] || "0"
- };
- };
-
- matched = jQuery.uaMatch(navigator.userAgent);
- browser = {};
-
- if (matched.browser) {
- browser[ matched.browser ] = true;
- browser.version = matched.version;
- }
-
- // Chrome is Webkit, but Webkit is also Safari.
- if (browser.chrome) {
- browser.webkit = true;
- } else if (browser.webkit) {
- browser.safari = true;
- }
-
- jQuery.browser = browser;
-
- jQuery.sub = function () {
- function jQuerySub(selector, context) {
- return new jQuerySub.fn.init(selector, context);
- }
-
- jQuery.extend(true, jQuerySub, this);
- jQuerySub.superclass = this;
- jQuerySub.fn = jQuerySub.prototype = this();
- jQuerySub.fn.constructor = jQuerySub;
- jQuerySub.sub = this.sub;
- jQuerySub.fn.init = function init(selector, context) {
- if (context && context instanceof jQuery && !(context instanceof jQuerySub)) {
- context = jQuerySub(context);
- }
-
- return jQuery.fn.init.call(this, selector, context, rootjQuerySub);
- };
- jQuerySub.fn.init.prototype = jQuerySub.fn;
- var rootjQuerySub = jQuerySub(document);
- return jQuerySub;
- };
-
- })();
-
- /*
- * jQuery stringifyJSON
- * http://github.com/flowersinthesand/jquery-stringifyJSON
- *
- * Copyright 2011, Donghwan Kim
- * Licensed under the Apache License, Version 2.0
- * http://www.apache.org/licenses/LICENSE-2.0
- */
- // This plugin is heavily based on Douglas Crockford's reference implementation
- (function(jQuery) {
-
- var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, meta = {
- '\b' : '\\b',
- '\t' : '\\t',
- '\n' : '\\n',
- '\f' : '\\f',
- '\r' : '\\r',
- '"' : '\\"',
- '\\' : '\\\\'
- };
-
- function quote(string) {
- return '"' + string.replace(escapable, function(a) {
- var c = meta[a];
- return typeof c === "string" ? c : "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
- }) + '"';
- }
-
- function f(n) {
- return n < 10 ? "0" + n : n;
- }
-
- function str(key, holder) {
- var i, v, len, partial, value = holder[key], type = typeof value;
-
- if (value && typeof value === "object" && typeof value.toJSON === "function") {
- value = value.toJSON(key);
- type = typeof value;
- }
-
- switch (type) {
- case "string":
- return quote(value);
- case "number":
- return isFinite(value) ? String(value) : "null";
- case "boolean":
- return String(value);
- case "object":
- if (!value) {
- return "null";
- }
-
- switch (Object.prototype.toString.call(value)) {
- case "[object Date]":
- return isFinite(value.valueOf()) ? '"' + value.getUTCFullYear() + "-" + f(value.getUTCMonth() + 1) + "-" + f(value.getUTCDate()) + "T" +
- f(value.getUTCHours()) + ":" + f(value.getUTCMinutes()) + ":" + f(value.getUTCSeconds()) + "Z" + '"' : "null";
- case "[object Array]":
- len = value.length;
- partial = [];
- for (i = 0; i < len; i++) {
- partial.push(str(i, value) || "null");
- }
-
- return "[" + partial.join(",") + "]";
- default:
- partial = [];
- for (i in value) {
- if (Object.prototype.hasOwnProperty.call(value, i)) {
- v = str(i, value);
- if (v) {
- partial.push(quote(i) + ":" + v);
- }
- }
- }
-
- return "{" + partial.join(",") + "}";
- }
- }
- }
-
- jQuery.stringifyJSON = function(value) {
- if (window.JSON && window.JSON.stringify) {
- return window.JSON.stringify(value);
- }
-
- return str("", {"": value});
- };
-
- }(jQuery));
|