root / trunk / web / dojo / dojox / cometd / _base.js @ 10
History | View | Annotate | Download (9.08 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._base"]){ |
||
9 | dojo._hasResource["dojox.cometd._base"]=true; |
||
10 | dojo.provide("dojox.cometd._base");
|
||
11 | dojo.require("dojo.AdapterRegistry");
|
||
12 | dojox.cometd={Connection:function(_1){ |
||
13 | dojo.mixin(this,{prefix:_1,_status:"unconnected",_handshook:false,_initialized:false,_polling:false,expectedNetworkDelay:10000,connectTimeout:0,version:"1.0",minimumVersion:"0.9",clientId:null,messageId:0,batch:0,_isXD:false,handshakeReturn:null,currentTransport:null,url:null,lastMessage:null,_messageQ:[],handleAs:"json",_advice:{},_backoffInterval:0,_backoffIncrement:1000,_backoffMax:60000,_deferredSubscribes:{},_deferredUnsubscribes:{},_subscriptions:[],_extendInList:[],_extendOutList:[]}); |
||
14 | this.state=function(){ |
||
15 | return this._status; |
||
16 | }; |
||
17 | this.init=function(_2,_3,_4){ |
||
18 | _3=_3||{}; |
||
19 | _3.version=this.version;
|
||
20 | _3.minimumVersion=this.minimumVersion;
|
||
21 | _3.channel="/meta/handshake";
|
||
22 | _3.id=""+this.messageId++; |
||
23 | this.url=_2||dojo.config["cometdRoot"]; |
||
24 | if(!this.url){ |
||
25 | throw "no cometd root"; |
||
26 | return null; |
||
27 | } |
||
28 | var _5="^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$"; |
||
29 | var _6=(""+window.location).match(new RegExp(_5)); |
||
30 | if(_6[4]){ |
||
31 | var _7=_6[4].split(":"); |
||
32 | var _8=_7[0]; |
||
33 | var _9=_7[1]||"80"; |
||
34 | _6=this.url.match(new RegExp(_5)); |
||
35 | if(_6[4]){ |
||
36 | _7=_6[4].split(":"); |
||
37 | var _a=_7[0]; |
||
38 | var _b=_7[1]||"80"; |
||
39 | this._isXD=((_a!=_8)||(_b!=_9));
|
||
40 | } |
||
41 | } |
||
42 | if(!this._isXD){ |
||
43 | _3.supportedConnectionTypes=dojo.map(dojox.cometd.connectionTypes.pairs,"return item[0]");
|
||
44 | } |
||
45 | _3=this._extendOut(_3);
|
||
46 | var _c={url:this.url,handleAs:this.handleAs,content:{"message":dojo.toJson([_3])},load:dojo.hitch(this,function(_d){ |
||
47 | this._backon();
|
||
48 | this._finishInit(_d);
|
||
49 | }),error:dojo.hitch(this,function(e){ |
||
50 | this._backoff();
|
||
51 | this._finishInit(e);
|
||
52 | }),timeout:this.expectedNetworkDelay}; |
||
53 | if(_4){
|
||
54 | dojo.mixin(_c,_4); |
||
55 | } |
||
56 | this._props=_3;
|
||
57 | for(var _e in this._subscriptions){ |
||
58 | for(var _f in this._subscriptions[_e]){ |
||
59 | if(this._subscriptions[_e][_f].topic){ |
||
60 | dojo.unsubscribe(this._subscriptions[_e][_f].topic);
|
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | this._messageQ=[];
|
||
65 | this._subscriptions=[];
|
||
66 | this._initialized=true; |
||
67 | this._status="handshaking"; |
||
68 | this.batch=0; |
||
69 | this.startBatch();
|
||
70 | var r;
|
||
71 | if(this._isXD){ |
||
72 | _c.callbackParamName="jsonp";
|
||
73 | r=dojo.io.script.get(_c); |
||
74 | }else{
|
||
75 | r=dojo.xhrPost(_c); |
||
76 | } |
||
77 | return r;
|
||
78 | }; |
||
79 | this.publish=function(_10,_11,_12){ |
||
80 | var _13={data:_11,channel:_10}; |
||
81 | if(_12){
|
||
82 | dojo.mixin(_13,_12); |
||
83 | } |
||
84 | this._sendMessage(_13);
|
||
85 | }; |
||
86 | this.subscribe=function(_14,_15,_16,_17){ |
||
87 | _17=_17||{}; |
||
88 | if(_15){
|
||
89 | var _18=_1+_14;
|
||
90 | var _19=this._subscriptions[_18]; |
||
91 | if(!_19||_19.length==0){ |
||
92 | _19=[]; |
||
93 | _17.channel="/meta/subscribe";
|
||
94 | _17.subscription=_14; |
||
95 | this._sendMessage(_17);
|
||
96 | var _1a=this._deferredSubscribes; |
||
97 | if(_1a[_14]){
|
||
98 | _1a[_14].cancel(); |
||
99 | delete _1a[_14];
|
||
100 | } |
||
101 | _1a[_14]=new dojo.Deferred();
|
||
102 | } |
||
103 | for(var i in _19){ |
||
104 | if(_19[i].objOrFunc===_15&&(!_19[i].funcName&&!_16||_19[i].funcName==_16)){
|
||
105 | return null; |
||
106 | } |
||
107 | } |
||
108 | var _1b=dojo.subscribe(_18,_15,_16);
|
||
109 | _19.push({topic:_1b,objOrFunc:_15,funcName:_16}); |
||
110 | this._subscriptions[_18]=_19;
|
||
111 | } |
||
112 | var ret=this._deferredSubscribes[_14]||{}; |
||
113 | ret.args=dojo._toArray(arguments);
|
||
114 | return ret;
|
||
115 | }; |
||
116 | this.unsubscribe=function(_1c,_1d,_1e,_1f){ |
||
117 | if((arguments.length==1)&&(!dojo.isString(_1c))&&(_1c.args)){ |
||
118 | return this.unsubscribe.apply(this,_1c.args); |
||
119 | } |
||
120 | var _20=_1+_1c;
|
||
121 | var _21=this._subscriptions[_20]; |
||
122 | if(!_21||_21.length==0){ |
||
123 | return null; |
||
124 | } |
||
125 | var s=0; |
||
126 | for(var i in _21){ |
||
127 | var sb=_21[i];
|
||
128 | if((!_1d)||(sb.objOrFunc===_1d&&(!sb.funcName&&!_1e||sb.funcName==_1e))){
|
||
129 | dojo.unsubscribe(_21[i].topic); |
||
130 | delete _21[i];
|
||
131 | }else{
|
||
132 | s++; |
||
133 | } |
||
134 | } |
||
135 | if(s==0){ |
||
136 | _1f=_1f||{}; |
||
137 | _1f.channel="/meta/unsubscribe";
|
||
138 | _1f.subscription=_1c; |
||
139 | delete this._subscriptions[_20]; |
||
140 | this._sendMessage(_1f);
|
||
141 | this._deferredUnsubscribes[_1c]=new dojo.Deferred(); |
||
142 | if(this._deferredSubscribes[_1c]){ |
||
143 | this._deferredSubscribes[_1c].cancel();
|
||
144 | delete this._deferredSubscribes[_1c]; |
||
145 | } |
||
146 | } |
||
147 | return this._deferredUnsubscribes[_1c]; |
||
148 | }; |
||
149 | this.disconnect=function(){ |
||
150 | for(var _22 in this._subscriptions){ |
||
151 | for(var sub in this._subscriptions[_22]){ |
||
152 | if(this._subscriptions[_22][sub].topic){ |
||
153 | dojo.unsubscribe(this._subscriptions[_22][sub].topic);
|
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | this._subscriptions=[];
|
||
158 | this._messageQ=[];
|
||
159 | if(this._initialized&&this.currentTransport){ |
||
160 | this._initialized=false; |
||
161 | this.currentTransport.disconnect();
|
||
162 | } |
||
163 | if(!this._polling){ |
||
164 | this._publishMeta("connect",false); |
||
165 | } |
||
166 | this._initialized=false; |
||
167 | this._handshook=false; |
||
168 | this._status="disconnected"; |
||
169 | this._publishMeta("disconnect",true); |
||
170 | }; |
||
171 | this.subscribed=function(_23,_24){ |
||
172 | }; |
||
173 | this.unsubscribed=function(_25,_26){ |
||
174 | }; |
||
175 | this.tunnelInit=function(_27,_28){ |
||
176 | }; |
||
177 | this.tunnelCollapse=function(){ |
||
178 | }; |
||
179 | this._backoff=function(){ |
||
180 | if(!this._advice){ |
||
181 | this._advice={reconnect:"retry",interval:0}; |
||
182 | }else{
|
||
183 | if(!this._advice.interval){ |
||
184 | this._advice.interval=0; |
||
185 | } |
||
186 | } |
||
187 | if(this._backoffInterval<this._backoffMax){ |
||
188 | this._backoffInterval+=this._backoffIncrement; |
||
189 | } |
||
190 | }; |
||
191 | this._backon=function(){ |
||
192 | this._backoffInterval=0; |
||
193 | }; |
||
194 | this._interval=function(){ |
||
195 | var i=this._backoffInterval+(this._advice?(this._advice.interval?this._advice.interval:0):0); |
||
196 | if(i>0){ |
||
197 | } |
||
198 | return i;
|
||
199 | }; |
||
200 | this._publishMeta=function(_29,_2a,_2b){ |
||
201 | try{
|
||
202 | var _2c={cometd:this,action:_29,successful:_2a,state:this.state()}; |
||
203 | if(_2b){
|
||
204 | dojo.mixin(_2c,_2b); |
||
205 | } |
||
206 | dojo.publish(this.prefix+"/meta",[_2c]); |
||
207 | } |
||
208 | catch(e){
|
||
209 | } |
||
210 | }; |
||
211 | this._finishInit=function(_2d){ |
||
212 | if(this._status!="handshaking"){ |
||
213 | return;
|
||
214 | } |
||
215 | var _2e=this._handshook; |
||
216 | var _2f=false; |
||
217 | var _30={};
|
||
218 | if(_2d instanceof Error){ |
||
219 | dojo.mixin(_30,{reestablish:false,failure:true,error:_2d,advice:this._advice}); |
||
220 | }else{
|
||
221 | _2d=_2d[0];
|
||
222 | _2d=this._extendIn(_2d);
|
||
223 | this.handshakeReturn=_2d;
|
||
224 | if(_2d["advice"]){ |
||
225 | this._advice=_2d.advice;
|
||
226 | } |
||
227 | _2f=_2d.successful?_2d.successful:false;
|
||
228 | if(_2d.version<this.minimumVersion){ |
||
229 | if(console.log){
|
||
230 | } |
||
231 | _2f=false;
|
||
232 | this._advice.reconnect="none"; |
||
233 | } |
||
234 | dojo.mixin(_30,{reestablish:_2f&&_2e,response:_2d}); |
||
235 | } |
||
236 | this._publishMeta("handshake",_2f,_30); |
||
237 | if(this._status!="handshaking"){ |
||
238 | return;
|
||
239 | } |
||
240 | if(_2f){
|
||
241 | this._status="connecting"; |
||
242 | this._handshook=true; |
||
243 | this.currentTransport=dojox.cometd.connectionTypes.match(_2d.supportedConnectionTypes,_2d.version,this._isXD); |
||
244 | var _31=this.currentTransport; |
||
245 | _31._cometd=this;
|
||
246 | _31.version=_2d.version; |
||
247 | this.clientId=_2d.clientId;
|
||
248 | this.tunnelInit=_31.tunnelInit&&dojo.hitch(_31,"tunnelInit"); |
||
249 | this.tunnelCollapse=_31.tunnelCollapse&&dojo.hitch(_31,"tunnelCollapse"); |
||
250 | _31.startup(_2d); |
||
251 | }else{
|
||
252 | if(!this._advice||this._advice["reconnect"]!="none"){ |
||
253 | setTimeout(dojo.hitch(this,"init",this.url,this._props),this._interval()); |
||
254 | } |
||
255 | } |
||
256 | }; |
||
257 | this._extendIn=function(_32){ |
||
258 | dojo.forEach(dojox.cometd._extendInList,function(f){
|
||
259 | _32=f(_32)||_32; |
||
260 | }); |
||
261 | return _32;
|
||
262 | }; |
||
263 | this._extendOut=function(_33){ |
||
264 | dojo.forEach(dojox.cometd._extendOutList,function(f){
|
||
265 | _33=f(_33)||_33; |
||
266 | }); |
||
267 | return _33;
|
||
268 | }; |
||
269 | this.deliver=function(_34){ |
||
270 | dojo.forEach(_34,this._deliver,this); |
||
271 | return _34;
|
||
272 | }; |
||
273 | this._deliver=function(_35){ |
||
274 | _35=this._extendIn(_35);
|
||
275 | if(!_35["channel"]){ |
||
276 | if(_35["success"]!==true){ |
||
277 | return;
|
||
278 | } |
||
279 | } |
||
280 | this.lastMessage=_35;
|
||
281 | if(_35.advice){
|
||
282 | this._advice=_35.advice;
|
||
283 | } |
||
284 | var _36=null; |
||
285 | if((_35["channel"])&&(_35.channel.length>5)&&(_35.channel.substr(0,5)=="/meta")){ |
||
286 | switch(_35.channel){
|
||
287 | case "/meta/connect": |
||
288 | var _37={response:_35}; |
||
289 | if(_35.successful){
|
||
290 | if(this._status!="connected"){ |
||
291 | this._status="connected"; |
||
292 | this.endBatch();
|
||
293 | } |
||
294 | } |
||
295 | if(this._initialized){ |
||
296 | this._publishMeta("connect",_35.successful,_37); |
||
297 | } |
||
298 | break;
|
||
299 | case "/meta/subscribe": |
||
300 | _36=this._deferredSubscribes[_35.subscription];
|
||
301 | try{
|
||
302 | if(!_35.successful){
|
||
303 | if(_36){
|
||
304 | _36.errback(new Error(_35.error));
|
||
305 | } |
||
306 | this.currentTransport.cancelConnect();
|
||
307 | return;
|
||
308 | } |
||
309 | if(_36){
|
||
310 | _36.callback(true);
|
||
311 | } |
||
312 | this.subscribed(_35.subscription,_35);
|
||
313 | } |
||
314 | catch(e){
|
||
315 | log.warn(e); |
||
316 | } |
||
317 | break;
|
||
318 | case "/meta/unsubscribe": |
||
319 | _36=this._deferredUnsubscribes[_35.subscription];
|
||
320 | try{
|
||
321 | if(!_35.successful){
|
||
322 | if(_36){
|
||
323 | _36.errback(new Error(_35.error));
|
||
324 | } |
||
325 | this.currentTransport.cancelConnect();
|
||
326 | return;
|
||
327 | } |
||
328 | if(_36){
|
||
329 | _36.callback(true);
|
||
330 | } |
||
331 | this.unsubscribed(_35.subscription,_35);
|
||
332 | } |
||
333 | catch(e){
|
||
334 | log.warn(e); |
||
335 | } |
||
336 | break;
|
||
337 | default:
|
||
338 | if(_35.successful&&!_35.successful){
|
||
339 | this.currentTransport.cancelConnect();
|
||
340 | return;
|
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | this.currentTransport.deliver(_35);
|
||
345 | if(_35.data){
|
||
346 | try{
|
||
347 | var _38=[_35];
|
||
348 | var _39=_1+_35.channel;
|
||
349 | var _3a=_35.channel.split("/"); |
||
350 | var _3b=_1;
|
||
351 | for(var i=1;i<_3a.length-1;i++){ |
||
352 | dojo.publish(_3b+"/**",_38);
|
||
353 | _3b+="/"+_3a[i];
|
||
354 | } |
||
355 | dojo.publish(_3b+"/**",_38);
|
||
356 | dojo.publish(_3b+"/*",_38);
|
||
357 | dojo.publish(_39,_38); |
||
358 | } |
||
359 | catch(e){
|
||
360 | } |
||
361 | } |
||
362 | }; |
||
363 | this._sendMessage=function(_3c){ |
||
364 | if(this.currentTransport&&!this.batch){ |
||
365 | return this.currentTransport.sendMessages([_3c]); |
||
366 | }else{
|
||
367 | this._messageQ.push(_3c);
|
||
368 | return null; |
||
369 | } |
||
370 | }; |
||
371 | this.startBatch=function(){ |
||
372 | this.batch++;
|
||
373 | }; |
||
374 | this.endBatch=function(){ |
||
375 | if(--this.batch<=0&&this.currentTransport&&this._status=="connected"){ |
||
376 | this.batch=0; |
||
377 | var _3d=this._messageQ; |
||
378 | this._messageQ=[];
|
||
379 | if(_3d.length>0){ |
||
380 | this.currentTransport.sendMessages(_3d);
|
||
381 | } |
||
382 | } |
||
383 | }; |
||
384 | this._onUnload=function(){ |
||
385 | dojo.addOnUnload(dojox.cometd,"disconnect");
|
||
386 | }; |
||
387 | this._connectTimeout=function(){ |
||
388 | var _3e=0; |
||
389 | if(this._advice&&this._advice.timeout&&this.expectedNetworkDelay>0){ |
||
390 | _3e=this._advice.timeout+this.expectedNetworkDelay; |
||
391 | } |
||
392 | if(this.connectTimeout>0&&this.connectTimeout<_3e){ |
||
393 | return this.connectTimeout; |
||
394 | } |
||
395 | return _3e;
|
||
396 | }; |
||
397 | },connectionTypes:new dojo.AdapterRegistry(true)}; |
||
398 | dojox.cometd.Connection.call(dojox.cometd,"/cometd");
|
||
399 | dojo.addOnUnload(dojox.cometd,"_onUnload");
|
||
400 | } |