wujingjing
2025-01-14 12a89593d13fa38810c7af54c7ea8cb72ae65a10
src/utils/sse/SSEClient.ts
@@ -1,5 +1,4 @@
import type { AxiosRequestConfig } from 'axios';
import { EventSourcePolyfill } from 'event-source-polyfill';
import { accessSessionKey } from '../request';
import { SESSION_KEY } from '../request';
import { Local } from '../storage';
@@ -39,7 +38,7 @@
      this.options = {
         retryDelay: 1000,
         autoReconnect: true,
         timeout: 24 * 60 * 60 * 1000,
         timeout: 3000,
         headers: {
            [SESSION_KEY]: Local.get(accessSessionKey),
         },
@@ -58,19 +57,13 @@
         // 创建 AbortController 用于超时控制
         // this.abortController = new AbortController();
         // 创建 EventSource 并添加 headers
         this.eventSource = new EventSourcePolyfill(fullUrl, {
            headers: this.options.headers,
            heartbeatTimeout: this.options.timeout,
         });
         this.eventSource = new EventSource(fullUrl);
         // 绑定事件处理
         this.bindEvents();
      } catch (error) {
         console.log('catch error');
         await this.debounceHandleError.call(this, error);
      }
   }
@@ -101,9 +94,6 @@
      };
      this.eventSource.onerror = async (error) => {
         console.log('on error');
         await this.debounceHandleError.call(this, error);
      };
   }
@@ -111,7 +101,6 @@
    * 错误处理
    */
   private async handleError(error: any): Promise<void> {
      console.log('🚀 ~ error:', error);
      this.callbacks.onError?.(error);
      if (this.options.autoReconnect) {
         this.callbacks.onRetry?.();
@@ -125,9 +114,7 @@
      }
   }
   private debounceHandleError = debounce((error: any) => {
      this.handleError(error);
   }, 500);
   /**
    * 断开连接