atmosphere-min.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. (function(a,b){if(typeof define==="function"&&define.amd){define(b)
  2. }else{if(typeof exports!=="undefined"){module.exports=b()
  3. }else{a.atmosphere=b()
  4. }}}(this,function(){var a={},d,c=false,g=[],f=[],e=0,b=Object.prototype.hasOwnProperty;
  5. a={version:"2.3.6-javascript",onError:function(h){},onClose:function(h){},onOpen:function(h){},onReopen:function(h){},onMessage:function(h){},onReconnect:function(i,h){},onMessagePublished:function(h){},onTransportFailure:function(i,h){},onLocalMessage:function(h){},onFailureToReconnect:function(i,h){},onClientTimeout:function(h){},onOpenAfterResume:function(h){},WebsocketApiAdapter:function(i){var h,j;
  6. i.onMessage=function(k){j.onmessage({data:k.responseBody})
  7. };
  8. i.onMessagePublished=function(k){j.onmessage({data:k.responseBody})
  9. };
  10. i.onOpen=function(k){j.onopen(k)
  11. };
  12. j={close:function(){h.close()
  13. },send:function(k){h.push(k)
  14. },onmessage:function(k){},onopen:function(k){},onclose:function(k){},onerror:function(k){}};
  15. h=new a.subscribe(i);
  16. return j
  17. },AtmosphereRequest:function(ad){var p={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,webSocketBinaryType:null,dispatchUrl:null,webSocketPathDelimiter:"@@",enableXDR:false,rewriteURL:false,attachHeadersAsQueryString:true,executeCallbackBeforeReconnect:false,readyState:0,withCredentials:false,trackMessageLength:false,messageDelimiter:"|",connectTimeout:-1,reconnectInterval:0,dropHeaders:true,uuid:0,async:true,shared:false,readResponsesHeaders:false,maxReconnectOnClose:5,enableProtocol:true,disableDisconnect:false,pollingInterval:0,heartbeat:{client:null,server:null},ackInterval:0,closeAsync:false,reconnectOnServerError:true,handleOnlineOffline:true,maxWebsocketErrorRetries:1,curWebsocketErrorRetries:0,onError:function(aI){},onClose:function(aI){},onOpen:function(aI){},onMessage:function(aI){},onReopen:function(aJ,aI){},onReconnect:function(aJ,aI){},onMessagePublished:function(aI){},onTransportFailure:function(aJ,aI){},onLocalMessage:function(aI){},onFailureToReconnect:function(aJ,aI){},onClientTimeout:function(aI){},onOpenAfterResume:function(aI){}};
  18. var ar={status:200,reasonPhrase:"OK",responseBody:"",messages:[],headers:[],state:"messageReceived",transport:"polling",error:null,request:null,partialMessage:"",errorHandled:false,closedByClientTimeout:false,ffTryingReconnect:false};
  19. var aw=null;
  20. var ag=null;
  21. var z=null;
  22. var n=null;
  23. var X=null;
  24. var u=true;
  25. var ay=0;
  26. var J=0;
  27. var ak="X";
  28. var aG=false;
  29. var Q=null;
  30. var h;
  31. var ax=null;
  32. var R=a.util.now();
  33. var y;
  34. var aF;
  35. var Y=false;
  36. ao(ad);
  37. function aj(){u=true;
  38. aG=false;
  39. ay=0;
  40. aw=null;
  41. ag=null;
  42. z=null;
  43. n=null
  44. }function U(){l();
  45. aj()
  46. }function w(aI){if(aI=="debug"){return p.logLevel==="debug"
  47. }else{if(aI=="info"){return p.logLevel==="info"||p.logLevel==="debug"
  48. }else{if(aI=="warn"){return p.logLevel==="warn"||p.logLevel==="info"||p.logLevel==="debug"
  49. }else{if(aI=="error"){return p.logLevel==="error"||p.logLevel==="warn"||p.logLevel==="info"||p.logLevel==="debug"
  50. }else{return false
  51. }}}}}function aH(aI){if(w("debug")){a.util.debug(new Date()+" Atmosphere: "+aI)
  52. }}function I(aJ,aI){if(ar.partialMessage===""&&(aI.transport==="streaming")&&(aJ.responseText.length>aI.maxStreamingLength)){return true
  53. }return false
  54. }function D(){if(p.enableProtocol&&!p.disableDisconnect&&!p.firstMessage){var aK="X-Atmosphere-Transport=close&X-Atmosphere-tracking-id="+p.uuid;
  55. a.util.each(p.headers,function(aM,aO){var aN=a.util.isFunction(aO)?aO.call(this,p,p,ar):aO;
  56. if(aN!=null){aK+="&"+encodeURIComponent(aM)+"="+encodeURIComponent(aN)
  57. }});
  58. var aI=p.url.replace(/([?&])_=[^&]*/,aK);
  59. aI=aI+(aI===p.url?(/\?/.test(p.url)?"&":"?")+aK:"");
  60. var aJ={connected:false};
  61. var aL=new a.AtmosphereRequest(aJ);
  62. aL.connectTimeout=p.connectTimeout;
  63. aL.attachHeadersAsQueryString=false;
  64. aL.dropHeaders=true;
  65. aL.url=aI;
  66. aL.contentType="text/plain";
  67. aL.transport="polling";
  68. aL.method="GET";
  69. aL.data="";
  70. aL.heartbeat=null;
  71. if(p.enableXDR){aL.enableXDR=p.enableXDR
  72. }aL.async=p.closeAsync;
  73. am("",aL)
  74. }}function H(){aH("Closing (AtmosphereRequest._close() called)");
  75. aG=true;
  76. if(p.reconnectId){clearTimeout(p.reconnectId);
  77. delete p.reconnectId
  78. }if(p.heartbeatTimer){clearTimeout(p.heartbeatTimer)
  79. }p.reconnect=false;
  80. ar.request=p;
  81. ar.state="unsubscribe";
  82. ar.responseBody="";
  83. ar.status=408;
  84. ar.partialMessage="";
  85. p.curWebsocketErrorRetries=0;
  86. ai();
  87. D();
  88. l()
  89. }function l(){ar.partialMessage="";
  90. if(p.id){clearTimeout(p.id)
  91. }if(p.heartbeatTimer){clearTimeout(p.heartbeatTimer)
  92. }if(p.reconnectId){clearTimeout(p.reconnectId);
  93. delete p.reconnectId
  94. }if(n!=null){n.close();
  95. n=null
  96. }if(X!=null){X.abort();
  97. X=null
  98. }if(z!=null){z.abort();
  99. z=null
  100. }if(aw!=null){if(aw.canSendMessage){aH("invoking .close() on WebSocket object");
  101. aw.close()
  102. }aw=null
  103. }if(ag!=null){ag.close();
  104. ag=null
  105. }ah()
  106. }function ah(){if(h!=null){clearInterval(y);
  107. document.cookie=aF+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
  108. h.signal("close",{reason:"",heir:!aG?R:(h.get("children")||[])[0]});
  109. h.close()
  110. }if(ax!=null){ax.close()
  111. }}function ao(aI){U();
  112. p=a.util.extend(p,aI);
  113. p.mrequest=p.reconnect;
  114. if(!p.reconnect){p.reconnect=true
  115. }}function au(){return p.webSocketImpl!=null||window.WebSocket||window.MozWebSocket
  116. }function at(){var aJ=a.util.getAbsoluteURL(p.url.toLowerCase());
  117. var aK=/^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/.exec(aJ);
  118. var aI=!!(aK&&(aK[1]!=window.location.protocol||aK[2]!=window.location.hostname||(aK[3]||(aK[1]==="http:"?80:443))!=(window.location.port||(window.location.protocol==="http:"?80:443))));
  119. return window.EventSource&&(!aI||!a.util.browser.safari||a.util.browser.vmajor>=7)
  120. }function aa(){if(p.shared){ax=aD(p);
  121. if(ax!=null){if(w("debug")){a.util.debug("Storage service available. All communication will be local")
  122. }if(ax.open(p)){return
  123. }}if(w("debug")){a.util.debug("No Storage service available.")
  124. }ax=null
  125. }p.firstMessage=e==0?true:false;
  126. p.isOpen=false;
  127. p.ctime=a.util.now();
  128. if(p.uuid===0){p.uuid=e
  129. }ar.closedByClientTimeout=false;
  130. if(p.transport!=="websocket"&&p.transport!=="sse"){L(p)
  131. }else{if(p.transport==="websocket"){if(!au()){az("Websocket is not supported, using request.fallbackTransport ("+p.fallbackTransport+")")
  132. }else{af(false)
  133. }}else{if(p.transport==="sse"){if(!at()){az("Server Side Events(SSE) is not supported, using request.fallbackTransport ("+p.fallbackTransport+")")
  134. }else{C(false)
  135. }}}}}function aD(aM){var aN,aL,aQ,aI="atmosphere-"+aM.url,aJ={storage:function(){function aR(aV){if(aV.key===aI&&aV.newValue){aK(aV.newValue)
  136. }}if(!a.util.storage){return
  137. }var aU=window.localStorage,aS=function(aV){var aW=aU.getItem(aI+"-"+aV);
  138. return aW===null?[]:JSON.parse(aW)
  139. },aT=function(aV,aW){aU.setItem(aI+"-"+aV,JSON.stringify(aW))
  140. };
  141. return{init:function(){aT("children",aS("children").concat([R]));
  142. a.util.on(window,"storage",aR);
  143. return aS("opened")
  144. },signal:function(aV,aW){aU.setItem(aI,JSON.stringify({target:"p",type:aV,data:aW}))
  145. },close:function(){var aV=aS("children");
  146. a.util.off(window,"storage",aR);
  147. if(aV){if(aO(aV,aM.id)){aT("children",aV)
  148. }}}}
  149. },windowref:function(){var aR=window.open("",aI.replace(/\W/g,""));
  150. if(!aR||aR.closed||!aR.callbacks){return
  151. }return{init:function(){aR.callbacks.push(aK);
  152. aR.children.push(R);
  153. return aR.opened
  154. },signal:function(aS,aT){if(!aR.closed&&aR.fire){aR.fire(JSON.stringify({target:"p",type:aS,data:aT}))
  155. }},close:function(){if(!aQ){aO(aR.callbacks,aK);
  156. aO(aR.children,R)
  157. }}}
  158. }};
  159. function aO(aU,aT){var aR,aS=aU.length;
  160. for(aR=0;
  161. aR<aS;
  162. aR++){if(aU[aR]===aT){aU.splice(aR,1)
  163. }}return aS!==aU.length
  164. }function aK(aR){var aT=JSON.parse(aR),aS=aT.data;
  165. if(aT.target==="c"){switch(aT.type){case"open":V("opening","local",p);
  166. break;
  167. case"close":if(!aQ){aQ=true;
  168. if(aS.reason==="aborted"){H()
  169. }else{if(aS.heir===R){aa()
  170. }else{setTimeout(function(){aa()
  171. },100)
  172. }}}break;
  173. case"message":k(aS,"messageReceived",200,aM.transport);
  174. break;
  175. case"localMessage":F(aS);
  176. break
  177. }}}function aP(){var aR=new RegExp("(?:^|; )("+encodeURIComponent(aI)+")=([^;]*)").exec(document.cookie);
  178. if(aR){return JSON.parse(decodeURIComponent(aR[2]))
  179. }}aN=aP();
  180. if(!aN||a.util.now()-aN.ts>1000){return
  181. }aL=aJ.storage()||aJ.windowref();
  182. if(!aL){return
  183. }return{open:function(){var aR;
  184. y=setInterval(function(){var aS=aN;
  185. aN=aP();
  186. if(!aN||aS.ts===aN.ts){aK(JSON.stringify({target:"c",type:"close",data:{reason:"error",heir:aS.heir}}))
  187. }},1000);
  188. aR=aL.init();
  189. if(aR){setTimeout(function(){V("opening","local",aM)
  190. },50)
  191. }return aR
  192. },send:function(aR){aL.signal("send",aR)
  193. },localSend:function(aR){aL.signal("localSend",JSON.stringify({id:R,event:aR}))
  194. },close:function(){if(!aG){clearInterval(y);
  195. aL.signal("close");
  196. aL.close()
  197. }}}
  198. }function aE(){var aJ,aI="atmosphere-"+p.url,aN={storage:function(){function aO(aQ){if(aQ.key===aI&&aQ.newValue){aK(aQ.newValue)
  199. }}if(!a.util.storage){return
  200. }var aP=window.localStorage;
  201. return{init:function(){a.util.on(window,"storage",aO)
  202. },signal:function(aQ,aR){aP.setItem(aI,JSON.stringify({target:"c",type:aQ,data:aR}))
  203. },get:function(aQ){return JSON.parse(aP.getItem(aI+"-"+aQ))
  204. },set:function(aQ,aR){aP.setItem(aI+"-"+aQ,JSON.stringify(aR))
  205. },close:function(){a.util.off(window,"storage",aO);
  206. aP.removeItem(aI);
  207. aP.removeItem(aI+"-opened");
  208. aP.removeItem(aI+"-children")
  209. }}
  210. },windowref:function(){var aP=aI.replace(/\W/g,""),aO=document.getElementById(aP),aQ;
  211. if(!aO){aO=document.createElement("div");
  212. aO.id=aP;
  213. aO.style.display="none";
  214. aO.innerHTML='<iframe name="'+aP+'" />';
  215. document.body.appendChild(aO)
  216. }aQ=aO.firstChild.contentWindow;
  217. return{init:function(){aQ.callbacks=[aK];
  218. aQ.fire=function(aR){var aS;
  219. for(aS=0;
  220. aS<aQ.callbacks.length;
  221. aS++){aQ.callbacks[aS](aR)
  222. }}
  223. },signal:function(aR,aS){if(!aQ.closed&&aQ.fire){aQ.fire(JSON.stringify({target:"c",type:aR,data:aS}))
  224. }},get:function(aR){return !aQ.closed?aQ[aR]:null
  225. },set:function(aR,aS){if(!aQ.closed){aQ[aR]=aS
  226. }},close:function(){}}
  227. }};
  228. function aK(aO){var aQ=JSON.parse(aO),aP=aQ.data;
  229. if(aQ.target==="p"){switch(aQ.type){case"send":t(aP);
  230. break;
  231. case"localSend":F(aP);
  232. break;
  233. case"close":H();
  234. break
  235. }}}Q=function aM(aO){aJ.signal("message",aO)
  236. };
  237. function aL(){document.cookie=aF+"="+encodeURIComponent(JSON.stringify({ts:a.util.now()+1,heir:(aJ.get("children")||[])[0]}))+"; path=/"
  238. }aJ=aN.storage()||aN.windowref();
  239. aJ.init();
  240. if(w("debug")){a.util.debug("Installed StorageService "+aJ)
  241. }aJ.set("children",[]);
  242. if(aJ.get("opened")!=null&&!aJ.get("opened")){aJ.set("opened",false)
  243. }aF=encodeURIComponent(aI);
  244. aL();
  245. y=setInterval(aL,1000);
  246. h=aJ
  247. }function V(aK,aN,aJ){if(p.shared&&aN!=="local"){aE()
  248. }if(h!=null){h.set("opened",true)
  249. }aJ.close=function(){H()
  250. };
  251. if(ay>0&&aK==="re-connecting"){aJ.isReopen=true;
  252. q(ar)
  253. }else{if(ar.error==null){ar.request=aJ;
  254. var aL=ar.state;
  255. ar.state=aK;
  256. var aI=ar.transport;
  257. ar.transport=aN;
  258. var aM=ar.responseBody;
  259. ai();
  260. ar.responseBody=aM;
  261. ar.state=aL;
  262. ar.transport=aI
  263. }}}function aB(aK){aK.transport="jsonp";
  264. var aJ=p,aI;
  265. if((aK!=null)&&(typeof(aK)!=="undefined")){aJ=aK
  266. }X={open:function(){var aN="atmosphere"+(++R);
  267. function aL(){aJ.lastIndex=0;
  268. if(aJ.openId){clearTimeout(aJ.openId)
  269. }if(aJ.heartbeatTimer){clearTimeout(aJ.heartbeatTimer)
  270. }if(aJ.reconnect&&ay++<aJ.maxReconnectOnClose){V("re-connecting",aJ.transport,aJ);
  271. an(X,aJ,aK.reconnectInterval);
  272. aJ.openId=setTimeout(function(){Z(aJ)
  273. },aJ.reconnectInterval+1000)
  274. }else{T(0,"maxReconnectOnClose reached")
  275. }}function aM(){var aO=aJ.url;
  276. if(aJ.dispatchUrl!=null){aO+=aJ.dispatchUrl
  277. }var aQ=aJ.data;
  278. if(aJ.attachHeadersAsQueryString){aO=o(aJ);
  279. if(aQ!==""){aO+="&X-Atmosphere-Post-Body="+encodeURIComponent(aQ)
  280. }aQ=""
  281. }var aP=document.head||document.getElementsByTagName("head")[0]||document.documentElement;
  282. aI=document.createElement("script");
  283. aI.src=aO+"&jsonpTransport="+aN;
  284. aI.clean=function(){aI.clean=aI.onerror=aI.onload=aI.onreadystatechange=null;
  285. if(aI.parentNode){aI.parentNode.removeChild(aI)
  286. }if(++aK.scriptCount===2){aK.scriptCount=1;
  287. aL()
  288. }};
  289. aI.onload=aI.onreadystatechange=function(){aH("jsonp.onload");
  290. if(!aI.readyState||/loaded|complete/.test(aI.readyState)){aI.clean()
  291. }};
  292. aI.onerror=function(){aH("jsonp.onerror");
  293. aK.scriptCount=1;
  294. aI.clean()
  295. };
  296. aP.insertBefore(aI,aP.firstChild)
  297. }window[aN]=function(aQ){aH("jsonp.window");
  298. aK.scriptCount=0;
  299. if(aJ.reconnect&&aJ.maxRequest===-1||aJ.requestCount++<aJ.maxRequest){if(!aJ.executeCallbackBeforeReconnect){an(X,aJ,aJ.pollingInterval)
  300. }if(aQ!=null&&typeof aQ!=="string"){try{aQ=aQ.message
  301. }catch(aP){}}var aO=r(aQ,aJ,ar);
  302. if(!aO){k(ar.responseBody,"messageReceived",200,aJ.transport)
  303. }if(aJ.executeCallbackBeforeReconnect){an(X,aJ,aJ.pollingInterval)
  304. }j(aJ)
  305. }else{a.util.log(p.logLevel,["JSONP reconnect maximum try reached "+p.requestCount]);
  306. T(0,"maxRequest reached")
  307. }};
  308. setTimeout(function(){aM()
  309. },50)
  310. },abort:function(){if(aI&&aI.clean){aI.clean()
  311. }}};
  312. X.open()
  313. }function av(aI){if(p.webSocketImpl!=null){return p.webSocketImpl
  314. }else{if(window.WebSocket){return new WebSocket(aI)
  315. }else{return new MozWebSocket(aI)
  316. }}}function v(){return o(p,a.util.getAbsoluteURL(p.webSocketUrl||p.url)).replace(/^http/,"ws")
  317. }function S(){var aI=o(p);
  318. return aI
  319. }function C(aJ){ar.transport="sse";
  320. var aI=S();
  321. if(w("debug")){a.util.debug("Invoking executeSSE");
  322. a.util.debug("Using URL: "+aI)
  323. }if(aJ&&!p.reconnect){if(ag!=null){l()
  324. }return
  325. }try{ag=new EventSource(aI,{withCredentials:p.withCredentials})
  326. }catch(aK){T(0,aK);
  327. az("SSE failed. Downgrading to fallback transport and resending");
  328. return
  329. }if(p.connectTimeout>0){p.id=setTimeout(function(){if(!aJ){l()
  330. }},p.connectTimeout)
  331. }ag.onopen=function(aL){aH("sse.onopen");
  332. j(p);
  333. if(w("debug")){a.util.debug("SSE successfully opened")
  334. }if(!p.enableProtocol){if(!aJ){V("opening","sse",p)
  335. }else{V("re-opening","sse",p)
  336. }}else{if(p.isReopen){p.isReopen=false;
  337. V("re-opening",p.transport,p)
  338. }}aJ=true;
  339. if(p.method==="POST"){ar.state="messageReceived";
  340. t(p.data)
  341. }};
  342. ag.onmessage=function(aM){aH("sse.onmessage");
  343. j(p);
  344. if(!p.enableXDR&&window.location.host&&aM.origin&&aM.origin!==window.location.protocol+"//"+window.location.host){a.util.log(p.logLevel,["Origin was not "+window.location.protocol+"//"+window.location.host]);
  345. return
  346. }ar.state="messageReceived";
  347. ar.status=200;
  348. aM=aM.data;
  349. var aL=r(aM,p,ar);
  350. if(!aL){ai();
  351. ar.responseBody="";
  352. ar.messages=[]
  353. }};
  354. ag.onerror=function(aL){aH("sse.onerror");
  355. clearTimeout(p.id);
  356. if(p.heartbeatTimer){clearTimeout(p.heartbeatTimer)
  357. }if(ar.closedByClientTimeout){return
  358. }ae(aJ);
  359. l();
  360. if(aG){a.util.log(p.logLevel,["SSE closed normally"])
  361. }else{if(!aJ){az("SSE failed. Downgrading to fallback transport and resending")
  362. }else{if(p.reconnect&&(ar.transport==="sse")){if(ay++<p.maxReconnectOnClose){V("re-connecting",p.transport,p);
  363. if(p.reconnectInterval>0){p.reconnectId=setTimeout(function(){C(true)
  364. },p.reconnectInterval)
  365. }else{C(true)
  366. }ar.responseBody="";
  367. ar.messages=[]
  368. }else{a.util.log(p.logLevel,["SSE reconnect maximum try reached "+ay]);
  369. T(0,"maxReconnectOnClose reached")
  370. }}}}}
  371. }function af(aJ){ar.transport="websocket";
  372. var aI=v(p.url);
  373. if(w("debug")){a.util.debug("Invoking executeWebSocket, using URL: "+aI)
  374. }if(aJ&&!p.reconnect){if(aw!=null){l()
  375. }return
  376. }aw=av(aI);
  377. if(p.webSocketBinaryType!=null){aw.binaryType=p.webSocketBinaryType
  378. }if(p.connectTimeout>0){p.id=setTimeout(function(){if(!aJ){var aM={code:1002,reason:"",wasClean:false};
  379. aw.onclose(aM);
  380. try{l()
  381. }catch(aN){}return
  382. }},p.connectTimeout)
  383. }aw.onopen=function(aN){aH("websocket.onopen");
  384. j(p);
  385. c=false;
  386. if(w("debug")){a.util.debug("Websocket successfully opened")
  387. }var aM=aJ;
  388. if(aw!=null){aw.canSendMessage=true
  389. }if(!p.enableProtocol){aJ=true;
  390. if(aM){V("re-opening","websocket",p)
  391. }else{V("opening","websocket",p)
  392. }}if(aw!=null){if(p.method==="POST"){ar.state="messageReceived";
  393. aw.send(p.data)
  394. }}};
  395. aw.onmessage=function(aO){aH("websocket.onmessage");
  396. j(p);
  397. if(p.enableProtocol){aJ=true
  398. }ar.state="messageReceived";
  399. ar.status=200;
  400. aO=aO.data;
  401. var aM=typeof(aO)==="string";
  402. if(aM){var aN=r(aO,p,ar);
  403. if(!aN){ai();
  404. ar.responseBody="";
  405. ar.messages=[]
  406. }}else{aO=s(p,aO);
  407. if(aO===""){return
  408. }ar.responseBody=aO;
  409. ai();
  410. ar.responseBody=null
  411. }};
  412. aw.onerror=function(aM){aH("websocket.onerror");
  413. clearTimeout(p.id);
  414. if(p.heartbeatTimer){clearTimeout(p.heartbeatTimer)
  415. }if(++p.curWebsocketErrorRetries<p.maxWebsocketErrorRetries&&p.fallbackTransport!=="websocket"){az("Failed to connect via Websocket. Downgrading to "+p.fallbackTransport+" and resending")
  416. }};
  417. aw.onclose=function(aM){aH("websocket.onclose");
  418. clearTimeout(p.id);
  419. if(ar.state==="closed"){return
  420. }var aN=aM.reason;
  421. if(aN===""){switch(aM.code){case 1000:aN="Normal closure; the connection successfully completed whatever purpose for which it was created.";
  422. break;
  423. case 1001:aN="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.";
  424. break;
  425. case 1002:aN="The endpoint is terminating the connection due to a protocol error.";
  426. break;
  427. case 1003:aN="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).";
  428. break;
  429. case 1004:aN="The endpoint is terminating the connection because a data frame was received that is too large.";
  430. break;
  431. case 1005:aN="Unknown: no status code was provided even though one was expected.";
  432. break;
  433. case 1006:aN="Connection was closed abnormally (that is, with no close frame being sent).";
  434. break
  435. }}if(w("warn")){a.util.warn("Websocket closed, reason: "+aN+" - wasClean: "+aM.wasClean)
  436. }if(ar.closedByClientTimeout||(p.handleOnlineOffline&&c)){if(p.reconnectId){clearTimeout(p.reconnectId);
  437. delete p.reconnectId
  438. }return
  439. }ae(aJ);
  440. ar.state="closed";
  441. if(aG){a.util.log(p.logLevel,["Websocket closed normally"])
  442. }else{if(!aJ&&p.fallbackTransport!=="websocket"){az("Websocket failed on first connection attempt. Downgrading to "+p.fallbackTransport+" and resending")
  443. }else{if(p.reconnect&&ar.transport==="websocket"){l();
  444. if(ay++<p.maxReconnectOnClose){V("re-connecting",p.transport,p);
  445. if(p.reconnectInterval>0){p.reconnectId=setTimeout(function(){ar.responseBody="";
  446. ar.messages=[];
  447. af(true)
  448. },p.reconnectInterval)
  449. }else{ar.responseBody="";
  450. ar.messages=[];
  451. af(true)
  452. }}else{a.util.log(p.logLevel,["Websocket reconnect maximum try reached "+ay]);
  453. if(w("warn")){a.util.warn("Websocket error, reason: "+aM.reason)
  454. }T(0,"maxReconnectOnClose reached")
  455. }}}}};
  456. var aK=navigator.userAgent.toLowerCase();
  457. var aL=aK.indexOf("android")>-1;
  458. if(aL&&aw.url===undefined){aw.onclose({reason:"Android 4.1 does not support websockets.",wasClean:false})
  459. }}function s(aM,aL){var aK=aL;
  460. if(aM.transport==="polling"){return aK
  461. }if(aM.enableProtocol&&aM.firstMessage&&a.util.trim(aL).length!==0){var aN=aM.trackMessageLength?1:0;
  462. var aJ=aL.split(aM.messageDelimiter);
  463. if(aJ.length<=aN+1){return aK
  464. }aM.firstMessage=false;
  465. aM.uuid=a.util.trim(aJ[aN]);
  466. if(aJ.length<=aN+2){a.util.log("error",["Protocol data not sent by the server. If you enable protocol on client side, be sure to install JavascriptProtocol interceptor on server side.Also note that atmosphere-runtime 2.2+ should be used."])
  467. }J=parseInt(a.util.trim(aJ[aN+1]),10);
  468. ak=aJ[aN+2];
  469. if(aM.transport!=="long-polling"){Z(aM)
  470. }e=aM.uuid;
  471. aK="";
  472. aN=aM.trackMessageLength?4:3;
  473. if(aJ.length>aN+1){for(var aI=aN;
  474. aI<aJ.length;
  475. aI++){aK+=aJ[aI];
  476. if(aI+1!==aJ.length){aK+=aM.messageDelimiter
  477. }}}if(aM.ackInterval!==0){setTimeout(function(){t("...ACK...")
  478. },aM.ackInterval)
  479. }}else{if(aM.enableProtocol&&aM.firstMessage&&a.util.browser.msie&&+a.util.browser.version.split(".")[0]<10){a.util.log(p.logLevel,["Receiving unexpected data from IE"])
  480. }else{Z(aM)
  481. }}return aK
  482. }function j(aI){clearTimeout(aI.id);
  483. if(aI.timeout>0&&aI.transport!=="polling"){aI.id=setTimeout(function(){aC(aI);
  484. D();
  485. l()
  486. },aI.timeout)
  487. }}function aC(aI){ar.closedByClientTimeout=true;
  488. ar.state="closedByClient";
  489. ar.responseBody="";
  490. ar.status=408;
  491. ar.messages=[];
  492. ai()
  493. }function T(aI,aJ){l();
  494. clearTimeout(p.id);
  495. ar.state="error";
  496. ar.reasonPhrase=aJ;
  497. ar.responseBody="";
  498. ar.status=aI;
  499. ar.messages=[];
  500. ai()
  501. }function r(aM,aL,aI){aM=s(aL,aM);
  502. if(aM.length===0){return true
  503. }aI.responseBody=aM;
  504. if(aL.trackMessageLength){aM=aI.partialMessage+aM;
  505. var aK=[];
  506. var aJ=aM.indexOf(aL.messageDelimiter);
  507. if(aJ!=-1){while(aJ!==-1){var aO=aM.substring(0,aJ);
  508. var aN=+aO;
  509. if(isNaN(aN)){aI.partialMessage="";
  510. throw new Error('message length "'+aO+'" is not a number')
  511. }aJ+=aL.messageDelimiter.length;
  512. if(aJ+aN>aM.length){aJ=-1
  513. }else{aK.push(aM.substring(aJ,aJ+aN));
  514. aM=aM.substring(aJ+aN,aM.length);
  515. aJ=aM.indexOf(aL.messageDelimiter)
  516. }}aI.partialMessage=aM;
  517. if(aK.length!==0){aI.responseBody=aK.join(aL.messageDelimiter);
  518. aI.messages=aK;
  519. return false
  520. }else{aI.responseBody="";
  521. aI.messages=[];
  522. return true
  523. }}}aI.responseBody=aM;
  524. aI.messages=[aM];
  525. return false
  526. }function az(aI){a.util.log(p.logLevel,[aI]);
  527. if(typeof(p.onTransportFailure)!=="undefined"){p.onTransportFailure(aI,p)
  528. }else{if(typeof(a.util.onTransportFailure)!=="undefined"){a.util.onTransportFailure(aI,p)
  529. }}var aJ=p.connectTimeout===-1?0:p.connectTimeout;
  530. if(p.reconnect&&p.transport!=="none"||p.transport==null){p.transport=p.fallbackTransport;
  531. p.method=p.fallbackMethod;
  532. ar.transport=p.fallbackTransport;
  533. ar.state="";
  534. p.fallbackTransport="none";
  535. if(aJ>0){p.reconnectId=setTimeout(function(){aa()
  536. },aJ)
  537. }else{aa()
  538. }}else{T(500,"Unable to reconnect with fallback transport")
  539. }}function o(aK,aI){var aJ=p;
  540. if((aK!=null)&&(typeof(aK)!=="undefined")){aJ=aK
  541. }if(aI==null){aI=aJ.url
  542. }if(!aJ.attachHeadersAsQueryString){return aI
  543. }if(aI.indexOf("X-Atmosphere-Framework")!==-1){return aI
  544. }aI+=(aI.indexOf("?")!==-1)?"&":"?";
  545. aI+="X-Atmosphere-tracking-id="+aJ.uuid;
  546. aI+="&X-Atmosphere-Framework="+a.version;
  547. aI+="&X-Atmosphere-Transport="+aJ.transport;
  548. if(aJ.trackMessageLength){aI+="&X-Atmosphere-TrackMessageSize=true"
  549. }if(aJ.heartbeat!==null&&aJ.heartbeat.server!==null){aI+="&X-Heartbeat-Server="+aJ.heartbeat.server
  550. }if(aJ.contentType!==""){aI+="&Content-Type="+(aJ.transport==="websocket"?aJ.contentType:encodeURIComponent(aJ.contentType))
  551. }if(aJ.enableProtocol){aI+="&X-atmo-protocol=true"
  552. }a.util.each(aJ.headers,function(aL,aN){var aM=a.util.isFunction(aN)?aN.call(this,aJ,aK,ar):aN;
  553. if(aM!=null){aI+="&"+encodeURIComponent(aL)+"="+encodeURIComponent(aM)
  554. }});
  555. return aI
  556. }function Z(aI){if(!aI.isOpen){aI.isOpen=true;
  557. V("opening",aI.transport,aI)
  558. }else{if(aI.isReopen){aI.isReopen=false;
  559. V("re-opening",aI.transport,aI)
  560. }else{if(ar.state==="messageReceived"&&(aI.transport==="jsonp"||aI.transport==="long-polling")){ap(ar)
  561. }else{return
  562. }}}B(aI)
  563. }function B(aJ){if(aJ.heartbeatTimer!=null){clearTimeout(aJ.heartbeatTimer)
  564. }if(!isNaN(J)&&J>0){var aI=function(){if(w("debug")){a.util.debug("Sending heartbeat")
  565. }t(ak);
  566. aJ.heartbeatTimer=setTimeout(aI,J)
  567. };
  568. aJ.heartbeatTimer=setTimeout(aI,J)
  569. }}function L(aM){var aJ=p;
  570. if((aM!=null)||(typeof(aM)!=="undefined")){aJ=aM
  571. }aJ.lastIndex=0;
  572. aJ.readyState=0;
  573. if((aJ.transport==="jsonp")||((aJ.enableXDR)&&(a.util.checkCORSSupport()))){aB(aJ);
  574. return
  575. }if(a.util.browser.msie&&+a.util.browser.version.split(".")[0]<10){if((aJ.transport==="streaming")){if(aJ.enableXDR&&window.XDomainRequest){P(aJ)
  576. }else{aA(aJ)
  577. }return
  578. }if((aJ.enableXDR)&&(window.XDomainRequest)){P(aJ);
  579. return
  580. }}var aL=function(aQ){aJ.lastIndex=0;
  581. ay++;
  582. if(aQ||(aJ.reconnect&&ay<=aJ.maxReconnectOnClose)){var aP=aQ?0:aM.reconnectInterval;
  583. ar.ffTryingReconnect=true;
  584. V("re-connecting",aM.transport,aM);
  585. an(aK,aJ,aP)
  586. }else{T(0,"maxReconnectOnClose reached")
  587. }};
  588. var aN=function(aP){if(a._beforeUnloadState){a.util.debug(new Date()+" Atmosphere: reconnectF: execution delayed due to _beforeUnloadState flag");
  589. setTimeout(function(){aL(aP)
  590. },5000)
  591. }else{aL(aP)
  592. }};
  593. var aI=function(){ar.errorHandled=true;
  594. l();
  595. aN(false)
  596. };
  597. if(aJ.force||(aJ.reconnect&&(aJ.maxRequest===-1||aJ.requestCount++<aJ.maxRequest))){aJ.force=false;
  598. var aK=a.util.xhr();
  599. aK.hasData=false;
  600. M(aK,aJ,true);
  601. if(aJ.suspend){z=aK
  602. }if(aJ.transport!=="polling"){ar.transport=aJ.transport;
  603. aK.onabort=function(){aH("ajaxrequest.onabort");
  604. ae(true)
  605. };
  606. aK.onerror=function(){aH("ajaxrequest.onerror");
  607. ar.error=true;
  608. ar.ffTryingReconnect=true;
  609. try{ar.status=XMLHttpRequest.status
  610. }catch(aP){ar.status=500
  611. }if(!ar.status){ar.status=500
  612. }if(!ar.errorHandled){l();
  613. aN(false)
  614. }}
  615. }aK.onreadystatechange=function(){aH("ajaxRequest.onreadystatechange, new state: "+aK.readyState);
  616. if(aG){aH("onreadystatechange has been ignored due to _abortingConnection flag");
  617. return
  618. }ar.error=null;
  619. var aQ=false;
  620. var aW=false;
  621. if(aJ.transport==="streaming"&&aJ.readyState>2&&aK.readyState===4){l();
  622. aN(false);
  623. return
  624. }aJ.readyState=aK.readyState;
  625. if(aJ.transport==="streaming"&&aK.readyState>=3){aW=true
  626. }else{if(aJ.transport==="long-polling"&&aK.readyState===4){aW=true
  627. }}j(p);
  628. if(aJ.transport!=="polling"){var aP=200;
  629. if(aK.readyState===4){aP=aK.status>1000?0:aK.status
  630. }if(!aJ.reconnectOnServerError&&(aP>=300&&aP<600)){T(aP,aK.statusText);
  631. return
  632. }if(aP>=300||aP===0){aI();
  633. return
  634. }if((!aJ.enableProtocol||!aM.firstMessage)&&aK.readyState===2){if(a.util.browser.mozilla&&ar.ffTryingReconnect){ar.ffTryingReconnect=false;
  635. setTimeout(function(){if(!ar.ffTryingReconnect){Z(aJ)
  636. }},500)
  637. }else{Z(aJ)
  638. }}}else{if(aK.readyState===4){aW=true
  639. }}if(aW){var aT=aK.responseText;
  640. ar.errorHandled=false;
  641. if(aJ.transport==="long-polling"&&a.util.trim(aT).length===0){if(!aK.hasData){aN(true)
  642. }else{aK.hasData=false
  643. }return
  644. }aK.hasData=true;
  645. G(aK,p);
  646. if(aJ.transport==="streaming"){if(!a.util.browser.opera){var aS=aT.substring(aJ.lastIndex,aT.length);
  647. aQ=r(aS,aJ,ar);
  648. aJ.lastIndex=aT.length;
  649. if(aQ){return
  650. }}else{a.util.iterate(function(){if(ar.status!==500&&aK.responseText.length>aJ.lastIndex){try{ar.status=aK.status;
  651. ar.headers=a.util.parseHeaders(aK.getAllResponseHeaders());
  652. G(aK,p)
  653. }catch(aY){ar.status=404
  654. }j(p);
  655. ar.state="messageReceived";
  656. var aX=aK.responseText.substring(aJ.lastIndex);
  657. aJ.lastIndex=aK.responseText.length;
  658. aQ=r(aX,aJ,ar);
  659. if(!aQ){ai()
  660. }if(I(aK,aJ)){K(aK,aJ);
  661. return
  662. }}else{if(ar.status>400){aJ.lastIndex=aK.responseText.length;
  663. return false
  664. }}},0)
  665. }}else{aQ=r(aT,aJ,ar)
  666. }var aV=I(aK,aJ);
  667. try{ar.status=aK.status;
  668. ar.headers=a.util.parseHeaders(aK.getAllResponseHeaders());
  669. G(aK,aJ)
  670. }catch(aU){ar.status=404
  671. }if(aJ.suspend){ar.state=ar.status===0?"closed":"messageReceived"
  672. }else{ar.state="messagePublished"
  673. }var aR=!aV&&aM.transport!=="streaming"&&aM.transport!=="polling";
  674. if(aR&&!aJ.executeCallbackBeforeReconnect){an(aK,aJ,aJ.pollingInterval)
  675. }if(ar.responseBody.length!==0&&!aQ){ai()
  676. }if(aR&&aJ.executeCallbackBeforeReconnect){an(aK,aJ,aJ.pollingInterval)
  677. }if(aV){K(aK,aJ)
  678. }}};
  679. try{aK.send(aJ.data);
  680. u=true
  681. }catch(aO){a.util.log(aJ.logLevel,["Unable to connect to "+aJ.url]);
  682. T(0,aO)
  683. }}else{if(aJ.logLevel==="debug"){a.util.log(aJ.logLevel,["Max re-connection reached."])
  684. }T(0,"maxRequest reached")
  685. }}function K(aJ,aI){ar.messages=[];
  686. aI.isReopen=true;
  687. H();
  688. aG=false;
  689. an(aJ,aI,500)
  690. }function M(aK,aL,aJ){var aI=aL.url;
  691. if(aL.dispatchUrl!=null&&aL.method==="POST"){aI+=aL.dispatchUrl
  692. }aI=o(aL,aI);
  693. aI=a.util.prepareURL(aI);
  694. if(aJ){aK.open(aL.method,aI,aL.async);
  695. if(aL.connectTimeout>0){aL.id=setTimeout(function(){if(aL.requestCount===0){l();
  696. k("Connect timeout","closed",200,aL.transport)
  697. }},aL.connectTimeout)
  698. }}if(p.withCredentials&&p.transport!=="websocket"){if("withCredentials" in aK){aK.withCredentials=true
  699. }}if(!p.dropHeaders){aK.setRequestHeader("X-Atmosphere-Framework",a.version);
  700. aK.setRequestHeader("X-Atmosphere-Transport",aL.transport);
  701. if(aL.heartbeat!==null&&aL.heartbeat.server!==null){aK.setRequestHeader("X-Heartbeat-Server",aK.heartbeat.server)
  702. }if(aL.trackMessageLength){aK.setRequestHeader("X-Atmosphere-TrackMessageSize","true")
  703. }aK.setRequestHeader("X-Atmosphere-tracking-id",aL.uuid);
  704. a.util.each(aL.headers,function(aM,aO){var aN=a.util.isFunction(aO)?aO.call(this,aK,aL,aJ,ar):aO;
  705. if(aN!=null){aK.setRequestHeader(aM,aN)
  706. }})
  707. }if(aL.contentType!==""){aK.setRequestHeader("Content-Type",aL.contentType)
  708. }}function an(aK,aL,aJ){if(ar.closedByClientTimeout){return
  709. }if(aL.reconnect||(aL.suspend&&u)){var aI=0;
  710. if(aK&&aK.readyState>1){aI=aK.status>1000?0:aK.status
  711. }ar.status=aI===0?204:aI;
  712. ar.reason=aI===0?"Server resumed the connection or down.":"OK";
  713. clearTimeout(aL.id);
  714. if(aL.reconnectId){clearTimeout(aL.reconnectId);
  715. delete aL.reconnectId
  716. }if(aJ>0){p.reconnectId=setTimeout(function(){L(aL)
  717. },aJ)
  718. }else{L(aL)
  719. }}}function q(aI){aI.state="re-connecting";
  720. al(aI)
  721. }function ap(aI){aI.state="openAfterResume";
  722. al(aI);
  723. aI.state="messageReceived"
  724. }function P(aI){if(aI.transport!=="polling"){n=ac(aI);
  725. n.open()
  726. }else{ac(aI).open()
  727. }}function ac(aK){var aJ=p;
  728. if((aK!=null)&&(typeof(aK)!=="undefined")){aJ=aK
  729. }var aP=aJ.transport;
  730. var aO=0;
  731. var aI=new window.XDomainRequest();
  732. var aM=function(){if(aJ.transport==="long-polling"&&(aJ.reconnect&&(aJ.maxRequest===-1||aJ.requestCount++<aJ.maxRequest))){aI.status=200;
  733. P(aJ)
  734. }};
  735. var aN=aJ.rewriteURL||function(aR){var aQ=/(?:^|;\s*)(JSESSIONID|PHPSESSID)=([^;]*)/.exec(document.cookie);
  736. switch(aQ&&aQ[1]){case"JSESSIONID":return aR.replace(/;jsessionid=[^\?]*|(\?)|$/,";jsessionid="+aQ[2]+"$1");
  737. case"PHPSESSID":return aR.replace(/\?PHPSESSID=[^&]*&?|\?|$/,"?PHPSESSID="+aQ[2]+"&").replace(/&$/,"")
  738. }return aR
  739. };
  740. aI.onprogress=function(){aL(aI)
  741. };
  742. aI.onerror=function(){if(aJ.transport!=="polling"){l();
  743. if(ay++<aJ.maxReconnectOnClose){if(aJ.reconnectInterval>0){aJ.reconnectId=setTimeout(function(){V("re-connecting",aK.transport,aK);
  744. P(aJ)
  745. },aJ.reconnectInterval)
  746. }else{V("re-connecting",aK.transport,aK);
  747. P(aJ)
  748. }}else{T(0,"maxReconnectOnClose reached")
  749. }}};
  750. aI.onload=function(){};
  751. var aL=function(aQ){clearTimeout(aJ.id);
  752. var aS=aQ.responseText;
  753. aS=aS.substring(aO);
  754. aO+=aS.length;
  755. if(aP!=="polling"){j(aJ);
  756. var aR=r(aS,aJ,ar);
  757. if(aP==="long-polling"&&a.util.trim(aS).length===0){return
  758. }if(aJ.executeCallbackBeforeReconnect){aM()
  759. }if(!aR){k(ar.responseBody,"messageReceived",200,aP)
  760. }if(!aJ.executeCallbackBeforeReconnect){aM()
  761. }}};
  762. return{open:function(){var aQ=aJ.url;
  763. if(aJ.dispatchUrl!=null){aQ+=aJ.dispatchUrl
  764. }aQ=o(aJ,aQ);
  765. aI.open(aJ.method,aN(aQ));
  766. if(aJ.method==="GET"){aI.send()
  767. }else{aI.send(aJ.data)
  768. }if(aJ.connectTimeout>0){aJ.id=setTimeout(function(){if(aJ.requestCount===0){l();
  769. k("Connect timeout","closed",200,aJ.transport)
  770. }},aJ.connectTimeout)
  771. }},close:function(){aI.abort()
  772. }}
  773. }function aA(aI){n=ab(aI);
  774. n.open()
  775. }function ab(aL){var aK=p;
  776. if((aL!=null)&&(typeof(aL)!=="undefined")){aK=aL
  777. }var aJ;
  778. var aM=new window.ActiveXObject("htmlfile");
  779. aM.open();
  780. aM.close();
  781. var aI=aK.url;
  782. if(aK.dispatchUrl!=null){aI+=aK.dispatchUrl
  783. }if(aK.transport!=="polling"){ar.transport=aK.transport
  784. }return{open:function(){var aN=aM.createElement("iframe");
  785. aI=o(aK);
  786. if(aK.data!==""){aI+="&X-Atmosphere-Post-Body="+encodeURIComponent(aK.data)
  787. }aI=a.util.prepareURL(aI);
  788. aN.src=aI;
  789. aM.body.appendChild(aN);
  790. var aO=aN.contentDocument||aN.contentWindow.document;
  791. aJ=a.util.iterate(function(){try{if(!aO.firstChild){return
  792. }var aR=aO.body?aO.body.lastChild:aO;
  793. if(aR.omgThisIsBroken){}var aT=function(){var aV=aR.cloneNode(true);
  794. aV.appendChild(aO.createTextNode("."));
  795. var aU=aV.innerText;
  796. aU=aU.substring(0,aU.length-1);
  797. return aU
  798. };
  799. if(!aO.body||!aO.body.firstChild||aO.body.firstChild.nodeName.toLowerCase()!=="pre"){var aQ=aO.head||aO.getElementsByTagName("head")[0]||aO.documentElement||aO;
  800. var aP=aO.createElement("script");
  801. aP.text="document.write('<plaintext>')";
  802. aQ.insertBefore(aP,aQ.firstChild);
  803. aQ.removeChild(aP);
  804. aR=aO.body.lastChild
  805. }if(aK.closed){aK.isReopen=true
  806. }aJ=a.util.iterate(function(){var aV=aT();
  807. if(aV.length>aK.lastIndex){j(p);
  808. ar.status=200;
  809. ar.error=null;
  810. aR.innerText="";
  811. var aU=r(aV,aK,ar);
  812. if(aU){return""
  813. }k(ar.responseBody,"messageReceived",200,aK.transport)
  814. }aK.lastIndex=0;
  815. if(aO.readyState==="complete"){ae(true);
  816. V("re-connecting",aK.transport,aK);
  817. if(aK.reconnectInterval>0){aK.reconnectId=setTimeout(function(){aA(aK)
  818. },aK.reconnectInterval)
  819. }else{aA(aK)
  820. }return false
  821. }},null);
  822. return false
  823. }catch(aS){ar.error=true;
  824. V("re-connecting",aK.transport,aK);
  825. if(ay++<aK.maxReconnectOnClose){if(aK.reconnectInterval>0){aK.reconnectId=setTimeout(function(){aA(aK)
  826. },aK.reconnectInterval)
  827. }else{aA(aK)
  828. }}else{T(0,"maxReconnectOnClose reached")
  829. }aM.execCommand("Stop");
  830. aM.close();
  831. return false
  832. }})
  833. },close:function(){if(aJ){aJ()
  834. }aM.execCommand("Stop");
  835. ae(true)
  836. }}
  837. }function t(aI){if(ax!=null){E(aI)
  838. }else{if(z!=null||ag!=null){O(aI)
  839. }else{if(n!=null){i(aI)
  840. }else{if(X!=null){A(aI)
  841. }else{if(aw!=null){W(aI)
  842. }else{T(0,"No suspended connection available");
  843. a.util.error("No suspended connection available. Make sure atmosphere.subscribe has been called and request.onOpen invoked before trying to push data")
  844. }}}}}}function am(aJ,aI){if(!aI){aI=x(aJ)
  845. }aI.transport="polling";
  846. aI.method="GET";
  847. aI.withCredentials=false;
  848. aI.reconnect=false;
  849. aI.force=true;
  850. aI.suspend=false;
  851. aI.timeout=1000;
  852. L(aI)
  853. }function E(aI){ax.send(aI)
  854. }function aq(aJ){if(aJ.length===0){return
  855. }try{if(ax){ax.localSend(aJ)
  856. }else{if(h){h.signal("localMessage",JSON.stringify({id:R,event:aJ}))
  857. }}}catch(aI){a.util.error(aI)
  858. }}function O(aJ){var aI=x(aJ);
  859. L(aI)
  860. }function i(aJ){if(p.enableXDR&&a.util.checkCORSSupport()){var aI=x(aJ);
  861. aI.reconnect=false;
  862. aB(aI)
  863. }else{O(aJ)
  864. }}function A(aI){O(aI)
  865. }function N(aI){var aJ=aI;
  866. if(typeof(aJ)==="object"){aJ=aI.data
  867. }return aJ
  868. }function x(aJ){var aK=N(aJ);
  869. var aI={connected:false,timeout:60000,method:"POST",url:p.url,contentType:p.contentType,headers:p.headers,reconnect:true,callback:null,data:aK,suspend:false,maxRequest:-1,logLevel:"info",requestCount:0,withCredentials:p.withCredentials,async:p.async,transport:"polling",isOpen:true,attachHeadersAsQueryString:true,enableXDR:p.enableXDR,uuid:p.uuid,dispatchUrl:p.dispatchUrl,enableProtocol:false,messageDelimiter:"|",trackMessageLength:p.trackMessageLength,maxReconnectOnClose:p.maxReconnectOnClose,heartbeatTimer:p.heartbeatTimer,heartbeat:p.heartbeat};
  870. if(typeof(aJ)==="object"){aI=a.util.extend(aI,aJ)
  871. }return aI
  872. }function W(aI){var aL=a.util.isBinary(aI)?aI:N(aI);
  873. var aJ;
  874. try{if(p.dispatchUrl!=null){aJ=p.webSocketPathDelimiter+p.dispatchUrl+p.webSocketPathDelimiter+aL
  875. }else{aJ=aL
  876. }if(!aw.canSendMessage){a.util.error("WebSocket not connected.");
  877. return
  878. }aw.send(aJ)
  879. }catch(aK){aw.onclose=function(aM){};
  880. l();
  881. az("Websocket failed. Downgrading to "+p.fallbackTransport+" and resending "+aI);
  882. O(aI)
  883. }}function F(aJ){var aI=JSON.parse(aJ);
  884. if(aI.id!==R){if(typeof(p.onLocalMessage)!=="undefined"){p.onLocalMessage(aI.event)
  885. }else{if(typeof(a.util.onLocalMessage)!=="undefined"){a.util.onLocalMessage(aI.event)
  886. }}}}function k(aL,aI,aJ,aK){ar.responseBody=aL;
  887. ar.transport=aK;
  888. ar.status=aJ;
  889. ar.state=aI;
  890. ai()
  891. }function G(aI,aK){if(!aK.readResponsesHeaders){if(!aK.enableProtocol){aK.uuid=R
  892. }}else{try{var aJ=aI.getResponseHeader("X-Atmosphere-tracking-id");
  893. if(aJ&&aJ!=null){aK.uuid=aJ.split(" ").pop()
  894. }}catch(aL){}}}function al(aI){m(aI,p);
  895. m(aI,a.util)
  896. }function m(aJ,aK){switch(aJ.state){case"messageReceived":aH("Firing onMessage");
  897. ay=0;
  898. if(typeof(aK.onMessage)!=="undefined"){aK.onMessage(aJ)
  899. }if(typeof(aK.onmessage)!=="undefined"){aK.onmessage(aJ)
  900. }break;
  901. case"error":var aL=(typeof(aJ.reasonPhrase)!="undefined")?aJ.reasonPhrase:"n/a";
  902. aH("Firing onError, reasonPhrase: "+aL);
  903. if(typeof(aK.onError)!=="undefined"){aK.onError(aJ)
  904. }if(typeof(aK.onerror)!=="undefined"){aK.onerror(aJ)
  905. }break;
  906. case"opening":delete p.closed;
  907. aH("Firing onOpen");
  908. if(typeof(aK.onOpen)!=="undefined"){aK.onOpen(aJ)
  909. }if(typeof(aK.onopen)!=="undefined"){aK.onopen(aJ)
  910. }break;
  911. case"messagePublished":aH("Firing messagePublished");
  912. if(typeof(aK.onMessagePublished)!=="undefined"){aK.onMessagePublished(aJ)
  913. }break;
  914. case"re-connecting":aH("Firing onReconnect");
  915. if(typeof(aK.onReconnect)!=="undefined"){aK.onReconnect(p,aJ)
  916. }break;
  917. case"closedByClient":aH("Firing closedByClient");
  918. if(typeof(aK.onClientTimeout)!=="undefined"){aK.onClientTimeout(p)
  919. }break;
  920. case"re-opening":delete p.closed;
  921. aH("Firing onReopen");
  922. if(typeof(aK.onReopen)!=="undefined"){aK.onReopen(p,aJ)
  923. }break;
  924. case"fail-to-reconnect":aH("Firing onFailureToReconnect");
  925. if(typeof(aK.onFailureToReconnect)!=="undefined"){aK.onFailureToReconnect(p,aJ)
  926. }break;
  927. case"unsubscribe":case"closed":var aI=typeof(p.closed)!=="undefined"?p.closed:false;
  928. if(!aI){aH("Firing onClose ("+aJ.state+" case)");
  929. if(typeof(aK.onClose)!=="undefined"){aK.onClose(aJ)
  930. }if(typeof(aK.onclose)!=="undefined"){aK.onclose(aJ)
  931. }}else{aH("Request already closed, not firing onClose ("+aJ.state+" case)")
  932. }p.closed=true;
  933. break;
  934. case"openAfterResume":if(typeof(aK.onOpenAfterResume)!=="undefined"){aK.onOpenAfterResume(p)
  935. }break
  936. }}function ae(aI){if(ar.state!=="closed"){ar.state="closed";
  937. ar.responseBody="";
  938. ar.messages=[];
  939. ar.status=!aI?501:200;
  940. ai()
  941. }}function ai(){var aK=function(aN,aO){aO(ar)
  942. };
  943. if(ax==null&&Q!=null){Q(ar.responseBody)
  944. }p.reconnect=p.mrequest;
  945. var aI=typeof(ar.responseBody)==="string";
  946. var aL=(aI&&p.trackMessageLength)?(ar.messages.length>0?ar.messages:[""]):new Array(ar.responseBody);
  947. for(var aJ=0;
  948. aJ<aL.length;
  949. aJ++){if(aL.length>1&&aL[aJ].length===0){continue
  950. }ar.responseBody=(aI)?a.util.trim(aL[aJ]):aL[aJ];
  951. if(ax==null&&Q!=null){Q(ar.responseBody)
  952. }if((ar.responseBody.length===0||(aI&&ak===ar.responseBody))&&ar.state==="messageReceived"){continue
  953. }al(ar);
  954. if(f.length>0){if(w("debug")){a.util.debug("Invoking "+f.length+" global callbacks: "+ar.state)
  955. }try{a.util.each(f,aK)
  956. }catch(aM){a.util.log(p.logLevel,["Callback exception"+aM])
  957. }}if(typeof(p.callback)==="function"){if(w("debug")){a.util.debug("Invoking request callbacks")
  958. }try{p.callback(ar)
  959. }catch(aM){a.util.log(p.logLevel,["Callback exception"+aM])
  960. }}}}this.subscribe=function(aI){ao(aI);
  961. aa()
  962. };
  963. this.execute=function(){aa()
  964. };
  965. this.close=function(){H()
  966. };
  967. this.disconnect=function(){D()
  968. };
  969. this.getUrl=function(){return p.url
  970. };
  971. this.push=function(aK,aJ){if(aJ!=null){var aI=p.dispatchUrl;
  972. p.dispatchUrl=aJ;
  973. t(aK);
  974. p.dispatchUrl=aI
  975. }else{t(aK)
  976. }};
  977. this.getUUID=function(){return p.uuid
  978. };
  979. this.pushLocal=function(aI){aq(aI)
  980. };
  981. this.enableProtocol=function(aI){return p.enableProtocol
  982. };
  983. this.init=function(){aj()
  984. };
  985. this.request=p;
  986. this.response=ar
  987. }};
  988. a.subscribe=function(h,k,j){if(typeof(k)==="function"){a.addCallback(k)
  989. }if(typeof(h)!=="string"){j=h
  990. }else{j.url=h
  991. }e=((typeof(j)!=="undefined")&&typeof(j.uuid)!=="undefined")?j.uuid:0;
  992. var i=new a.AtmosphereRequest(j);
  993. i.execute();
  994. g[g.length]=i;
  995. return i
  996. };
  997. a.unsubscribe=function(){if(g.length>0){var h=[].concat(g);
  998. for(var k=0;
  999. k<h.length;
  1000. k++){var j=h[k];
  1001. j.close();
  1002. clearTimeout(j.response.request.id);
  1003. if(j.heartbeatTimer){clearTimeout(j.heartbeatTimer)
  1004. }}}g=[];
  1005. f=[]
  1006. };
  1007. a.unsubscribeUrl=function(j){var h=-1;
  1008. if(g.length>0){for(var l=0;
  1009. l<g.length;
  1010. l++){var k=g[l];
  1011. if(k.getUrl()===j){k.close();
  1012. clearTimeout(k.response.request.id);
  1013. if(k.heartbeatTimer){clearTimeout(k.heartbeatTimer)
  1014. }h=l;
  1015. break
  1016. }}}if(h>=0){g.splice(h,1)
  1017. }};
  1018. a.addCallback=function(h){if(a.util.inArray(h,f)===-1){f.push(h)
  1019. }};
  1020. a.removeCallback=function(i){var h=a.util.inArray(i,f);
  1021. if(h!==-1){f.splice(h,1)
  1022. }};
  1023. a.util={browser:{},parseHeaders:function(i){var h,k=/^(.*?):[ \t]*([^\r\n]*)\r?$/mg,j={};
  1024. while(h=k.exec(i)){j[h[1]]=h[2]
  1025. }return j
  1026. },now:function(){return new Date().getTime()
  1027. },isArray:function(h){return Object.prototype.toString.call(h)==="[object Array]"
  1028. },inArray:function(k,l){if(!Array.prototype.indexOf){var h=l.length;
  1029. for(var j=0;
  1030. j<h;
  1031. ++j){if(l[j]===k){return j
  1032. }}return -1
  1033. }return l.indexOf(k)
  1034. },isBinary:function(h){return/^\[object\s(?:Blob|ArrayBuffer|.+Array)\]$/.test(Object.prototype.toString.call(h))
  1035. },isFunction:function(h){return Object.prototype.toString.call(h)==="[object Function]"
  1036. },getAbsoluteURL:function(h){if(typeof(document.createElement)==="undefined"){return h
  1037. }var i=document.createElement("div");
  1038. i.innerHTML='<a href="'+h+'"/>';
  1039. return encodeURI(decodeURI(i.firstChild.href))
  1040. },prepareURL:function(i){var j=a.util.now();
  1041. var h=i.replace(/([?&])_=[^&]*/,"$1_="+j);
  1042. return h+(h===i?(/\?/.test(i)?"&":"?")+"_="+j:"")
  1043. },trim:function(h){if(!String.prototype.trim){return h.toString().replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ")
  1044. }else{return h.toString().trim()
  1045. }},param:function(l){var j,h=[];
  1046. function k(m,n){n=a.util.isFunction(n)?n():(n==null?"":n);
  1047. h.push(encodeURIComponent(m)+"="+encodeURIComponent(n))
  1048. }function i(n,o){var m;
  1049. if(a.util.isArray(o)){a.util.each(o,function(q,p){if(/\[\]$/.test(n)){k(n,p)
  1050. }else{i(n+"["+(typeof p==="object"?q:"")+"]",p)
  1051. }})
  1052. }else{if(Object.prototype.toString.call(o)==="[object Object]"){for(m in o){i(n+"["+m+"]",o[m])
  1053. }}else{k(n,o)
  1054. }}}for(j in l){i(j,l[j])
  1055. }return h.join("&").replace(/%20/g,"+")
  1056. },storage:function(){try{return !!(window.localStorage&&window.StorageEvent)
  1057. }catch(h){return false
  1058. }},iterate:function(j,i){var k;
  1059. i=i||0;
  1060. (function h(){k=setTimeout(function(){if(j()===false){return
  1061. }h()
  1062. },i)
  1063. })();
  1064. return function(){clearTimeout(k)
  1065. }
  1066. },each:function(n,o,j){if(!n){return
  1067. }var m,k=0,l=n.length,h=a.util.isArray(n);
  1068. if(j){if(h){for(;
  1069. k<l;
  1070. k++){m=o.apply(n[k],j);
  1071. if(m===false){break
  1072. }}}else{for(k in n){m=o.apply(n[k],j);
  1073. if(m===false){break
  1074. }}}}else{if(h){for(;
  1075. k<l;
  1076. k++){m=o.call(n[k],k,n[k]);
  1077. if(m===false){break
  1078. }}}else{for(k in n){m=o.call(n[k],k,n[k]);
  1079. if(m===false){break
  1080. }}}}return n
  1081. },extend:function(l){var k,j,h;
  1082. for(k=1;
  1083. k<arguments.length;
  1084. k++){if((j=arguments[k])!=null){for(h in j){l[h]=j[h]
  1085. }}}return l
  1086. },on:function(j,i,h){if(j.addEventListener){j.addEventListener(i,h,false)
  1087. }else{if(j.attachEvent){j.attachEvent("on"+i,h)
  1088. }}},off:function(j,i,h){if(j.removeEventListener){j.removeEventListener(i,h,false)
  1089. }else{if(j.detachEvent){j.detachEvent("on"+i,h)
  1090. }}},log:function(j,i){if(window.console){var h=window.console[j];
  1091. if(typeof h==="function"){h.apply(window.console,i)
  1092. }}},warn:function(){a.util.log("warn",arguments)
  1093. },info:function(){a.util.log("info",arguments)
  1094. },debug:function(){a.util.log("debug",arguments)
  1095. },error:function(){a.util.log("error",arguments)
  1096. },xhr:function(){try{return new window.XMLHttpRequest()
  1097. }catch(i){try{return new window.ActiveXObject("Microsoft.XMLHTTP")
  1098. }catch(h){}}},checkCORSSupport:function(){if(a.util.browser.msie&&!window.XDomainRequest&&+a.util.browser.version.split(".")[0]<11){return true
  1099. }else{if(a.util.browser.opera&&+a.util.browser.version.split(".")<12){return true
  1100. }else{if(a.util.trim(navigator.userAgent).slice(0,16)==="KreaTVWebKit/531"){return true
  1101. }else{if(a.util.trim(navigator.userAgent).slice(-7).toLowerCase()==="kreatel"){return true
  1102. }}}}var i=navigator.userAgent.toLowerCase();
  1103. var j=i.match(/.+android ([0-9]{1,2})/i),h=parseInt((j&&j[0])||-1,10);
  1104. if(!isNaN(h)&&h>-1&&h<3){return true
  1105. }return false
  1106. }};
  1107. d=a.util.now();
  1108. (function(){var i=navigator.userAgent.toLowerCase(),h=/(chrome)[ \/]([\w.]+)/.exec(i)||/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(i)||/(msie) ([\w.]+)/.exec(i)||/(trident)(?:.*? rv:([\w.]+)|)/.exec(i)||i.indexOf("android")<0&&/version\/(.+) (safari)/.exec(i)||i.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(i)||[];
  1109. if(h[2]==="safari"){h[2]=h[1];
  1110. h[1]="safari"
  1111. }a.util.browser[h[1]||""]=true;
  1112. a.util.browser.version=h[2]||"0";
  1113. a.util.browser.vmajor=a.util.browser.version.split(".")[0];
  1114. if(a.util.browser.trident){a.util.browser.msie=true
  1115. }if(a.util.browser.msie||(a.util.browser.mozilla&&+a.util.browser.version.split(".")[0]===1)){a.util.storage=false
  1116. }})();
  1117. a.callbacks={unload:function(){a.util.debug(new Date()+" Atmosphere: unload event");
  1118. a.unsubscribe()
  1119. },beforeUnload:function(){a.util.debug(new Date()+" Atmosphere: beforeunload event");
  1120. a._beforeUnloadState=true;
  1121. setTimeout(function(){a.util.debug(new Date()+" Atmosphere: beforeunload event timeout reached. Reset _beforeUnloadState flag");
  1122. a._beforeUnloadState=false
  1123. },5000)
  1124. },offline:function(){a.util.debug(new Date()+" Atmosphere: offline event");
  1125. c=true;
  1126. if(g.length>0){var h=[].concat(g);
  1127. for(var k=0;
  1128. k<h.length;
  1129. k++){var j=h[k];
  1130. if(j.request.handleOnlineOffline){j.close();
  1131. clearTimeout(j.response.request.id);
  1132. if(j.heartbeatTimer){clearTimeout(j.heartbeatTimer)
  1133. }}}}},online:function(){a.util.debug(new Date()+" Atmosphere: online event");
  1134. if(g.length>0){for(var h=0;
  1135. h<g.length;
  1136. h++){if(g[h].request.handleOnlineOffline){g[h].init();
  1137. g[h].execute()
  1138. }}}c=false
  1139. }};
  1140. a.bindEvents=function(){a.util.on(window,"unload",a.callbacks.unload);
  1141. a.util.on(window,"beforeunload",a.callbacks.beforeUnload);
  1142. a.util.on(window,"offline",a.callbacks.offline);
  1143. a.util.on(window,"online",a.callbacks.online)
  1144. };
  1145. a.unbindEvents=function(){a.util.off(window,"unload",a.callbacks.unload);
  1146. a.util.off(window,"beforeunload",a.callbacks.beforeUnload);
  1147. a.util.off(window,"offline",a.callbacks.offline);
  1148. a.util.off(window,"online",a.callbacks.online)
  1149. };
  1150. a.bindEvents();
  1151. return a
  1152. }));