You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strophe.ping.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* global $, $iq, Strophe */
  2. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  3. /**
  4. * Ping every 20 sec
  5. */
  6. var PING_INTERVAL = 20000;
  7. /**
  8. * Ping timeout error after 15 sec of waiting.
  9. */
  10. var PING_TIMEOUT = 15000;
  11. /**
  12. * Will close the connection after 3 consecutive ping errors.
  13. */
  14. var PING_THRESHOLD = 3;
  15. /**
  16. * XEP-0199 ping plugin.
  17. *
  18. * Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
  19. */
  20. module.exports = function (XMPP, eventEmitter) {
  21. Strophe.addConnectionPlugin('ping', {
  22. connection: null,
  23. failedPings: 0,
  24. /**
  25. * Initializes the plugin. Method called by Strophe.
  26. * @param connection Strophe connection instance.
  27. */
  28. init: function (connection) {
  29. this.connection = connection;
  30. Strophe.addNamespace('PING', "urn:xmpp:ping");
  31. },
  32. /**
  33. * Sends "ping" to given <tt>jid</tt>
  34. * @param jid the JID to which ping request will be sent.
  35. * @param success callback called on success.
  36. * @param error callback called on error.
  37. * @param timeout ms how long are we going to wait for the response. On
  38. * timeout <tt>error<//t> callback is called with undefined error
  39. * argument.
  40. */
  41. ping: function (jid, success, error, timeout) {
  42. var iq = $iq({type: 'get', to: jid});
  43. iq.c('ping', {xmlns: Strophe.NS.PING});
  44. this.connection.sendIQ(iq, success, error, timeout);
  45. },
  46. /**
  47. * Checks if given <tt>jid</tt> has XEP-0199 ping support.
  48. * @param jid the JID to be checked for ping support.
  49. * @param callback function with boolean argument which will be
  50. * <tt>true</tt> if XEP-0199 ping is supported by given <tt>jid</tt>
  51. */
  52. hasPingSupport: function (jid, callback) {
  53. this.connection.disco.info(
  54. jid, null,
  55. function (result) {
  56. var ping = $(result).find('>>feature[var="urn:xmpp:ping"]');
  57. callback(ping.length > 0);
  58. },
  59. function (error) {
  60. console.error("Ping feature discovery error", error);
  61. callback(false);
  62. }
  63. );
  64. },
  65. /**
  66. * Starts to send ping in given interval to specified remote JID.
  67. * This plugin supports only one such task and <tt>stopInterval</tt>
  68. * must be called before starting a new one.
  69. * @param remoteJid remote JID to which ping requests will be sent to.
  70. * @param interval task interval in ms.
  71. */
  72. startInterval: function (remoteJid, interval) {
  73. if (this.intervalId) {
  74. console.error("Ping task scheduled already");
  75. return;
  76. }
  77. if (!interval)
  78. interval = PING_INTERVAL;
  79. var self = this;
  80. this.intervalId = window.setInterval(function () {
  81. self.ping(remoteJid,
  82. function (result) {
  83. // Ping OK
  84. self.failedPings = 0;
  85. },
  86. function (error) {
  87. self.failedPings += 1;
  88. console.error(
  89. "Ping " + (error ? "error" : "timeout"), error);
  90. if (self.failedPings >= PING_THRESHOLD) {
  91. self.connection.disconnect();
  92. }
  93. }, PING_TIMEOUT);
  94. }, interval);
  95. console.info("XMPP pings will be sent every " + interval + " ms");
  96. },
  97. /**
  98. * Stops current "ping" interval task.
  99. */
  100. stopInterval: function () {
  101. if (this.intervalId) {
  102. window.clearInterval(this.intervalId);
  103. this.intervalId = null;
  104. this.failedPings = 0;
  105. console.info("Ping interval cleared");
  106. }
  107. }
  108. });
  109. };