用fetch-event-source处理流式消息:Vue 3中实现openAI/DeepSeek的实时输出

microsoft/fetch-event-source 是一个由微软开发的开源库,旨在提供比浏览器原生 EventSource API 更强大的 Server-Sent Events (SSE) 请求处理能力

核心优势

  • ‌完全兼容 Fetch API‌:支持所有 HTTP 方法和自定义头部
  • 智能重试控制‌:开发者可以完全控制连接中断时的重试策略
  • 灵活的错误处理‌:提供完善的错误处理机制

一、安装依赖

javascript 复制代码
npm install @microsoft/fetch-event-source

二、基本使用

javascript 复制代码
import { fetchEventSource } from '@microsoft/fetch-event-source';

await fetchEventSource('/api/sse-endpoint', {
  method: 'GET', // 支持 GET/POST/PUT 等 HTTP 方法
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify({
    query: '你的查询内容'
  })
  onopen(response) {
    if (response.ok) {
      console.log('SSE连接成功');
    } else {
      throw new Error(`连接失败: ${response.status}`);
    }
  },
  onmessage(event) {
    console.log('收到消息:', event.data); //这里的消息就是流式输出的
  },
  onclose() {
    console.log('SSE连接关闭');
  },
  onerror(err) {
    console.error('发生错误:', err);
    throw err; // 抛出错误会停止重试
  }
});

三、封装成hooks

javascript 复制代码
import { fetchEventSource } from '@microsoft/fetch-event-source';
import { ref, onUnmounted } from 'vue';

interface EventSourceOptions {
  url: string;
  method?: 'GET' | 'POST';
  body?: any;
  onMessage?: (data: string) => void;
  onOpen?: (response: Response) => void;
  onError?: (err: any) => void;
}

export function useEventSource() {
  const error = ref<any>(null);
  const isLoading = ref<boolean>(false);
  let controller: AbortController | null = null;

  const fetchStream = async (options: EventSourceOptions) => {
    isLoading.value = true;
    error.value = null;
    controller = new AbortController();

    try {
      await fetchEventSource(options.url, {
        method: options.method || 'GET',
        headers: {
          'Content-Type': 'application/json',
          'Accept': "text/event-stream",
        },
        body: options.body ? JSON.stringify(options.body) : undefined,
        signal: controller.signal,
        // 建立链接
        onopen: async (response) => {
          if (response.ok && response.status == 200) {
            options.onOpen?.(response);
          }
        },
        // 接收消息
        onmessage: (event) => {
          if (event.data) {
            options.onMessage?.(JSON.parse(event.data));
          }
        },
        onerror: (err) => {
          options.onError?.(err);
          // 必须抛出错误才会停止重试
          throw err;
        },
        onclose: () => {
          isLoading.value = false;
        }
      });
    } catch (err) {
    	 console.log("链接失败");
      error.value = err;
      isLoading.value = false;
    }
  };

 // 主动中断链接
  const disconnect= () => {
    if (controller) {
      controller.abort();
      isLoading.value = false;
    }
  };

  onUnmounted(() => {
    disconnect();
  });

  return {
      isLoading,
      fetchStream,
      disconnect,
      onerror,
      onclose,
  };
}

四、使用hooks

javascript 复制代码
<template>
  <div>
    <button @click="sendMessage" :disabled="isLoading">
      {{ isLoading ? '发送中...' : '发送消息' }}
    </button>
    
    <div class="stream-content">
      {{ data }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useEventSource } from '@/composables/useEventSource';

const { isLoading, fetchStream } = useEventSource();

const data = ref('')

const sendMessage = async () => {
  await fetchStream({
    url: '/api/chat',
    method: 'POST',
    body: {
      content: '你好,请帮我回答这个问题'
    },
    onMessage: (content) => {
      console.log('收到数据:', content);
      data.value += content
    },
    onOpen: (response) => {
      console.log('连接已建立');
    },
    onError: (err) => {
      console.error('连接错误:', err);
    }
  });
};
</script>
相关推荐
暗冰ཏོ2 天前
《2026 Vue2 + Vue3 完整学习指南:基础语法、路由缓存、登录拦截、项目实战与面试题》
前端·vue.js·vue·vue3·vue2
曲幽3 天前
写页面时别再把 Element Plus 整个搬进来啦!Vue3按需加载的坑我帮你踩平了
vue3·web·vite·icon·element plus·vs code·import·unplugin
小云小白4 天前
若依-vue3 把深色版本改成天蓝色-含登录页
vue3·若依·天蓝色
曲幽5 天前
FastApiAdmin 后端接口开发好了,前端管理界面怎么调用与显示?
python·vue3·api·fastapi·web·ant design·view·menu·frontend
曲幽9 天前
我用了FastApiAdmin后,连夜把踩过的坑都整理出来了
redis·python·postgresql·vue3·fastapi·web·sqlalchemy·admin·fastapiadmin
Liu.7749 天前
vue3bug收录
vue3
小云小白18 天前
高性能 v-html 弹窗实现:Vue3 + Element Plus 最佳实践
vue3·弹窗·v-html
xun-ming19 天前
SpringBoot和Vue3实战阿里百炼大模型极简版
spring boot·ai·vue3·智能体·百炼大模型
哆啦A梦158820 天前
20, Springboot3+vue3实现前台轮播图和详情页的设计
javascript·数据库·spring boot·mybatis·vue3
小盼江20 天前
基于Springboot3+Vue3的协同过滤鲜花商城推荐系统
vue3·springboot3·鲜花商城