longPollTransportJsonEncoded.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright (c) 2004-2012, 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. if(!dojo._hasResource["dojox.cometd.longPollTransportJsonEncoded"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.cometd.longPollTransportJsonEncoded"] = true;
  8. dojo.provide("dojox.cometd.longPollTransportJsonEncoded");
  9. dojo.require("dojox.cometd._base");
  10. dojox.cometd.longPollTransportJsonEncoded = new function(){
  11. // This is an alternative implementation to that provided in logPollTransportFormEncoded.js
  12. // that sends messages as text/json rather than form encoding them.
  13. this._connectionType="long-polling";
  14. this._cometd=null;
  15. this.check = function(types, version, xdomain){
  16. return ((!xdomain)&&(dojo.indexOf(types, "long-polling") >= 0));
  17. }
  18. this.tunnelInit = function(){
  19. var message = {
  20. channel: "/meta/connect",
  21. clientId: this._cometd.clientId,
  22. connectionType: this._connectionType,
  23. id: ""+this._cometd.messageId++
  24. };
  25. message=this._cometd._extendOut(message);
  26. this.openTunnelWith([message]);
  27. }
  28. this.tunnelCollapse = function(){
  29. // TODO handle transport specific advice
  30. if(!this._cometd._initialized){ return; }
  31. if(this._cometd._advice && this._cometd._advice["reconnect"]=="none"){
  32. return;
  33. }
  34. if (this._cometd._status=="connected") {
  35. setTimeout(dojo.hitch(this,function(){this._connect();}),this._cometd._interval());
  36. }else{
  37. setTimeout(dojo.hitch(this._cometd,function(){this.init(this.url,this._props);}),this._cometd._interval());
  38. }
  39. }
  40. this._connect = function(){
  41. if(!this._cometd._initialized){ return; }
  42. if(this._cometd._polling) {
  43. return;
  44. }
  45. if((this._cometd._advice) && (this._cometd._advice["reconnect"]=="handshake")){
  46. this._cometd._status="unconnected";
  47. this._initialized = false;
  48. this._cometd.init(this._cometd.url,this._cometd._props);
  49. }else if(this._cometd._status=="connected"){
  50. var message={
  51. channel: "/meta/connect",
  52. connectionType: this._connectionType,
  53. clientId: this._cometd.clientId,
  54. id: ""+this._cometd.messageId++
  55. };
  56. if (this._cometd.connectTimeout>=this._cometd.expectedNetworkDelay){
  57. message.advice={timeout:(this._cometd.connectTimeout-this._cometd.expectedNetworkDelay)};
  58. }
  59. message=this._cometd._extendOut(message);
  60. this.openTunnelWith([message]);
  61. }
  62. }
  63. this.deliver = function(message){
  64. // Nothing to do
  65. }
  66. this.openTunnelWith = function(messages, url){
  67. this._cometd._polling = true;
  68. var post = {
  69. url: (url||this._cometd.url),
  70. postData: dojo.toJson(messages),
  71. contentType: "text/json;charset=UTF-8",
  72. handleAs: this._cometd.handleAs,
  73. load: dojo.hitch(this, function(data){
  74. this._cometd._polling=false;
  75. this._cometd.deliver(data);
  76. this._cometd._backon();
  77. this.tunnelCollapse();
  78. }),
  79. error: dojo.hitch(this, function(err){
  80. this._cometd._polling=false;
  81. var metaMsg = {
  82. failure: true,
  83. error: err,
  84. advice: this._cometd._advice
  85. };
  86. this._cometd._publishMeta("connect",false, metaMsg);
  87. this._cometd._backoff();
  88. this.tunnelCollapse();
  89. })
  90. };
  91. var connectTimeout=this._cometd._connectTimeout();
  92. if (connectTimeout>0) {
  93. post.timeout=connectTimeout;
  94. }
  95. this._poll = dojo.rawXhrPost(post);
  96. }
  97. this.sendMessages = function(messages){
  98. for(var i=0; i<messages.length; i++){
  99. messages[i].clientId = this._cometd.clientId;
  100. messages[i].id = ""+this._cometd.messageId++;
  101. messages[i]=this._cometd._extendOut(messages[i]);
  102. }
  103. return dojo.rawXhrPost({
  104. url: this._cometd.url||dojo.config["cometdRoot"],
  105. handleAs: this._cometd.handleAs,
  106. load: dojo.hitch(this._cometd, "deliver"),
  107. postData: dojo.toJson(messages),
  108. contentType: "text/json;charset=UTF-8",
  109. error: dojo.hitch(this, function(err){
  110. this._cometd._publishMeta("publish",false,{messages:messages});
  111. }),
  112. timeout: this._cometd.expectedNetworkDelay
  113. });
  114. }
  115. this.startup = function(handshakeData){
  116. if(this._cometd._status=="connected"){ return; }
  117. this.tunnelInit();
  118. }
  119. this.disconnect = function(){
  120. var message = {
  121. channel: "/meta/disconnect",
  122. clientId: this._cometd.clientId,
  123. id: "" + this._cometd.messageId++
  124. };
  125. message = this._cometd._extendOut(message);
  126. dojo.rawXhrPost({
  127. url: this._cometd.url || dojo.config["cometdRoot"],
  128. handleAs: this._cometd.handleAs,
  129. postData: dojo.toJson([message]),
  130. contentType: "text/json;charset=UTF-8"
  131. });
  132. }
  133. this.cancelConnect = function(){
  134. if(this._poll){
  135. this._poll.cancel();
  136. this._cometd._polling=false;
  137. this._cometd._publishMeta("connect",false,{cancel:true});
  138. this._cometd._backoff();
  139. this.disconnect();
  140. this.tunnelCollapse();
  141. }
  142. }
  143. }
  144. dojox.cometd.longPollTransport = dojox.cometd.longPollTransportJsonEncoded;
  145. dojox.cometd.connectionTypes.register("long-polling", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportJsonEncoded);
  146. dojox.cometd.connectionTypes.register("long-polling-json-encoded", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportJsonEncoded);
  147. }