您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StropheLastSuccess.js 991B

123456789101112131415161718192021222324252627282930313233343536
  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. }
  11. /**
  12. * Starts tracking requests on the given connection.
  13. *
  14. * @param {Object} stropheConnection - Strophe connection instance.
  15. */
  16. startTracking(stropheConnection) {
  17. const originalRawInput = stropheConnection.rawInput;
  18. stropheConnection.rawInput = function(...args) {
  19. this._lastSuccess = Date.now();
  20. originalRawInput.apply(stropheConnection, args);
  21. };
  22. }
  23. /**
  24. * Returns how many milliseconds have passed since the last successful BOSH request.
  25. *
  26. * @returns {number|null}
  27. */
  28. getTimeSinceLastSuccess() {
  29. return this._lastSuccess
  30. ? Date.now() - this._lastSuccess
  31. : null;
  32. }
  33. }