前端使用fetch接收流式數(shù)據(jù)處理實例demo
更新時間:2025年02月27日 08:27:17 作者:xiaoliyo_
這篇文章主要介紹了前端使用fetch接收流式數(shù)據(jù)處理的相關資料,文中描述了一個流式數(shù)據(jù)處理的demo,需要后端服務配合進行接口調試,需要的朋友可以參考下
流式數(shù)據(jù)
流式數(shù)據(jù)是指以連續(xù)的方式產生和傳輸?shù)臄?shù)據(jù),這類數(shù)據(jù)通常在實時或接近實時的情況下進行處理
代碼部分
這是我測試用的demo,需要后端配合起來一個服務,進行接口調試,下面我把整個demo打包出來
async getText() { const decoder = new TextDecoder("utf-8") try { const test = "1" const stream = await getAiText(test) // 獲取流 const reader = stream.getReader() // 獲取流的讀取器 // 讀取流數(shù)據(jù)的函數(shù) const read = async () => { const { done, value } = await reader.read() // 讀取下一個數(shù)據(jù)塊 if (done) { console.log("Stream complete") return } const chunk = decoder.decode(value, { stream: true }) console.log(chunk)//查看數(shù)據(jù)輸出 setTimeout(() => { this.text += chunk // 更新 text }, 200) read() // 繼續(xù)讀取下一個數(shù)據(jù)塊 } read() // 開始讀取流數(shù)據(jù) } catch (error) { console.error("Error fetching AI text:", error) } },
demo,壓縮包整不了,看代碼吧
//我這里做了個跨域,vite.config.js // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue(), ], server: { host: "0.0.0.0", proxy: { '/stream3': { target: 'xxxxxxxxxxx', // 目標服務器地址 changeOrigin: true, // 允許跨域 rewrite: (path) => path.replace(/^\/api/, '') // 去掉請求路徑中的/api前綴 }, } }, })
//封裝導出請求方法 export const getAiText = async (text) => { const response = await fetch('/stream3', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ test: text }), }) return response.body // 返回 ReadableStream }
//組件頁面引入 <template> <div class='HelloWorld_wrap'> <h1 @click="getText()">點擊</h1> {{text}} </div> </template> <script > import { getAiText } from "../http/testAi" import { defineComponent, ref, onMounted, onUpdated, toRefs, reactive, } from "vue" export default defineComponent({ name: "HelloWorld", props: {}, data() { return { text: "", } }, methods: { async getText() { const decoder = new TextDecoder("utf-8") try { const test = "1" const stream = await getAiText(test) // 獲取流 const reader = stream.getReader() // 獲取流的讀取器 // 讀取流數(shù)據(jù)的函數(shù) const read = async () => { const { done, value } = await reader.read() // 讀取下一個數(shù)據(jù)塊 if (done) { console.log("Stream complete") return } const chunk = decoder.decode(value, { stream: true }) console.log(chunk)//查看數(shù)據(jù)輸出 setTimeout(() => { this.text += chunk // 更新 text }, 200) read() // 繼續(xù)讀取下一個數(shù)據(jù)塊 } read() // 開始讀取流數(shù)據(jù) } catch (error) { console.error("Error fetching AI text:", error) } }, }, mounted() { }, }); </script> <style lang="scss" scoped> </style>
//最后app.vue引入 <script setup> import HelloWorld from './components/HelloWorld.vue' </script> <template> <HelloWorld /> </template>
總結
到此這篇關于前端使用fetch接收流式數(shù)據(jù)處理的文章就介紹到這了,更多相關前端fetch接收流式數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
學習JSON.stringify的9大特性和轉換規(guī)則
本文介紹JSON.stringify9大特性和轉換規(guī)則,JSON.stringify()方法將一個JavaScript對象或值轉換為?JSON?字符串,如果指定了一個replacer?函數(shù),則可以選擇性地替換值,或者指定的replacer是數(shù)組,則可選擇性地僅包含數(shù)組指定的屬性,更多內容需要的小火煸可以參考下面溫行內容2022-02-02JavaScript實現(xiàn)Tab標簽頁切換的最簡便方式(4種)
這篇文章主要介紹了JavaScript實現(xiàn)Tab標簽頁切換的最簡便方式(4種),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06layer插件實現(xiàn)在彈出層中彈出一警告提示并關閉彈出層的方法
今天小編就為大家分享一篇layer插件實現(xiàn)在彈出層中彈出一警告提示并關閉彈出層的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09