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

StropheLastSuccess.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 {XmppConnection} xmppConnection - The XMPP connection which manages the given {@code stropheConnection}.
  15. * @param {Object} stropheConnection - Strophe connection instance.
  16. */
  17. startTracking(xmppConnection, stropheConnection) {
  18. const originalRawInput = stropheConnection.rawInput;
  19. stropheConnection.rawInput = (...args) => {
  20. // It's okay to use rawInput callback only once the connection has been established, otherwise it will
  21. // treat 'item-not-found' or other connection error on websocket reconnect as successful stanza received.
  22. if (xmppConnection.connected) {
  23. this._lastSuccess = Date.now();
  24. }
  25. originalRawInput.apply(stropheConnection, args);
  26. };
  27. }
  28. /**
  29. * Returns how many milliseconds have passed since the last successful BOSH request.
  30. *
  31. * @returns {number|null}
  32. */
  33. getTimeSinceLastSuccess() {
  34. return this._lastSuccess
  35. ? Date.now() - this._lastSuccess
  36. : null;
  37. }
  38. }