Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

strophe.ping.js 5.0KB

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