/receipts/strophe.receipts.js

https://github.com/ankitsingh001/strophejs-plugins · JavaScript · 155 lines · 98 code · 24 blank · 33 comment · 13 complexity · f6ae5cd8aff945a52a65f840fd66d914 MD5 · raw file

  1. Strophe.addConnectionPlugin('receipts', {
  2. _conn: null,
  3. _msgQueue: {},
  4. _retries: {},
  5. _resendCount: 10,
  6. _resendTime: 9000,
  7. init: function(conn) {
  8. this._conn = conn;
  9. Strophe.addNamespace('RECEIPTS', 'urn:xmpp:receipts');
  10. },
  11. statusChanged: function (status) {
  12. if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
  13. // set up handlers for receipts
  14. //this._conn.addHandler(this._onRequestReceived.bind(this), Strophe.NS.RECEIPTS, "message");
  15. var that = this;
  16. setTimeout(function(){that.resendQueue();},5000);
  17. }
  18. },
  19. /*
  20. _onRequestReceived: function(msg){
  21. this._processReceipt(msg);
  22. return true;
  23. },
  24. * */
  25. /* sendMessage
  26. ** sends a message with a receipt and stores the message in the queue
  27. ** in case a receipt is never received
  28. **
  29. ** msg should be a builder
  30. */
  31. sendMessage: function(msg) {
  32. var id = this._conn.getUniqueId();
  33. msg.tree().setAttribute('id', id);
  34. var request = Strophe.xmlElement('request', {'xmlns': Strophe.NS.RECEIPTS});
  35. msg.tree().appendChild(request);
  36. this._msgQueue[id] = msg;
  37. this._retries[id] = 0;
  38. this._conn.send(msg);
  39. this.resendMessage(id);
  40. return id;
  41. },
  42. /*
  43. ** resend queued message
  44. */
  45. resendMessage: function(id){
  46. var that = this;
  47. setTimeout(function(){
  48. if (that._msgQueue[id]){
  49. // if we are disconnected, dont increment retries count and retry later
  50. if (!that._conn.connected) {
  51. that.resendMessage(id);
  52. return;
  53. }
  54. that._retries[id]++;
  55. if (that._retries[id] > that._resendCount) {
  56. //TODO: use mod_rest to force injection of the message
  57. //console.debug('message could not be delivered after ' + that._resendCount + ' attempts');
  58. return;
  59. }
  60. // FIX: use our actual jid in case we disconnected and changed jid
  61. that._msgQueue[id].tree().setAttribute('from', that._conn.jid);
  62. that._conn.send(that._msgQueue[id]);
  63. that.resendMessage(id);
  64. }
  65. },this._resendTime);
  66. },
  67. /* addMessageHandler
  68. ** add a message handler that handles XEP-0184 message receipts
  69. */
  70. addReceiptHandler: function(handler, type, from, options) {
  71. var that = this;
  72. var proxyHandler = function(msg) {
  73. that._processReceipt(msg);
  74. // call original handler
  75. return handler(msg);
  76. };
  77. this._conn.addHandler(proxyHandler, Strophe.NS.RECEIPTS, 'message',
  78. type, null, from, options);
  79. },
  80. /*
  81. * process a XEP-0184 message receipts
  82. * send recept on request
  83. * remove msg from queue on received
  84. */
  85. _processReceipt: function(msg){
  86. var id = msg.getAttribute('id'),
  87. from = msg.getAttribute('from'),
  88. req = msg.getElementsByTagName('request'),
  89. rec = msg.getElementsByTagName('received');
  90. // check for request in message
  91. if (req.length > 0) {
  92. // send receipt
  93. var out = $msg({to: from, from: this._conn.jid, id: this._conn.getUniqueId()}),
  94. request = Strophe.xmlElement('received', {'xmlns': Strophe.NS.RECEIPTS, 'id': id});
  95. out.tree().appendChild(request);
  96. this._conn.send(out);
  97. }
  98. // check for received
  99. if (rec.length > 0) {
  100. var recv_id = rec[0].getAttribute('id');
  101. if (recv_id) { // delete msg from queue
  102. delete this._msgQueue[recv_id];
  103. delete this._retries[recv_id];
  104. }
  105. }
  106. },
  107. resendQueue: function(){
  108. if (!this._conn.connected) {
  109. var that = this;
  110. setTimeout(function(){that.resendQueue();},5000);
  111. return;
  112. }
  113. for (var id in this._msgQueue) {
  114. if (this._msgQueue.hasOwnProperty(id)) {
  115. this._conn.send(this._msgQueue[id]);
  116. }
  117. }
  118. },
  119. getUnreceivedMsgs: function() {
  120. var msgs = [];
  121. for (var id in this._msgQueue) {
  122. if (this._msgQueue.hasOwnProperty(id)) {
  123. msgs.push(this._msgQueue[id]);
  124. }
  125. }
  126. return msgs;
  127. },
  128. clearMessages: function() {
  129. this._msgQueue = {};
  130. }
  131. });