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 4.0KB

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