bosh.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // wrapped by build app
  2. define("dojox/xmpp/bosh", ["dijit","dojo","dojox","dojo/require!dojo/io/script,dojo/io/iframe,dojox/xml/parser"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.xmpp.bosh");
  4. dojo.require("dojo.io.script");
  5. dojo.require("dojo.io.iframe");
  6. dojo.require("dojox.xml.parser");
  7. /*=====
  8. dojo.declare("dojox.xmpp.bosh.__initArgs", null, {
  9. constructor: function(){
  10. // summary:
  11. // The arguments passed to dojox.xmpp.bosh.initialize
  12. // iframes:
  13. // The number of iframes to use for transmission
  14. // load:
  15. // The function called when the first iframe is
  16. // loaded. Generally used to signal when to send
  17. // login information
  18. this.iframes = iframes;
  19. this.load = load;
  20. }
  21. });
  22. dojo.declare("dojox.xmpp.bosh.__ioArgs", dojo.__IoArgs, {
  23. constructor: function(){
  24. // summary:
  25. // All the properties described in the dojo.__ioArgs type, apply to this
  26. // type as well, EXCEPT "handleAs". It is not applicable to
  27. // dojox.xmpp.bosh.get() calls, since it is implied that the
  28. // return will be a string of XML.
  29. // rid:
  30. // The rid of the message being sent.
  31. this.rid = rid;
  32. }
  33. });
  34. =====*/
  35. dojox.xmpp.bosh = {
  36. transportIframes: [],
  37. initialize: function(/*dojox.xmpp.bosh.__initArgs*/ args){
  38. this.transportIframes = [];
  39. var scopedObj = dojox._scopeName + '.xmpp.bosh';
  40. var c = dojo.connect(dojo.getObject(scopedObj), '_iframeOnload', this, function(index){
  41. if(index==0){
  42. args.load();
  43. dojo.disconnect(c);
  44. }
  45. });
  46. for(var i = 0; i < args.iframes; i++){
  47. var fname = 'xmpp-transport-'+i;
  48. var iframe = dojo.byId('xmpp-transport-'+i);
  49. if(iframe){
  50. // we have to clean up the dojo.io.iframe references
  51. if(window[fname]){ window[fname] = null; }
  52. if(window.frames[fname]){ window.frames[fname] = null; }
  53. dojo.destroy(iframe);
  54. }
  55. iframe = dojo.io.iframe.create("xmpp-transport-" + i, scopedObj + "._iframeOnload("+i+");" );
  56. this.transportIframes.push(iframe);
  57. }
  58. },
  59. _iframeOnload: function(index){
  60. var doc = dojo.io.iframe.doc(dojo.byId("xmpp-transport-" + index));
  61. doc.write("<script>var isLoaded=true; var rid=0; var transmiting=false; function _BOSH_(msg) { transmiting=false; parent.dojox.xmpp.bosh.handle(msg, rid); } </script>");
  62. },
  63. findOpenIframe: function() {
  64. for(var i = 0; i < this.transportIframes.length; i++) {
  65. var iframe = this.transportIframes[i];
  66. var win = iframe.contentWindow;
  67. //console.log("Open transport?", win, win.isLoaded, win.transmiting);
  68. if(win.isLoaded && !win.transmiting) {
  69. return iframe;
  70. }
  71. }
  72. return false;
  73. },
  74. handle: function(msg, rid){
  75. var dfd = this['rid'+rid];
  76. var xmlMsg = dojox.xml.parser.parse(msg, 'text/xml');
  77. if(xmlMsg){
  78. dfd.ioArgs.xmppMessage = xmlMsg;
  79. }else{
  80. dfd.errback(new Error("Recieved bad document from server: " + msg));
  81. }
  82. },
  83. get: function(/*dojox.xmpp.bosh.__ioArgs*/args){
  84. // summary:
  85. // sends a get request using a dynamically created script tag.
  86. var iframe = this.findOpenIframe();
  87. var iframeDoc = dojo.io.iframe.doc(iframe);
  88. args.frameDoc = iframeDoc;
  89. var dfd = this._makeScriptDeferred(args);
  90. var ioArgs = dfd.ioArgs;
  91. iframe.contentWindow.rid=ioArgs.rid;
  92. iframe.contentWindow.transmiting=true;
  93. dojo._ioAddQueryToUrl(ioArgs);
  94. dojo._ioNotifyStart(dfd);
  95. dojo.io.script.attach(ioArgs.id, ioArgs.url, iframeDoc);
  96. dojo._ioWatch(dfd, this._validCheck, this._ioCheck, this._resHandle);
  97. return dfd;
  98. },
  99. remove: function(/*String*/id, /*Document?*/frameDocument){
  100. //summary: removes the script element with the given id, from the given frameDocument.
  101. //If no frameDocument is passed, the current document is used.
  102. dojo.destroy(dojo.byId(id, frameDocument));
  103. //Remove the BOSH callback on dojox.xmpp.bosh, if it exists.
  104. if(this[id]){
  105. delete this[id];
  106. }
  107. },
  108. _makeScriptDeferred: function(/*Object*/args){
  109. //summary:
  110. // sets up a Deferred object for an IO request.
  111. var dfd = dojo._ioSetArgs(args, this._deferredCancel, this._deferredOk, this._deferredError);
  112. var ioArgs = dfd.ioArgs;
  113. ioArgs.id = 'rid' + args.rid;
  114. ioArgs.rid = args.rid;
  115. ioArgs.canDelete = true;
  116. ioArgs.frameDoc = args.frameDoc;
  117. this[ioArgs.id] = dfd;
  118. return dfd; // dojo.Deferred
  119. },
  120. _deferredCancel: function(/*Deferred*/dfd){
  121. //summary: canceller function for dojo._ioSetArgs call.
  122. //DO NOT use "this" and expect it to be dojox.xmpp.bosh.
  123. dfd.canceled = true;
  124. if(dfd.ioArgs.canDelete){
  125. dojox.xmpp.bosh._addDeadScript(dfd.ioArgs);
  126. }
  127. },
  128. _deferredOk: function(/*Deferred*/dfd){
  129. //summary: okHandler function for dojo._ioSetArgs call.
  130. //DO NOT use "this" and expect it to be dojo.xmpp.bosh.
  131. var ioArgs = dfd.ioArgs;
  132. //Add script to list of things that can be removed.
  133. if(ioArgs.canDelete){
  134. dojox.xmpp.bosh._addDeadScript(ioArgs);
  135. }
  136. //Favor JSONP responses, script load events then lastly ioArgs.
  137. //The ioArgs are goofy, but cannot return the dfd since that stops
  138. //the callback chain in Deferred. The return value is not that important
  139. //in that case, probably a checkString case.
  140. return ioArgs.xmppMessage || ioArgs;
  141. },
  142. _deferredError: function(/*Error*/error, /*Deferred*/dfd){
  143. //summary: errHandler function for dojo._ioSetArgs call.
  144. if(dfd.ioArgs.canDelete){
  145. //DO NOT use "this" and expect it to be dojox.xmpp.bosh
  146. if(error.dojoType == "timeout"){
  147. //For timeouts, remove the script element immediately to
  148. //avoid a response from it coming back later and causing trouble.
  149. dojox.xmpp.bosh.remove(dfd.ioArgs.id, dfd.ioArgs.frameDoc);
  150. }else{
  151. dojox.xmpp.bosh._addDeadScript(dfd.ioArgs);
  152. }
  153. }
  154. return error;
  155. },
  156. _deadScripts: [],
  157. _addDeadScript: function(/*Object*/ioArgs){
  158. //summary: sets up an entry in the deadScripts array.
  159. dojox.xmpp.bosh._deadScripts.push({id: ioArgs.id, frameDoc: ioArgs.frameDoc});
  160. //Being extra paranoid about leaks:
  161. ioArgs.frameDoc = null;
  162. },
  163. _validCheck: function(/*Deferred*/dfd){
  164. //summary: inflight check function to see if dfd is still valid.
  165. //Do script cleanup here. We wait for one inflight pass
  166. //to make sure we don't get any weird things by trying to remove a script
  167. //tag that is part of the call chain (IE 6 has been known to
  168. //crash in that case).
  169. var _self = dojox.xmpp.bosh;
  170. var deadScripts = _self._deadScripts;
  171. if(deadScripts && deadScripts.length > 0){
  172. for(var i = 0; i < deadScripts.length; i++){
  173. //Remove the script tag
  174. _self.remove(deadScripts[i].id, deadScripts[i].frameDoc);
  175. deadScripts[i].frameDoc = null;
  176. }
  177. dojox.xmpp.bosh._deadScripts = [];
  178. }
  179. return true;
  180. },
  181. _ioCheck: function(/*Deferred*/dfd){
  182. //summary: inflight check function to see if IO finished.
  183. var ioArgs = dfd.ioArgs;
  184. //Check for returned message
  185. if(ioArgs.xmppMessage){
  186. return true;
  187. }
  188. return false;
  189. },
  190. _resHandle: function(/*Deferred*/dfd){
  191. //summary: inflight function to handle a completed response.
  192. if(dojox.xmpp.bosh._ioCheck(dfd)){
  193. dfd.callback(dfd);
  194. }else{
  195. //This path should never happen since the only way we can get
  196. //to _resHandle is if _ioCheck is true.
  197. dfd.errback(new Error("inconceivable dojox.xmpp.bosh._resHandle error"));
  198. }
  199. }
  200. };
  201. });