| | |
| | | this.options = { |
| | | retries: 3, |
| | | retryDelay: 1000, |
| | | timeout: 30000, |
| | | autoReconnect: true, |
| | | timeout: Infinity, |
| | | autoReconnect: false, |
| | | ...options, |
| | | }; |
| | | } |
| | |
| | | |
| | | try { |
| | | // 构建 URL 和参数 |
| | | const queryString = params ? `?${new URLSearchParams(params).toString()}` : ''; |
| | | const queryString = params && Object.values(params).length ? `?${new URLSearchParams(params).toString()}` : ''; |
| | | const fullUrl = `${this.url}${queryString}`; |
| | | |
| | | // 创建 AbortController 用于超时控制 |
| | |
| | | import { onMounted, onUnmounted } from 'vue'; |
| | | import { SSEClient } from './SSEClient'; |
| | | import { MAIN_URL } from '/@/constants'; |
| | | |
| | | // 创建实例 |
| | | const sseClient = new SSEClient( |
| | | '/events', |
| | | { |
| | | retries: 3, |
| | | retryDelay: 1000, |
| | | timeout: 30000, |
| | | autoReconnect: true, |
| | | `${MAIN_URL}events`, |
| | | |
| | | }, |
| | | {}, |
| | | { |
| | | onMessage: (data) => { |
| | | console.log('Received message:', data); |
| | |
| | | // 在 Vue 组件中使用 |
| | | export function useSSE() { |
| | | // 建立连接 |
| | | sseClient.connect({ |
| | | userId: 'xxx', |
| | | sessionId: 'xxx', |
| | | }); |
| | | sseClient.connect({}); |
| | | |
| | | |
| | | // onMounted(() => { |
| | | // // 建立连接 |