当前位置:首页 > 每日看点 > 正文内容

为什么招聘高级前端开发这么难?

卡卷网10个月前 (03-03)每日看点138

在现代前端开发中,使用Serve-Sent Events(SSE)实现流式数据传输((如聊天消息、实时日志、AI 生成文本的逐字输出等))越来越流行,本文来介绍一个前端如何实现接受数据,解析数据,展示到页面,有很好的用户体验。

一、什么是Serve-Sent Events(SSE)?

@microsoft/fetch-event-source 是一个由微软开发的 JavaScript 库,旨在提供更灵活、功能更强大的服务器发送事件(Server-Sent Events, SSE)。它结合了浏览器原生的 fetch API 和 EventSource 的特性,允许开发者通过 HTTP 流(HTTP Streaming)实现实时数据传输,同时支持更多自定义配置(如请求头、身份认证、错误重试等)。

  • 回调方法:
字段含义
method请求方法(POST、GET)
headers请求头Record<string, string>,通常需要指定'Content-Type': 'application/json','Accept': 'text/event-stream'
body请求的参数
onopen响应回来的回调
onmessage接收到消息的回调,每当服务器发一条消息,就触发接受一条消息
onclose响应结束的回调
onerror响应失败报错的回调
  • 对比原生 API 的优势
特性@microsoft/fetch-event-source原生 EventSource
HTTP 方法支持 GET/POST/PUT 等仅 GET
自定义请求头
请求体支持任意数据(如 JSON)不支持
错误重试可配置的重试逻辑有限的重试
流控制可手动暂停/恢复不支持
页面隐藏时行为可配置是否保持连接默认暂停

适用场景推荐

  • 需要与需要认证的 SSE 服务通信(如传递 Authorization 头)。
  • 使用 POST 请求传递参数并接收流式响应(如 OpenAI 的流式 API)。
  • 需要更健壮的连接管理和错误恢复机制。

二、如何使用@microsoft/fetch-event-source

首先,安装库 npm install @microsoft/fetch-event-source

安装成功,主要的基本用法

