longPollTransportFormEncoded.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // wrapped by build app
  2. define("dojox/cometd/longPollTransportFormEncoded", ["dijit","dojo","dojox","dojo/require!dojox/cometd/_base"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.cometd.longPollTransportFormEncoded");
  4. dojo.require("dojox.cometd._base");
  5. dojox.cometd.longPollTransportFormEncoded = new function(){
  6. // This is an alternative implementation to that provided in logPollTransport.js that
  7. // form encodes all messages instead of sending them as text/json
  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: dojo.toJson([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. var interval = this._cometd._interval();
  30. if (this._cometd._status=="connected") {
  31. setTimeout(dojo.hitch(this, "_connect"), interval);
  32. }else{
  33. setTimeout(dojo.hitch(this._cometd, function(){
  34. this.init(this.url, this._props);
  35. }), interval);
  36. }
  37. }
  38. this._connect = function(){
  39. if(!this._cometd._initialized){ return; }
  40. if(this._cometd._polling) { return; }
  41. if((this._cometd._advice) && (this._cometd._advice["reconnect"]=="handshake")){
  42. this._cometd._status="unconnected"; //?
  43. this._initialized = false;
  44. this._cometd.init(this._cometd.url, this._cometd._props);
  45. }else if(this._cometd._status=="connected"){
  46. var message = {
  47. channel: "/meta/connect",
  48. connectionType: this._connectionType,
  49. clientId: this._cometd.clientId,
  50. id: "" + this._cometd.messageId++
  51. };
  52. if(this._cometd.connectTimeout>=this._cometd.expectedNetworkDelay){
  53. message.advice = {
  54. timeout: this._cometd.connectTimeout - this._cometd.expectedNetworkDelay
  55. };
  56. }
  57. message = this._cometd._extendOut(message);
  58. this.openTunnelWith({ message: dojo.toJson([message]) });
  59. }
  60. }
  61. this.deliver = function(message){
  62. // Nothing to do
  63. }
  64. this.openTunnelWith = function(content, url){
  65. this._cometd._polling = true;
  66. var post = {
  67. url: (url||this._cometd.url),
  68. content: content,
  69. handleAs: this._cometd.handleAs,
  70. load: dojo.hitch(this, function(data){
  71. this._cometd._polling=false;
  72. this._cometd.deliver(data);
  73. this._cometd._backon();
  74. this.tunnelCollapse();
  75. }),
  76. error: dojo.hitch(this, function(err){
  77. var metaMsg = {
  78. failure: true,
  79. error: err,
  80. advice: this._cometd._advice
  81. };
  82. this._cometd._polling=false;
  83. this._cometd._publishMeta("connect",false, metaMsg);
  84. this._cometd._backoff();
  85. this.tunnelCollapse();
  86. })
  87. };
  88. var connectTimeout = this._cometd._connectTimeout();
  89. if(connectTimeout > 0){
  90. post.timeout = connectTimeout;
  91. }
  92. this._poll = dojo.xhrPost(post);
  93. }
  94. this.sendMessages = function(messages){
  95. for(var i=0; i < messages.length; i++){
  96. messages[i].clientId = this._cometd.clientId;
  97. messages[i].id = "" + this._cometd.messageId++;
  98. messages[i]= this._cometd._extendOut(messages[i]);
  99. }
  100. return dojo.xhrPost({
  101. url: this._cometd.url||dojo.config["cometdRoot"],
  102. handleAs: this._cometd.handleAs,
  103. load: dojo.hitch(this._cometd, "deliver"),
  104. content: { message: dojo.toJson(messages) },
  105. error: dojo.hitch(this, function(err){
  106. this._cometd._publishMeta("publish",false,{messages:messages});
  107. }),
  108. timeout: this._cometd.expectedNetworkDelay
  109. });
  110. }
  111. this.startup = function(handshakeData){
  112. if(this._cometd._status=="connected"){ return; }
  113. this.tunnelInit();
  114. }
  115. this.disconnect = function(){
  116. var message = {
  117. channel: "/meta/disconnect",
  118. clientId: this._cometd.clientId,
  119. id: "" + this._cometd.messageId++
  120. };
  121. message = this._cometd._extendOut(message);
  122. dojo.xhrPost({
  123. url: this._cometd.url || dojo.config["cometdRoot"],
  124. handleAs: this._cometd.handleAs,
  125. content: { message: dojo.toJson([message]) }
  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.longPollTransportFormEncoded;
  140. dojox.cometd.connectionTypes.register("long-polling", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportFormEncoded);
  141. dojox.cometd.connectionTypes.register("long-polling-form-encoded", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportFormEncoded);
  142. });