前端使用fetch接收流式數(shù)據(jù)處理實(shí)例demo
流式數(shù)據(jù)
流式數(shù)據(jù)是指以連續(xù)的方式產(chǎn)生和傳輸?shù)臄?shù)據(jù),這類數(shù)據(jù)通常在實(shí)時(shí)或接近實(shí)時(shí)的情況下進(jìn)行處理
代碼部分
這是我測(cè)試用的demo,需要后端配合起來一個(gè)服務(wù),進(jìn)行接口調(diào)試,下面我把整個(gè)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() // 讀取下一個(gè)數(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ù)讀取下一個(gè)數(shù)據(jù)塊
}
read() // 開始讀取流數(shù)據(jù)
} catch (error) {
console.error("Error fetching AI text:", error)
}
},
demo,壓縮包整不了,看代碼吧
//我這里做了個(gè)跨域,vite.config.js
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(),
],
server: {
host: "0.0.0.0",
proxy: {
'/stream3': {
target: 'xxxxxxxxxxx', // 目標(biāo)服務(wù)器地址
changeOrigin: true, // 允許跨域
rewrite: (path) => path.replace(/^\/api/, '') // 去掉請(qǐng)求路徑中的/api前綴
},
}
},
})
//封裝導(dǎo)出請(qǐng)求方法
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()">點(diǎn)擊</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() // 讀取下一個(gè)數(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ù)讀取下一個(gè)數(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>
總結(jié)
到此這篇關(guān)于前端使用fetch接收流式數(shù)據(jù)處理的文章就介紹到這了,更多相關(guān)前端fetch接收流式數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
學(xué)習(xí)JSON.stringify的9大特性和轉(zhuǎn)換規(guī)則
本文介紹JSON.stringify9大特性和轉(zhuǎn)換規(guī)則,JSON.stringify()方法將一個(gè)JavaScript對(duì)象或值轉(zhuǎn)換為?JSON?字符串,如果指定了一個(gè)replacer?函數(shù),則可以選擇性地替換值,或者指定的replacer是數(shù)組,則可選擇性地僅包含數(shù)組指定的屬性,更多內(nèi)容需要的小火煸可以參考下面溫行內(nèi)容2022-02-02
JavaScript實(shí)現(xiàn)Tab標(biāo)簽頁切換的最簡(jiǎn)便方式(4種)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)Tab標(biāo)簽頁切換的最簡(jiǎn)便方式(4種),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
layer插件實(shí)現(xiàn)在彈出層中彈出一警告提示并關(guān)閉彈出層的方法
今天小編就為大家分享一篇layer插件實(shí)現(xiàn)在彈出層中彈出一警告提示并關(guān)閉彈出層的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
js鼠標(biāo)按鍵事件和鍵盤按鍵事件用法實(shí)例匯總
這篇文章主要介紹了js鼠標(biāo)按鍵事件和鍵盤按鍵事件用法,結(jié)合實(shí)例形式總結(jié)分析了JavaScript針對(duì)鼠標(biāo)與鍵盤事件的常用操作技巧,需要的朋友可以參考下2016-10-10
JS實(shí)現(xiàn)CheckBox復(fù)選框全選全不選功能
在網(wǎng)站的管理后臺(tái)應(yīng)用此功能居多,如一次性處理多個(gè)產(chǎn)品,或?qū)ξ恼碌膭h除對(duì)產(chǎn)品的下架等處理,一條一條的點(diǎn)顯然有一些麻煩,如果能每一行放一個(gè)checkbox,然后統(tǒng)一處理就好辦的多了,今天我就用簡(jiǎn)單的篇幅來講解一下這個(gè)功能的實(shí)現(xiàn)原理和實(shí)現(xiàn)過程。2015-05-05
javascript實(shí)現(xiàn)點(diǎn)擊小圖顯示大圖
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)點(diǎn)擊小圖顯示大圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11

