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.

StropheBoshLastSuccess.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Class attaches to Strophe BOSH connection and tracks the time of last successful request.
  3. * It does that by overriding {@code nextValidRid} method and tracking how the RID value changes.
  4. * A request was successful if the number has increased by 1 since the last time the method was called.
  5. */
  6. export default class LastRequestTracker {
  7. /**
  8. * Initializes new instance.
  9. */
  10. constructor() {
  11. this._nextValidRid = null;
  12. this._lastSuccess = null;
  13. }
  14. /**
  15. * Starts tracking requests on the given connection.
  16. *
  17. * @param {Object} stropheConnection - Strophe connection instance.
  18. */
  19. startTracking(stropheConnection) {
  20. stropheConnection.nextValidRid = rid => {
  21. // Just before connect and on disconnect RID will get assigned a new random value.
  22. // A request was successful only when the value got increased exactly by 1.
  23. if (this._nextValidRid === rid - 1) {
  24. this._lastSuccess = new Date().getTime();
  25. }
  26. this._nextValidRid = rid;
  27. };
  28. }
  29. /**
  30. * Returns how many milliseconds have passed since the last successful BOSH request.
  31. *
  32. * @returns {number|null}
  33. */
  34. getTimeSinceLastSuccess() {
  35. return this._lastSuccess
  36. ? new Date().getTime() - this._lastSuccess
  37. : null;
  38. }
  39. }