consumer.ts 812 B

123456789101112131415161718192021222324252627282930
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import * as ActionCable from '@rails/actioncable'
  3. import log from '#shared/utils/log.ts'
  4. ActionCable.adapters.logger = log as unknown as Console
  5. ActionCable.logger.enabled = true
  6. export const consumer = ActionCable.createConsumer()
  7. export const reopenWebSocketConnection = () => {
  8. consumer.connection.reopen()
  9. return new Promise<void>((resolve, reject) => {
  10. const startTime = Date.now()
  11. const checkConnection = () => {
  12. if (consumer.connection.isOpen()) {
  13. resolve()
  14. }
  15. // to avoid infinite loop
  16. else if (Date.now() - startTime > 10_000) {
  17. reject(new Error('failed to reconnect'))
  18. } else {
  19. setTimeout(checkConnection, 100)
  20. }
  21. }
  22. checkConnection()
  23. })
  24. }