Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

strophe.ping.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * Starts to send ping in given interval to specified remote JID.
  48. * This plugin supports only one such task and <tt>stopInterval</tt>
  49. * must be called before starting a new one.
  50. * @param remoteJid remote JID to which ping requests will be sent to.
  51. * @param interval task interval in ms.
  52. */
  53. startInterval: function (remoteJid, interval) {
  54. if (this.intervalId) {
  55. console.error("Ping task scheduled already");
  56. return;
  57. }
  58. if (!interval)
  59. interval = PING_INTERVAL;
  60. var self = this;
  61. this.intervalId = window.setInterval(function () {
  62. self.ping(remoteJid,
  63. function (result) {
  64. // Ping OK
  65. self.failedPings = 0;
  66. },
  67. function (error) {
  68. self.failedPings += 1;
  69. console.error(
  70. "Ping " + (error ? "error" : "timeout"), error);
  71. if (self.failedPings >= PING_THRESHOLD) {
  72. self.connection.disconnect();
  73. }
  74. }, PING_TIMEOUT);
  75. }, interval);
  76. console.info("XMPP pings will be sent every " + interval + " ms");
  77. },
  78. /**
  79. * Stops current "ping" interval task.
  80. */
  81. stopInterval: function () {
  82. if (this.intervalId) {
  83. window.clearInterval(this.intervalId);
  84. this.intervalId = null;
  85. this.failedPings = 0;
  86. console.info("Ping interval cleared");
  87. }
  88. }
  89. });
  90. };