/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml */ dojo.require("dojo.io.common"); dojo.provide("dojo.io.cometd"); dojo.require("dojo.AdapterRegistry"); dojo.require("dojo.json"); dojo.require("dojo.io.BrowserIO"); dojo.require("dojo.io.IframeIO"); dojo.require("dojo.io.ScriptSrcIO"); dojo.require("dojo.io.cookie"); dojo.require("dojo.event.*"); dojo.require("dojo.lang.common"); dojo.require("dojo.lang.func"); cometd=new function(){ this.initialized=false; this.connected=false; this.connectionTypes=new dojo.AdapterRegistry(true); this.version=0.1; this.minimumVersion=0.1; this.clientId=null; this._isXD=false; this.handshakeReturn=null; this.currentTransport=null; this.url=null; this.lastMessage=null; this.globalTopicChannels={}; this.backlog=[]; this.tunnelInit=function(_1,_2){ }; this.tunnelCollapse=function(){ dojo.debug("tunnel collapsed!"); }; this.init=function(_3,_4,_5){ _3=_3||{}; _3.version=this.version; _3.minimumVersion=this.minimumVersion; _3.channel="/meta/handshake"; this.url=_4||djConfig["cometdRoot"]; if(!this.url){ dojo.debug("no cometd root specified in djConfig and no root passed"); return; } var _6={url:this.url,method:"POST",mimetype:"text/json",load:dojo.lang.hitch(this,"finishInit"),content:{"message":dojo.json.serialize([_3])}}; var _7="^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$"; var r=(""+window.location).match(new RegExp(_7)); if(r[4]){ var _9=r[4].split(":"); var _a=_9[0]; var _b=_9[1]||"80"; r=this.url.match(new RegExp(_7)); if(r[4]){ _9=r[4].split(":"); var _c=_9[0]; var _d=_9[1]||"80"; if((_c!=_a)||(_d!=_b)){ dojo.debug(_a,_c); dojo.debug(_b,_d); this._isXD=true; _6.transport="ScriptSrcTransport"; _6.jsonParamName="jsonp"; _6.method="GET"; } } } if(_5){ dojo.lang.mixin(_6,_5); } return dojo.io.bind(_6); }; this.finishInit=function(_e,_f,evt,_11){ _f=_f[0]; this.handshakeReturn=_f; if(_f["authSuccessful"]==false){ dojo.debug("cometd authentication failed"); return; } if(_f.version5)&&(_15.channel.substr(0,5)=="/meta")){ switch(_15.channel){ case "/meta/subscribe": if(!_15.successful){ dojo.debug("cometd subscription error for channel",_15.channel,":",_15.error); return; } this.subscribed(_15.subscription,_15); break; case "/meta/unsubscribe": if(!_15.successful){ dojo.debug("cometd unsubscription error for channel",_15.channel,":",_15.error); return; } this.unsubscribed(_15.subscription,_15); break; } } this.currentTransport.deliver(_15); if(_15.data){ var _16=(this.globalTopicChannels[_15.channel])?_15.channel:"/cometd"+_15.channel; dojo.event.topic.publish(_16,_15); } }; this.disconnect=function(){ if(!this.currentTransport){ dojo.debug("no current transport to disconnect from"); return; } this.currentTransport.disconnect(); }; this.publish=function(_17,_18,_19){ if(!this.currentTransport){ this.backlog.push(["publish",_17,_18,_19]); return; } var _1a={data:_18,channel:_17}; if(_19){ dojo.lang.mixin(_1a,_19); } return this.currentTransport.sendMessage(_1a); }; this.subscribe=function(_1b,_1c,_1d,_1e){ if(!this.currentTransport){ this.backlog.push(["subscribe",_1b,_1c,_1d,_1e]); return; } if(_1d){ var _1f=(_1c)?_1b:"/cometd"+_1b; if(_1c){ this.globalTopicChannels[_1b]=true; } dojo.event.topic.subscribe(_1f,_1d,_1e); } return this.currentTransport.sendMessage({channel:"/meta/subscribe",subscription:_1b}); }; this.subscribed=function(_20,_21){ dojo.debug(_20); dojo.debugShallow(_21); }; this.unsubscribe=function(_22,_23,_24,_25){ if(!this.currentTransport){ this.backlog.push(["unsubscribe",_22,_23,_24,_25]); return; } if(_24){ var _26=(_23)?_22:"/cometd"+_22; dojo.event.topic.unsubscribe(_26,_24,_25); } return this.currentTransport.sendMessage({channel:"/meta/unsubscribe",subscription:_22}); }; this.unsubscribed=function(_27,_28){ dojo.debug(_27); dojo.debugShallow(_28); }; }; cometd.iframeTransport=new function(){ this.connected=false; this.connectionId=null; this.rcvNode=null; this.rcvNodeName=""; this.phonyForm=null; this.authToken=null; this.lastTimestamp=null; this.lastId=null; this.backlog=[]; this.check=function(_29,_2a,_2b){ return ((!_2b)&&(!dojo.render.html.safari)&&(dojo.lang.inArray(_29,"iframe"))); }; this.tunnelInit=function(){ this.postToIframe({message:dojo.json.serialize([{channel:"/meta/connect",clientId:cometd.clientId,connectionType:"iframe"}])}); }; this.tunnelCollapse=function(){ if(this.connected){ this.connected=false; this.postToIframe({message:dojo.json.serialize([{channel:"/meta/reconnect",clientId:cometd.clientId,connectionId:this.connectionId,timestamp:this.lastTimestamp,id:this.lastId}])}); } }; this.deliver=function(_2c){ if(_2c["timestamp"]){ this.lastTimestamp=_2c.timestamp; } if(_2c["id"]){ this.lastId=_2c.id; } if((_2c.channel.length>5)&&(_2c.channel.substr(0,5)=="/meta")){ switch(_2c.channel){ case "/meta/connect": if(!_2c.successful){ dojo.debug("cometd connection error:",_2c.error); return; } this.connectionId=_2c.connectionId; this.connected=true; this.processBacklog(); break; case "/meta/reconnect": if(!_2c.successful){ dojo.debug("cometd reconnection error:",_2c.error); return; } this.connected=true; break; case "/meta/subscribe": if(!_2c.successful){ dojo.debug("cometd subscription error for channel",_2c.channel,":",_2c.error); return; } dojo.debug(_2c.channel); break; } } }; this.widenDomain=function(_2d){ var cd=_2d||document.domain; if(cd.indexOf(".")==-1){ return; } var dps=cd.split("."); if(dps.length<=2){ return; } dps=dps.slice(dps.length-2); document.domain=dps.join("."); return document.domain; }; this.postToIframe=function(_30,url){ if(!this.phonyForm){ if(dojo.render.html.ie){ this.phonyForm=document.createElement("
"); dojo.body().appendChild(this.phonyForm); }else{ this.phonyForm=document.createElement("form"); this.phonyForm.style.display="none"; dojo.body().appendChild(this.phonyForm); this.phonyForm.enctype="application/x-www-form-urlencoded"; this.phonyForm.method="POST"; } } this.phonyForm.action=url||cometd.url; this.phonyForm.target=this.rcvNodeName; this.phonyForm.setAttribute("target",this.rcvNodeName); while(this.phonyForm.firstChild){ this.phonyForm.removeChild(this.phonyForm.firstChild); } for(var x in _30){ var tn; if(dojo.render.html.ie){ tn=document.createElement(""); this.phonyForm.appendChild(tn); }else{ tn=document.createElement("input"); this.phonyForm.appendChild(tn); tn.type="hidden"; tn.name=x; tn.value=_30[x]; } } this.phonyForm.submit(); }; this.processBacklog=function(){ while(this.backlog.length>0){ this.sendMessage(this.backlog.shift(),true); } }; this.sendMessage=function(_34,_35){ if((_35)||(this.connected)){ _34.connectionId=this.connectionId; _34.clientId=cometd.clientId; var _36={url:cometd.url||djConfig["cometdRoot"],method:"POST",mimetype:"text/json",content:{message:dojo.json.serialize([_34])}}; return dojo.io.bind(_36); }else{ this.backlog.push(_34); } }; this.startup=function(_37){ dojo.debug("startup!"); dojo.debug(dojo.json.serialize(_37)); if(this.connected){ return; } this.rcvNodeName="cometdRcv_"+cometd._getRandStr(); var _38=cometd.url+"/?tunnelInit=iframe"; if(false&&dojo.render.html.ie){ this.rcvNode=new ActiveXObject("htmlfile"); this.rcvNode.open(); this.rcvNode.write(""); this.rcvNode.write("