root / trunk / web / dojo / dojox / xmpp / bosh.js
History | View | Annotate | Download (3.44 KB)
1 |
/*
|
---|---|
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.xmpp.bosh"]){ |
9 |
dojo._hasResource["dojox.xmpp.bosh"]=true; |
10 |
dojo.provide("dojox.xmpp.bosh");
|
11 |
dojo.require("dojo.io.script");
|
12 |
dojo.require("dojo.io.iframe");
|
13 |
dojo.require("dojox.xml.parser");
|
14 |
dojox.xmpp.bosh={transportIframes:[],initialize:function(_1){ |
15 |
this.transportIframes=[];
|
16 |
var _2=dojox._scopeName+".xmpp.bosh"; |
17 |
var c=dojo.connect(dojo.getObject(_2),"_iframeOnload",this,function(_3){ |
18 |
if(_3==0){ |
19 |
_1.load(); |
20 |
dojo.disconnect(c); |
21 |
} |
22 |
}); |
23 |
for(var i=0;i<_1.iframes;i++){ |
24 |
var _4="xmpp-transport-"+i; |
25 |
var _5=dojo.byId("xmpp-transport-"+i); |
26 |
if(_5){
|
27 |
if(window[_4]){
|
28 |
window[_4]=null;
|
29 |
} |
30 |
if(window.frames[_4]){
|
31 |
window.frames[_4]=null;
|
32 |
} |
33 |
dojo.destroy(_5); |
34 |
} |
35 |
_5=dojo.io.iframe.create("xmpp-transport-"+i,_2+"._iframeOnload("+i+");"); |
36 |
this.transportIframes.push(_5);
|
37 |
} |
38 |
},_iframeOnload:function(_6){ |
39 |
var _7=dojo.io.iframe.doc(dojo.byId("xmpp-transport-"+_6)); |
40 |
_7.write("<script>var isLoaded=true; var rid=0; var transmiting=false; function _BOSH_(msg) { transmiting=false; parent.dojox.xmpp.bosh.handle(msg, rid); } </script>");
|
41 |
},findOpenIframe:function(){ |
42 |
for(var i=0;i<this.transportIframes.length;i++){ |
43 |
var _8=this.transportIframes[i]; |
44 |
var _9=_8.contentWindow;
|
45 |
if(_9.isLoaded&&!_9.transmiting){
|
46 |
return _8;
|
47 |
} |
48 |
} |
49 |
return false; |
50 |
},handle:function(_a,_b){ |
51 |
var _c=this["rid"+_b]; |
52 |
var _d=dojox.xml.parser.parse(_a,"text/xml"); |
53 |
if(_d){
|
54 |
_c.ioArgs.xmppMessage=_d; |
55 |
}else{
|
56 |
_c.errback(new Error("Recieved bad document from server: "+_a)); |
57 |
} |
58 |
},get:function(_e){ |
59 |
var _f=this.findOpenIframe(); |
60 |
var _10=dojo.io.iframe.doc(_f);
|
61 |
_e.frameDoc=_10; |
62 |
var dfd=this._makeScriptDeferred(_e); |
63 |
var _11=dfd.ioArgs;
|
64 |
_f.contentWindow.rid=_11.rid; |
65 |
_f.contentWindow.transmiting=true;
|
66 |
dojo._ioAddQueryToUrl(_11); |
67 |
dojo._ioNotifyStart(dfd); |
68 |
dojo.io.script.attach(_11.id,_11.url,_10); |
69 |
dojo._ioWatch(dfd,this._validCheck,this._ioCheck,this._resHandle); |
70 |
return dfd;
|
71 |
},remove:function(id,_12){ |
72 |
dojo.destroy(dojo.byId(id,_12)); |
73 |
if(this[id]){ |
74 |
delete this[id]; |
75 |
} |
76 |
},_makeScriptDeferred:function(_13){ |
77 |
var dfd=dojo._ioSetArgs(_13,this._deferredCancel,this._deferredOk,this._deferredError); |
78 |
var _14=dfd.ioArgs;
|
79 |
_14.id="rid"+_13.rid;
|
80 |
_14.rid=_13.rid; |
81 |
_14.canDelete=true;
|
82 |
_14.frameDoc=_13.frameDoc; |
83 |
this[_14.id]=dfd;
|
84 |
return dfd;
|
85 |
},_deferredCancel:function(dfd){ |
86 |
dfd.canceled=true;
|
87 |
if(dfd.ioArgs.canDelete){
|
88 |
dojox.xmpp.bosh._addDeadScript(dfd.ioArgs); |
89 |
} |
90 |
},_deferredOk:function(dfd){ |
91 |
var _15=dfd.ioArgs;
|
92 |
if(_15.canDelete){
|
93 |
dojox.xmpp.bosh._addDeadScript(_15); |
94 |
} |
95 |
return _15.xmppMessage||_15;
|
96 |
},_deferredError:function(_16,dfd){ |
97 |
if(dfd.ioArgs.canDelete){
|
98 |
if(_16.dojoType=="timeout"){ |
99 |
dojox.xmpp.bosh.remove(dfd.ioArgs.id,dfd.ioArgs.frameDoc); |
100 |
}else{
|
101 |
dojox.xmpp.bosh._addDeadScript(dfd.ioArgs); |
102 |
} |
103 |
} |
104 |
return _16;
|
105 |
},_deadScripts:[],_addDeadScript:function(_17){ |
106 |
dojox.xmpp.bosh._deadScripts.push({id:_17.id,frameDoc:_17.frameDoc}); |
107 |
_17.frameDoc=null;
|
108 |
},_validCheck:function(dfd){ |
109 |
var _18=dojox.xmpp.bosh;
|
110 |
var _19=_18._deadScripts;
|
111 |
if(_19&&_19.length>0){ |
112 |
for(var i=0;i<_19.length;i++){ |
113 |
_18.remove(_19[i].id,_19[i].frameDoc); |
114 |
_19[i].frameDoc=null;
|
115 |
} |
116 |
dojox.xmpp.bosh._deadScripts=[]; |
117 |
} |
118 |
return true; |
119 |
},_ioCheck:function(dfd){ |
120 |
var _1a=dfd.ioArgs;
|
121 |
if(_1a.xmppMessage){
|
122 |
return true; |
123 |
} |
124 |
return false; |
125 |
},_resHandle:function(dfd){ |
126 |
if(dojox.xmpp.bosh._ioCheck(dfd)){
|
127 |
dfd.callback(dfd); |
128 |
}else{
|
129 |
dfd.errback(new Error("inconceivable dojox.xmpp.bosh._resHandle error")); |
130 |
} |
131 |
}}; |
132 |
} |