ack.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.ack"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.cometd.ack"] = true;
  8. dojo.provide("dojox.cometd.ack");
  9. dojo.require("dojox.cometd._base");
  10. /*
  11. * This file provides the dojox cometd ack extension which
  12. * acknowledges the messages received in /meta/connect responses.
  13. * Each meta/connect is sent with the id of the last successful meta/connect
  14. * received. The server uses this information to manage a queue of unacknowleged
  15. * messages.
  16. *
  17. * To use, add dojo.require("dojox.cometd.ack"); and if the handshake will be sent
  18. * with ext:{ack:true}. If the server supports the same extension, then the
  19. * mechanism will be initialized. The dojox.cometd.ackEnabled field may also be
  20. * used to optionally enable/disable the extension before init of cometd.
  21. *
  22. */
  23. dojox.cometd._ack = new function(){
  24. var supportAcks = false;
  25. var lastAck = -1;
  26. this._in = function(msg){
  27. if (msg.channel == "/meta/handshake") {
  28. supportAcks = msg.ext && msg.ext.ack;
  29. } else if (supportAcks && msg.channel == "/meta/connect" && msg.ext && msg.ext.ack && msg.successful) {
  30. var ackId = parseInt(msg.ext.ack);
  31. lastAck = ackId;
  32. }
  33. return msg;
  34. }
  35. this._out = function(msg){
  36. if (msg.channel == "/meta/handshake") {
  37. if (!msg.ext)
  38. msg.ext = {};
  39. msg.ext.ack = dojox.cometd.ackEnabled;
  40. lastAck = -1;
  41. }
  42. if (supportAcks && msg.channel == "/meta/connect") {
  43. if (!msg.ext)
  44. msg.ext = {};
  45. msg.ext.ack = lastAck;
  46. }
  47. return msg;
  48. }
  49. };
  50. dojox.cometd._extendInList.push(dojo.hitch(dojox.cometd._ack, "_in"));
  51. dojox.cometd._extendOutList.push(dojo.hitch(dojox.cometd._ack, "_out"));
  52. dojox.cometd.ackEnabled = true;
  53. }