Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

strophe.ping.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* global $, $iq, Strophe */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  4. /**
  5. * Ping every 10 sec
  6. */
  7. var PING_INTERVAL = 10000;
  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. var disco = this.connection.disco;
  55. // XXX The following disco.info was observed to throw a "TypeError:
  56. // Cannot read property 'info' of undefined" during porting to React
  57. // Native. Since disco is checked in multiple places (e.g.
  58. // strophe.jingle.js, strophe.rayo.js), check it here as well.
  59. if (disco) {
  60. disco.info(
  61. jid,
  62. null,
  63. function (result) {
  64. var ping
  65. = $(result).find('>>feature[var="urn:xmpp:ping"]');
  66. callback(ping.length > 0);
  67. },
  68. function (error) {
  69. logger.error("Ping feature discovery error", error);
  70. callback(false);
  71. }
  72. );
  73. } else {
  74. // FIXME Should callback be invoked here? Maybe with false as an
  75. // argument?
  76. }
  77. },
  78. /**
  79. * Starts to send ping in given interval to specified remote JID.
  80. * This plugin supports only one such task and <tt>stopInterval</tt>
  81. * must be called before starting a new one.
  82. * @param remoteJid remote JID to which ping requests will be sent to.
  83. * @param interval task interval in ms.
  84. */
  85. startInterval: function (remoteJid, interval) {
  86. if (this.intervalId) {
  87. logger.error("Ping task scheduled already");
  88. return;
  89. }
  90. if (!interval)
  91. interval = PING_INTERVAL;
  92. var self = this;
  93. this.intervalId = window.setInterval(function () {
  94. self.ping(remoteJid,
  95. function (result) {
  96. // Ping OK
  97. self.failedPings = 0;
  98. },
  99. function (error) {
  100. self.failedPings += 1;
  101. logger.error(
  102. "Ping " + (error ? "error" : "timeout"), error);
  103. if (self.failedPings >= PING_THRESHOLD) {
  104. self.connection.disconnect();
  105. }
  106. }, PING_TIMEOUT);
  107. }, interval);
  108. logger.info("XMPP pings will be sent every " + interval + " ms");
  109. },
  110. /**
  111. * Stops current "ping" interval task.
  112. */
  113. stopInterval: function () {
  114. if (this.intervalId) {
  115. window.clearInterval(this.intervalId);
  116. this.intervalId = null;
  117. this.failedPings = 0;
  118. logger.info("Ping interval cleared");
  119. }
  120. }
  121. });
  122. };