最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

前端使用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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

肃北| 昌图县| 恩平市| 杭锦旗| 石楼县| 哈尔滨市| 福建省| 镶黄旗| 正安县| 法库县| 龙陵县| 射阳县| 保靖县| 三门县| 遂溪县| 防城港市| 英超| 武平县| 东兰县| 东城区| 永泰县| 沂南县| 靖江市| 定远县| 许昌市| 庆城县| 永和县| 井陉县| 文化| 靖江市| 辽中县| 祁连县| 曲靖市| 新田县| 南木林县| 巴马| 平邑县| 女性| 临颍县| 迁安市| 彝良县|