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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* global $iq, Strophe */
  2. import { getLogger } from "jitsi-meet-logger";
  3. const logger = getLogger(__filename);
  4. import ConnectionPlugin from "./ConnectionPlugin";
  5. import GlobalOnErrorHandler from "../util/GlobalOnErrorHandler";
  6. /**
  7. * Ping every 10 sec
  8. */
  9. const PING_INTERVAL = 10000;
  10. /**
  11. * Ping timeout error after 15 sec of waiting.
  12. */
  13. const PING_TIMEOUT = 15000;
  14. /**
  15. * Will close the connection after 3 consecutive ping errors.
  16. */
  17. const PING_THRESHOLD = 3;
  18. /**
  19. * XEP-0199 ping plugin.
  20. *
  21. * Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
  22. */
  23. class PingConnectionPlugin extends ConnectionPlugin {
  24. /**
  25. * Contructs new object
  26. * @param {XMPP} xmpp the xmpp module.
  27. * @constructor
  28. */
  29. constructor(xmpp) {
  30. super();
  31. this.failedPings = 0;
  32. this.xmpp = xmpp;
  33. }
  34. /**
  35. * Initializes the plugin. Method called by Strophe.
  36. * @param connection Strophe connection instance.
  37. */
  38. init (connection) {
  39. super.init(connection);
  40. Strophe.addNamespace('PING', "urn:xmpp:ping");
  41. }
  42. /**
  43. * Sends "ping" to given <tt>jid</tt>
  44. * @param jid the JID to which ping request will be sent.
  45. * @param success callback called on success.
  46. * @param error callback called on error.
  47. * @param timeout ms how long are we going to wait for the response. On
  48. * timeout <tt>error<//t> callback is called with undefined error
  49. * argument.
  50. */
  51. ping (jid, success, error, timeout) {
  52. const iq = $iq({type: 'get', to: jid});
  53. iq.c('ping', {xmlns: Strophe.NS.PING});
  54. this.connection.sendIQ(iq, success, error, timeout);
  55. }
  56. /**
  57. * Checks if given <tt>jid</tt> has XEP-0199 ping support.
  58. * @param jid the JID to be checked for ping support.
  59. * @param callback function with boolean argument which will be
  60. * <tt>true</tt> if XEP-0199 ping is supported by given <tt>jid</tt>
  61. */
  62. hasPingSupport (jid, callback) {
  63. this.xmpp.caps.getFeatures(jid).then(features =>
  64. callback(features.has("urn:xmpp:ping")), error => {
  65. const errmsg = "Ping feature discovery error";
  66. GlobalOnErrorHandler.callErrorHandler(new Error(
  67. errmsg + ": " + error));
  68. logger.error(errmsg, error);
  69. callback(false);
  70. });
  71. }
  72. /**
  73. * Starts to send ping in given interval to specified remote JID.
  74. * This plugin supports only one such task and <tt>stopInterval</tt>
  75. * must be called before starting a new one.
  76. * @param remoteJid remote JID to which ping requests will be sent to.
  77. * @param interval task interval in ms.
  78. */
  79. startInterval (remoteJid, interval = PING_INTERVAL) {
  80. if (this.intervalId) {
  81. const errmsg = "Ping task scheduled already";
  82. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  83. logger.error(errmsg);
  84. return;
  85. }
  86. this.intervalId = window.setInterval(() => {
  87. this.ping(remoteJid, () => {
  88. this.failedPings = 0;
  89. }, (error) => {
  90. this.failedPings += 1;
  91. const errmsg = "Ping " + (error ? "error" : "timeout");
  92. if (this.failedPings >= PING_THRESHOLD) {
  93. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  94. logger.error(errmsg, error);
  95. // FIXME it doesn't help to disconnect when 3rd PING
  96. // times out, it only stops Strophe from retrying.
  97. // Not really sure what's the right thing to do in that
  98. // situation, but just closing the connection makes no
  99. // sense.
  100. //self.connection.disconnect();
  101. } else {
  102. logger.warn(errmsg, error);
  103. }
  104. }, PING_TIMEOUT);
  105. }, interval);
  106. logger.info("XMPP pings will be sent every " + interval + " ms");
  107. }
  108. /**
  109. * Stops current "ping" interval task.
  110. */
  111. stopInterval () {
  112. if (this.intervalId) {
  113. window.clearInterval(this.intervalId);
  114. this.intervalId = null;
  115. this.failedPings = 0;
  116. logger.info("Ping interval cleared");
  117. }
  118. }
  119. }
  120. export default function (xmpp) {
  121. Strophe.addConnectionPlugin('ping', new PingConnectionPlugin(xmpp));
  122. }