longPollTransportFormEncoded.js 5.0 KB

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