root / trunk / web / dojo / dojox / cometd / longPollTransportJsonEncoded.js @ 13
History | View | Annotate | Download (4.09 KB)
| 1 | 9 | andrej.cim | /*
|
|---|---|---|---|
| 2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
| 3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
| 4 | see: http://dojotoolkit.org/license for details
|
||
| 5 | */
|
||
| 6 | |||
| 7 | |||
| 8 | if(!dojo._hasResource["dojox.cometd.longPollTransportJsonEncoded"]){ |
||
| 9 | dojo._hasResource["dojox.cometd.longPollTransportJsonEncoded"]=true; |
||
| 10 | dojo.provide("dojox.cometd.longPollTransportJsonEncoded");
|
||
| 11 | dojo.require("dojox.cometd._base");
|
||
| 12 | dojox.cometd.longPollTransportJsonEncoded=new function(){ |
||
| 13 | this._connectionType="long-polling"; |
||
| 14 | this._cometd=null; |
||
| 15 | this.check=function(_1,_2,_3){ |
||
| 16 | return ((!_3)&&(dojo.indexOf(_1,"long-polling")>=0)); |
||
| 17 | }; |
||
| 18 | this.tunnelInit=function(){ |
||
| 19 | var _4={channel:"/meta/connect",clientId:this._cometd.clientId,connectionType:this._connectionType,id:""+this._cometd.messageId++}; |
||
| 20 | _4=this._cometd._extendOut(_4);
|
||
| 21 | this.openTunnelWith([_4]);
|
||
| 22 | }; |
||
| 23 | this.tunnelCollapse=function(){ |
||
| 24 | if(!this._cometd._initialized){ |
||
| 25 | return;
|
||
| 26 | } |
||
| 27 | if(this._cometd._advice&&this._cometd._advice["reconnect"]=="none"){ |
||
| 28 | return;
|
||
| 29 | } |
||
| 30 | if(this._cometd._status=="connected"){ |
||
| 31 | setTimeout(dojo.hitch(this,function(){ |
||
| 32 | this._connect();
|
||
| 33 | }),this._cometd._interval());
|
||
| 34 | }else{
|
||
| 35 | setTimeout(dojo.hitch(this._cometd,function(){ |
||
| 36 | this.init(this.url,this._props); |
||
| 37 | }),this._cometd._interval());
|
||
| 38 | } |
||
| 39 | }; |
||
| 40 | this._connect=function(){ |
||
| 41 | if(!this._cometd._initialized){ |
||
| 42 | return;
|
||
| 43 | } |
||
| 44 | if(this._cometd._polling){ |
||
| 45 | return;
|
||
| 46 | } |
||
| 47 | if((this._cometd._advice)&&(this._cometd._advice["reconnect"]=="handshake")){ |
||
| 48 | this._cometd._status="unconnected"; |
||
| 49 | this._initialized=false; |
||
| 50 | this._cometd.init(this._cometd.url,this._cometd._props); |
||
| 51 | }else{
|
||
| 52 | if(this._cometd._status=="connected"){ |
||
| 53 | var _5={channel:"/meta/connect",connectionType:this._connectionType,clientId:this._cometd.clientId,id:""+this._cometd.messageId++}; |
||
| 54 | if(this._cometd.connectTimeout>=this._cometd.expectedNetworkDelay){ |
||
| 55 | _5.advice={timeout:(this._cometd.connectTimeout-this._cometd.expectedNetworkDelay)};
|
||
| 56 | } |
||
| 57 | _5=this._cometd._extendOut(_5);
|
||
| 58 | this.openTunnelWith([_5]);
|
||
| 59 | } |
||
| 60 | } |
||
| 61 | }; |
||
| 62 | this.deliver=function(_6){ |
||
| 63 | }; |
||
| 64 | this.openTunnelWith=function(_7,_8){ |
||
| 65 | this._cometd._polling=true; |
||
| 66 | var _9={url:(_8||this._cometd.url),postData:dojo.toJson(_7),contentType:"text/json;charset=UTF-8",handleAs:this._cometd.handleAs,load:dojo.hitch(this,function(_a){ |
||
| 67 | this._cometd._polling=false; |
||
| 68 | this._cometd.deliver(_a);
|
||
| 69 | this._cometd._backon();
|
||
| 70 | this.tunnelCollapse();
|
||
| 71 | }),error:dojo.hitch(this,function(_b){ |
||
| 72 | this._cometd._polling=false; |
||
| 73 | var _c={failure:true,error:_b,advice:this._cometd._advice}; |
||
| 74 | this._cometd._publishMeta("connect",false,_c); |
||
| 75 | this._cometd._backoff();
|
||
| 76 | this.tunnelCollapse();
|
||
| 77 | })}; |
||
| 78 | var _d=this._cometd._connectTimeout(); |
||
| 79 | if(_d>0){ |
||
| 80 | _9.timeout=_d; |
||
| 81 | } |
||
| 82 | this._poll=dojo.rawXhrPost(_9);
|
||
| 83 | }; |
||
| 84 | this.sendMessages=function(_e){ |
||
| 85 | for(var i=0;i<_e.length;i++){ |
||
| 86 | _e[i].clientId=this._cometd.clientId;
|
||
| 87 | _e[i].id=""+this._cometd.messageId++; |
||
| 88 | _e[i]=this._cometd._extendOut(_e[i]);
|
||
| 89 | } |
||
| 90 | return dojo.rawXhrPost({url:this._cometd.url||dojo.config["cometdRoot"],handleAs:this._cometd.handleAs,load:dojo.hitch(this._cometd,"deliver"),postData:dojo.toJson(_e),contentType:"text/json;charset=UTF-8",error:dojo.hitch(this,function(_f){ |
||
| 91 | this._cometd._publishMeta("publish",false,{messages:_e}); |
||
| 92 | }),timeout:this._cometd.expectedNetworkDelay}); |
||
| 93 | }; |
||
| 94 | this.startup=function(_10){ |
||
| 95 | if(this._cometd._status=="connected"){ |
||
| 96 | return;
|
||
| 97 | } |
||
| 98 | this.tunnelInit();
|
||
| 99 | }; |
||
| 100 | this.disconnect=function(){ |
||
| 101 | var _11={channel:"/meta/disconnect",clientId:this._cometd.clientId,id:""+this._cometd.messageId++}; |
||
| 102 | _11=this._cometd._extendOut(_11);
|
||
| 103 | dojo.rawXhrPost({url:this._cometd.url||dojo.config["cometdRoot"],handleAs:this._cometd.handleAs,postData:dojo.toJson([_11]),contentType:"text/json;charset=UTF-8"});
|
||
| 104 | }; |
||
| 105 | this.cancelConnect=function(){ |
||
| 106 | if(this._poll){ |
||
| 107 | this._poll.cancel();
|
||
| 108 | this._cometd._polling=false; |
||
| 109 | this._cometd._publishMeta("connect",false,{cancel:true}); |
||
| 110 | this._cometd._backoff();
|
||
| 111 | this.disconnect();
|
||
| 112 | this.tunnelCollapse();
|
||
| 113 | } |
||
| 114 | }; |
||
| 115 | }; |
||
| 116 | dojox.cometd.longPollTransport=dojox.cometd.longPollTransportJsonEncoded; |
||
| 117 | dojox.cometd.connectionTypes.register("long-polling",dojox.cometd.longPollTransport.check,dojox.cometd.longPollTransportJsonEncoded);
|
||
| 118 | dojox.cometd.connectionTypes.register("long-polling-json-encoded",dojox.cometd.longPollTransport.check,dojox.cometd.longPollTransportJsonEncoded);
|
||
| 119 | } |