From 6f1435cd635f532d5cb8b87f38322e05552cf799 Mon Sep 17 00:00:00 2001 From: weipengfei <2187978347@qq.com> Date: Fri, 13 Oct 2023 14:07:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/typings/chat.d.ts | 1 - src/views/chat/index.vue | 34 +++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/typings/chat.d.ts b/src/typings/chat.d.ts index 18d8973..0c8012e 100644 --- a/src/typings/chat.d.ts +++ b/src/typings/chat.d.ts @@ -3,7 +3,6 @@ declare namespace Chat { interface Chat { dateTime: string text: string - mp3: Array | null inversion?: boolean error?: boolean loading?: boolean diff --git a/src/views/chat/index.vue b/src/views/chat/index.vue index 9b53a69..d501d60 100644 --- a/src/views/chat/index.vue +++ b/src/views/chat/index.vue @@ -57,12 +57,12 @@ user_channel.on('message', (data: any) => { if (recordFalg.value == 0) { RecordXunfei() recordFalg.value = 1 - nowStauts.value = 'record' + nowStatus.value = 'record' } else { RecordXunfei() recordFalg.value = 0 - nowStauts.value = 'loding' + nowStatus.value = 'loding' handleSubmit() } }) @@ -71,12 +71,12 @@ const changeRecord = ()=>{ if (recordFalg.value == 0) { RecordXunfei() recordFalg.value = 1 - nowStauts.value = 'record' + nowStatus.value = 'record' } else { RecordXunfei() recordFalg.value = 0 - nowStauts.value = 'loding' + nowStatus.value = 'loding' } } @@ -108,7 +108,7 @@ const conversationList = computed(() => const prompt = ref('') const loading = ref(false) const inputRef = ref(null) -const nowStauts = ref('input') // 当前系统状态, input等待, loding加载, record录音 +const nowStatus = ref('input') // 当前系统状态, input等待, loding加载, record录音 // 添加PromptStore const promptStore = usePromptStore() @@ -147,11 +147,11 @@ async function onConversation() { // 监听WebSocket关闭事件 socket.onclose = (event: any) => { console.log('连接已关闭: ', event) - nowStauts.value = 'input' + nowStatus.value = 'input' } socket.onerror = (event: any) => { console.log('连接出错: ', event) - nowStauts.value = 'input' + nowStatus.value = 'input' } if (loading.value) @@ -196,7 +196,6 @@ async function onConversation() { try { let lastText = '' - const mp3: any = [] console.log('发送消息', message) const fetchChatAPIOnce = async () => { @@ -209,15 +208,15 @@ async function onConversation() { }, ], })) - nowStauts.value = 'loding' + nowStatus.value = 'loding' // 监听WebSocket接收消息事件 socket.onmessage = (event: any) => { - const msg = JSON.parse(event.data) + try { + const msg = JSON.parse(event.data) // console.log(`收到消息: `, msg.payload.choices.text[0].content); // console.log(`当前消息: `, dataSources.value[dataSources.value.length - 1].text); lastText += msg.payload.choices.text[0].content - mp3.push(msg.payload.choices.mp3) const loading = true updateChat( +uuid, @@ -225,7 +224,6 @@ async function onConversation() { { dateTime: new Date().toLocaleString(), text: lastText, - mp3, inversion: false, error: false, loading, @@ -233,6 +231,13 @@ async function onConversation() { requestOptions: { prompt: message, options: { ...options } }, }, ) + // 关闭连接 + if(msg.header.code!=0||(event.data&&msg.header.status==2)){ + socket.close() + } + } catch (error) { + socket.close() + } } // await fetchChatAPIProcess({ @@ -624,8 +629,8 @@ const click = () => {