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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* global $, $iq, Strophe */
  2. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  3. var PING_INTERVAL = 15000;
  4. var PING_TIMEOUT = 10000;
  5. /**
  6. * XEP-0199 ping plugin.
  7. *
  8. * Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
  9. */
  10. module.exports = function () {
  11. Strophe.addConnectionPlugin('ping', {
  12. connection: null,
  13. /**
  14. * Initializes the plugin. Method called by Strophe.
  15. * @param connection Strophe connection instance.
  16. */
  17. init: function (connection) {
  18. this.connection = connection;
  19. Strophe.addNamespace('PING', "urn:xmpp:ping");
  20. },
  21. /**
  22. * Sends "ping" to given <tt>jid</tt>
  23. * @param jid the JID to which ping request will be sent.
  24. * @param success callback called on success.
  25. * @param error callback called on error.
  26. * @param timeout ms how long are we going to wait for the response. On
  27. * timeout <tt>error<//t> callback is called with undefined error
  28. * argument.
  29. */
  30. ping: function (jid, success, error, timeout) {
  31. var iq = $iq({type: 'get', to: jid});
  32. iq.c('ping', {xmlns: Strophe.NS.PING});
  33. this.connection.sendIQ(iq, success, error, timeout);
  34. },
  35. /**
  36. * Starts to send ping in given interval to specified remote JID.
  37. * This plugin supports only one such task and <tt>stopInterval</tt>
  38. * must be called before starting a new one.
  39. * @param remoteJid remote JID to which ping requests will be sent to.
  40. * @param interval task interval in ms.
  41. */
  42. startInterval: function (remoteJid, interval) {
  43. if (this.intervalId) {
  44. console.error("Ping task scheduled already");
  45. return;
  46. }
  47. if (!interval)
  48. interval = PING_INTERVAL;
  49. var self = this;
  50. this.intervalId = window.setInterval(function () {
  51. self.ping(remoteJid,
  52. function (result) {
  53. // Ping OK
  54. },
  55. function (error) {
  56. console.error(
  57. "Ping " + (error ? "error" : "timeout"), error);
  58. //FIXME: react
  59. }, PING_TIMEOUT);
  60. }, interval);
  61. console.info("XMPP pings will be sent every " + interval + " ms");
  62. },
  63. /**
  64. * Stops current "ping" interval task.
  65. */
  66. stopInterval: function () {
  67. if (this.intervalId) {
  68. window.clearInterval(this.intervalId);
  69. this.intervalId = null;
  70. console.info("Ping interval cleared");
  71. }
  72. }
  73. });
  74. };