From 12a89593d13fa38810c7af54c7ea8cb72ae65a10 Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期二, 14 一月 2025 14:56:45 +0800 Subject: [PATCH] 使用 url 参数传 session --- src/utils/sse/SSEClient.ts | 71 ++++++++++++----------------------- 1 files changed, 25 insertions(+), 46 deletions(-) diff --git a/src/utils/sse/SSEClient.ts b/src/utils/sse/SSEClient.ts index 45a3f62..e277c30 100644 --- a/src/utils/sse/SSEClient.ts +++ b/src/utils/sse/SSEClient.ts @@ -1,16 +1,18 @@ import type { AxiosRequestConfig } from 'axios'; - +import { accessSessionKey } from '../request'; +import { SESSION_KEY } from '../request'; +import { Local } from '../storage'; +import { debounce } from 'lodash-es'; export interface SSEOptions { - /** 閲嶈瘯娆℃暟 */ - retries?: number; /** 閲嶈瘯寤惰繜(ms) */ retryDelay?: number; /** 瓒呮椂鏃堕棿(ms) */ timeout?: number; - /** 璇锋眰閰嶇疆 */ - // requestConfig?: AxiosRequestConfig; /** 鏄惁鑷姩閲嶈繛 */ autoReconnect?: boolean; + + /** 璇锋眰澶� */ + headers?: Record<string, string>; } export interface SSEEventCallbacks { @@ -23,23 +25,23 @@ /** 杩炴帴鍏抽棴鍥炶皟 */ onClose?: () => void; /** 閲嶈瘯鍥炶皟 */ - onRetry?: (retryCount: number) => void; + onRetry?: () => void; } export class SSEClient { private eventSource: EventSource | null = null; - private retryCount = 0; - private isConnecting = false; private reconnectTimeout: number | null = null; private abortController: AbortController | null = null; constructor(private url: string, private options: SSEOptions = {}, private callbacks: SSEEventCallbacks = {}) { // 璁剧疆榛樿鍊� this.options = { - retries: 3, retryDelay: 1000, - timeout: Infinity, - autoReconnect: false, + autoReconnect: true, + timeout: 3000, + headers: { + [SESSION_KEY]: Local.get(accessSessionKey), + }, ...options, }; } @@ -48,36 +50,20 @@ * 寤虹珛杩炴帴 */ async connect(params?: Record<string, any>): Promise<void> { - if (this.isConnecting) return; - this.isConnecting = true; - try { // 鏋勫缓 URL 鍜屽弬鏁� const queryString = params && Object.values(params).length ? `?${new URLSearchParams(params).toString()}` : ''; const fullUrl = `${this.url}${queryString}`; // 鍒涘缓 AbortController 鐢ㄤ簬瓒呮椂鎺у埗 - this.abortController = new AbortController(); - - // 璁剧疆瓒呮椂 - const timeoutId = setTimeout(() => { - this.abortController?.abort(); - throw new Error('Connection timeout'); - }, this.options.timeout); - - // 鍒涘缓 EventSource + // this.abortController = new AbortController(); + // 鍒涘缓 EventSource 骞舵坊鍔� headers this.eventSource = new EventSource(fullUrl); - - // 娓呴櫎瓒呮椂 - clearTimeout(timeoutId); // 缁戝畾浜嬩欢澶勭悊 this.bindEvents(); - - this.isConnecting = false; } catch (error) { - this.isConnecting = false; - await this.handleError(error); + console.log('catch error'); } } @@ -88,6 +74,7 @@ if (!this.eventSource) return; this.eventSource.onopen = () => { + console.log('杩炴帴鎴愬姛'); this.callbacks.onOpen?.(); }; @@ -107,7 +94,6 @@ }; this.eventSource.onerror = async (error) => { - await this.handleError(error); }; } @@ -116,23 +102,19 @@ */ private async handleError(error: any): Promise<void> { this.callbacks.onError?.(error); - - if (this.options.autoReconnect && this.retryCount < (this.options.retries || 0)) { - // debugger; - this.retryCount++; - this.callbacks.onRetry?.(this.retryCount); - + if (this.options.autoReconnect) { + this.callbacks.onRetry?.(); // 寤惰繜閲嶈瘯 await new Promise((resolve) => { this.reconnectTimeout = setTimeout(resolve, this.options.retryDelay); }); - - // 閲嶆柊杩炴帴 await this.connect(); } else { this.disconnect(); } } + + /** * 鏂紑杩炴帴 @@ -142,20 +124,17 @@ clearTimeout(this.reconnectTimeout); this.reconnectTimeout = null; } - if (this.eventSource) { this.eventSource.close(); this.eventSource = null; } - if (this.abortController) { - this.abortController.abort(); - this.abortController = null; - } + // if (this.abortController) { + // this.abortController.abort(); + // this.abortController = null; + // } - this.isConnecting = false; this.callbacks.onClose?.(); - this.retryCount = 0; } /** -- Gitblit v1.9.3