import { fetchEventSource } from '@microsoft/fetch-event-source'; async function startStream() { await fetchEventSource('request url', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_TOKEN', }, body: JSON.stringify({ query: "Hello" }), onopen(response) { // 连接成功时触发 if (response.ok) return; throw new Error('连接失败'); }, onmessage(event) { // 接收服务器发送的每条事件 console.log('收到数据:', event.data); // 请求完成 console.log('请求结束标记', data.done) }, onclose() { // 连接关闭时触发 console.log('连接终止'); }, onerror(err) { // 错误处理(默认会抛出异常并自动重试) console.error('错误:', err); throw err; // 抛出错误会触发重试机制 } }); }

三、在angular中实现,具体方案

1、安装库 npm install @microsoft/fetch-event-source

2、新建一个新的服务文件来处理 SSE 请求(chat.service.ts文件)

import { Injectable } from '@angular/core'; import { fetchEventSource } from '@microsoft/fetch-event-source'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ChatService { streamGptResponse(prompt: string): Observable<string> { return new Observable(observer => { let fullResponse = ''; const ctrl = new AbortController(); fetchEventSource('request url', { method: 'POST', headers: { 'Content-Type': 'application/json', // 告诉服务器,客户端数据格式 'Accept': 'text/event-stream' // 客户端声明:希望接收事件流格式的响应 }, body: JSON.stringify({ prompt }), signal: ctrl.signal, onmessage(event) { // 处理每个数据块 try { const data = JSON.parse(event.data); // data可以根据后端返回的具体格式,进行相应的处理 if (data.content) { fullResponse += data.content; observer.next(fullResponse); } // 标记请求完成 if (data.done) { observer.complete(); } } catch (error) { console.error('Parse message failed:', error); } }, onopen(response) { if (response.ok && response.headers.get('content-type')?.includes('text/event-stream')) { console.log('Connection opened'); } else { throw new Error(`Failed to open connection: ${response.status}`); } }, onclose() { console.log('Connection closed'); observer.complete(); }, onerror(error) { console.error('Connection error:', error); observer.error(error); ctrl.abort(); } }); // 返回清理函数 return () => { ctrl.abort(); }; }); } }

3、创建一个组件使用该服务

安装markdown,解析样式: npm i ngx-markdown

推荐使用14.0.1版本,markdown的使用下一篇文章会解释

import { Component } from '@angular/core'; import { SseService } from '../../services/sse.service'; @Component({ selector: 'app-gpt-stream', template: ` <div class="gpt-stream-container"> <div class="input-area"> <textarea [(ngModel)]="prompt" placeholder="请输入..." ></textarea> <button (click)="generateResponse()" [disabled]="isGenerating" > 发送 </button> </div> <div class="response-area"> <markdown *ngIf="response" [data]="response"></markdown> <div *ngIf="isGenerating" class="loading"> 思考中... </div> </div> </div> `, styles: [` .gpt-stream-container { padding: 20px; } .input-area { margin-bottom: 20px; } textarea { width: 100%; min-height: 100px; padding: 10px; margin-bottom: 10px; } .response-area { padding: 15px; border: 1px solid #eee; border-radius: 4px; min-height: 100px; } .loading { color: #666; font-style: italic; } `] }) export class GptStreamComponent { prompt = ''; response = ''; isGenerating = false; constructor(private sseService: SseService) {} generateResponse() { if (!this.prompt.trim() || this.isGenerating) { return; } this.isGenerating = true; this.response = ''; this.sseService.streamGptResponse(this.prompt) .subscribe({ next: (chunk) => { this.response = chunk; }, error: (error) => { console.error('Stream error:', error); this.isGenerating = false; }, complete: () => { this.isGenerating = false; } }); } }

3、在 module 文件中注册组件和服务:

import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { MarkdownModule } from 'ngx-markdown'; import { GptStreamComponent } from './components/gpt-stream/gpt-stream.component'; import { SseService } from './services/sse.service'; @NgModule({ declarations: [ GptStreamComponent ], imports: [ CommonModule, FormsModule, MarkdownModule.forRoot() ], exports: [ GptStreamComponent ], providers: [ SseService ] }) export class SharedModule { }

六、总结

主要实现特点:

1. 流式处理:

  • 使用 fetchEventSource 建立 SSE 连接
  • 通过 Observable 包装事件流
  • 实时更新 UI 显示生成的内容

2. 错误处理:

  • 包含完整的错误处理机制
  • 连接错误自动中断
  • 解析错误的容错处理

3. 资源清理:

  • 使用 AbortController 控制连接
  • Observable 完成时自动清理
  • 组件销毁时中断连接

4. 用户体验:

  • 显示加载状态
  • 防止重复提交
  • 实时显示生成内容

5. 类型安全:

  • 使用 TypeScript 类型
  • 接口定义清晰
  • 错误类型处理
原文链接:juejin.cn/post/74768813

扫描二维码推送至手机访问。

版权声明:本文由卡卷网发布,如需转载请注明出处。

本文链接:https://www.kajuan.net/ttnews/2025/03/11506.html

分享给朋友:

相关文章

报名的网课分期付款怎么退?

你在你分期付款的订单下面有客服电话,直接打电话描述一下你的问题,你可以告诉他你是被恶意绑定的,在不了解有退学条件这一说的情况下报的课程,可能遇到消费者诈骗了,不承认有退学金,说是霸王条款,诈骗消费者,你若分期了先把自动续费关了,别让自己个人...

阿里云服务器续费价格好贵,想换一家云服务厂商,该怎么选择?

阿里云服务器续费价格好贵,想换一家云服务厂商,该怎么选择?

最近一台买了3年时间的腾讯云轻量服务器到期了,还有5天时间。当时买的价格是3年198元。配置是2核CPU、4GB内存,80GB SSD云硬盘,1200GB 流量包,然后中途给免费升级了CPU,从2核变成了4核。平均下来一年的费用70元不到,...

什么样的网站能快速捕获你的心?

什么样的网站能快速捕获你的心?

大家好,我是程序员鱼皮。大家如果平时使用网站或产品时出现了问题,一般都会去寻找 “联系客服” 的位置,从而获得人工的帮助。我们团队的面试刷题产品 - 面试鸭最近就遇到了这样一个难题:明明我们网站右下角就有联系客服按钮、而且我们每道面试题目下...

年收入40万的网文作者,是不是可以吊打任何职业了?

哈,我二十岁的时候也这么狂。想当年,我一个大二学生,在宿舍里写出一本大精品,月入两万五,在学校里走路都是横着的,游戏卡池一开,看都不看,氪到出货为止。iPhone12pro一发布直接买,还买了个iPad Pro,都是官网直接买的,Apple...

你们发文章的插图都是从哪来的?在网上搜出来的图能用吗?

你们发文章的插图都是从哪来的?在网上搜出来的图能用吗?

写文章发帖子,里面的插图尽量自己画,自己编辑,避免引起版权或者所有权纠纷。我写过6本书,在知乎上也写了4000篇帖子和文章,其中的插图都是自己绘制的,照片绝大多数是自己拍摄的,摘自技术样本的图则必须加以说明。自己制图,看似麻烦,但积少成多,...

为什么原来说 7 nm 是半导体工艺的极限,但现在又被突破了?

10年前我们觉得65nm工艺是极限,因为到了65nm节点二氧化硅绝缘层漏电已经不可容忍。所以工业界搞出了HKMG,用high-k介质取代了二氧化硅,传统的多晶硅-二氧化硅-单晶硅结构变成了金属-highK-单晶硅结构。5年前我们觉得22nm...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。