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/components/chat/Chat.vue | 7 ++++++- src/utils/sse/SSEClient.ts | 19 +++---------------- src/components/chat/hooks/useSyncMsg.ts | 14 +++++++------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/components/chat/Chat.vue b/src/components/chat/Chat.vue index 5d55abb..43f5f77 100644 --- a/src/components/chat/Chat.vue +++ b/src/components/chat/Chat.vue @@ -75,6 +75,7 @@ import { useCompRef } from '/@/utils/types'; import { toMyFixed } from '/@/utils/util'; import { useLoadData } from './hooks/useLoadData'; +import { useSyncMsg } from './hooks/useSyncMsg'; const containerRef = useCompRef(ChatContainer); const chatListDom = computed(() => containerRef.value?.chatListDom); @@ -417,13 +418,17 @@ sendChatMessage(messageContent.value); }; + const { loadRangeData, onChatListScroll, moreIsLoading, updateLoadIndex } = useScrollLoad({ container: chatListDom, historyGroupId: currentRouteId, messageList, loadReplyData, }); - +useSyncMsg({ + msgList: messageList, + updateLoadIndex, +}); const chatListLoading = ref(true); onActivated(() => { diff --git a/src/components/chat/hooks/useSyncMsg.ts b/src/components/chat/hooks/useSyncMsg.ts index 76610b1..1403909 100644 --- a/src/components/chat/hooks/useSyncMsg.ts +++ b/src/components/chat/hooks/useSyncMsg.ts @@ -1,9 +1,10 @@ -import { onMounted, onUnmounted, type Ref } from 'vue'; +import { reverse } from 'lodash-es'; +import { type Ref } from 'vue'; import type { ChatMessage } from '../model/types'; -import { RoleEnum } from '../model/types'; import { MAIN_URL } from '/@/constants'; import { SSEClient } from '/@/utils/sse/SSEClient'; -import { reverse } from 'lodash-es'; +import { Local } from '/@/utils/storage'; +import { accessSessionKey } from '/@/utils/request'; type UseSyncMsgOptions = { updateLoadIndex: (addCount: number) => void; @@ -19,7 +20,6 @@ {}, { onMessage: (data) => { - console.log("馃殌 ~ data:\n", data) return; const recentIds = reverse([ { id: 'a1b2c3d4', time: '2024-03-27 15:42:33' }, @@ -47,8 +47,6 @@ // {id: 'k7l8m9n0', time: '2024-10-25 08:19:07'}, ]); - - // 鑾峰彇鏈悓姝ョ殑娑堟伅 const unsyncedMessages = findUnsyncedMessages(recentIds, userHistoryIds); console.log('鏈悓姝ョ殑娑堟伅:', unsyncedMessages); @@ -67,7 +65,9 @@ }, } ); - sseClient.connect({}); + sseClient.connect({ + websessionid: Local.get(accessSessionKey), + }); // onMounted(() => { // sseClient.connect({}); diff --git a/src/utils/sse/SSEClient.ts b/src/utils/sse/SSEClient.ts index 2812458..e277c30 100644 --- a/src/utils/sse/SSEClient.ts +++ b/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); + /** * 鏂紑杩炴帴 -- Gitblit v1.9.3