longPollTransportJsonEncoded.js 4.8 KB

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