sse.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import type { ReportStatusType } from '@/api/self_check'
  2. import type { FrontendTask } from '../types'
  3. import { ReportStatus } from '@/api/self_check'
  4. import { useSSE } from '@/composables/useSSE'
  5. /**
  6. * SSE Task
  7. *
  8. * Checks if the application is able to connect to the backend through the Server-Sent Events protocol
  9. */
  10. const SSETask: FrontendTask = {
  11. key: 'sse',
  12. name: () => 'SSE',
  13. description: () => $gettext('Support communication with the backend through the Server-Sent Events protocol. '
  14. + 'If your Nginx UI is being used via an Nginx reverse proxy, '
  15. + 'please refer to this link to write the corresponding configuration file: '
  16. + 'https://nginxui.com/guide/nginx-proxy-example.html'),
  17. check: async (): Promise<ReportStatusType> => {
  18. try {
  19. const connected = await new Promise<boolean>(resolve => {
  20. const { connect, disconnect } = useSSE()
  21. // Use the connect method from useSSE
  22. connect({
  23. url: '/api/self_check/sse',
  24. onMessage: () => {
  25. resolve(true)
  26. },
  27. onError: () => {
  28. resolve(false)
  29. disconnect()
  30. },
  31. reconnectInterval: 0,
  32. })
  33. // Set a timeout for the connection attempt
  34. setTimeout(() => {
  35. resolve(false)
  36. disconnect()
  37. }, 5000)
  38. })
  39. if (connected) {
  40. return ReportStatus.Success
  41. }
  42. else {
  43. return ReportStatus.Error
  44. }
  45. }
  46. catch (error) {
  47. console.error(error)
  48. return ReportStatus.Error
  49. }
  50. },
  51. }
  52. export default SSETask