You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StropheLastSuccess.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Attaches to the {@link Strophe.Connection.rawInput} which is called whenever any data is received from the server.
  3. */
  4. export default class LastRequestTracker {
  5. /**
  6. * Initializes new instance.
  7. */
  8. constructor() {
  9. this._lastSuccess = null;
  10. this._lastFailedMessage = null;
  11. }
  12. /**
  13. * Starts tracking requests on the given connection.
  14. *
  15. * @param {XmppConnection} xmppConnection - The XMPP connection which manages the given {@code stropheConnection}.
  16. * @param {Object} stropheConnection - Strophe connection instance.
  17. */
  18. startTracking(xmppConnection, stropheConnection) {
  19. const originalRawInput = stropheConnection.rawInput;
  20. stropheConnection.rawInput = (...args) => {
  21. const rawMessage = args[0];
  22. if (rawMessage.includes('failure')) {
  23. this._lastFailedMessage = rawMessage;
  24. }
  25. // It's okay to use rawInput callback only once the connection has been established, otherwise it will
  26. // treat 'item-not-found' or other connection error on websocket reconnect as successful stanza received.
  27. if (xmppConnection.connected) {
  28. this._lastSuccess = Date.now();
  29. }
  30. originalRawInput.apply(stropheConnection, args);
  31. };
  32. }
  33. /**
  34. * Returns the last raw failed incoming message on the xmpp connection.
  35. *
  36. * @returns {string|null}
  37. */
  38. getLastFailedMessage() {
  39. return this._lastFailedMessage;
  40. }
  41. /**
  42. * Returns how many milliseconds have passed since the last successful BOSH request.
  43. *
  44. * @returns {number|null}
  45. */
  46. getTimeSinceLastSuccess() {
  47. return this._lastSuccess
  48. ? Date.now() - this._lastSuccess
  49. : null;
  50. }
  51. }