虚拟空间的网站赚钱吗,学什么技术挣两三万,iis 子网站,永城市专业做网站引言
在现代 Web 应用中#xff0c;实时数据展示是一个常见需求#xff0c;例如聊天消息逐字显示、日志实时推送、股票行情更新等。传统的轮询或一次性数据加载方式无法满足这类场景的流畅体验#xff0c;而 流式传输#xff08;Streaming#xff09; 技术则能实现数据的…
引言
在现代 Web 应用中实时数据展示是一个常见需求例如聊天消息逐字显示、日志实时推送、股票行情更新等。传统的轮询或一次性数据加载方式无法满足这类场景的流畅体验而 流式传输Streaming 技术则能实现数据的“边接收边渲染”。本文将介绍如何在 Vue 项目中利用 EventStream基于 Server-Sent Events, SSE实现文字流式输出效果并提供完整代码示例和优化思路。 一、什么是 EventStream
EventStream 是 HTML5 中 Server-Sent EventsSSE的实现允许服务器通过 HTTP 协议向客户端推送实时数据流。与 WebSocket 不同SSE 是单向通信服务端到客户端适合需要实时更新但交互简单的场景如新闻推送、实时日志等。
核心优势 基于 HTTP无需复杂协议 自动重连机制 轻量级兼容性良好 二、实现思路 服务端通过 SSE 接口持续推送数据流文本片段。 客户端使用 EventSource 监听服务端事件逐步拼接并渲染数据。 Vue 组件动态更新 DOM实现文字逐字输出或分段显示效果。 三、代码实现
1. 服务端Node.js Express // server.js
const express require(express);
const app express();app.get(/stream, (req, res) {res.setHeader(Content-Type,text/event-stream);res.setHeader(Cache-Control, no-cache);res.setHeader(Connection, keep-alive);// 模拟流式数据推送const messages [Hello, , , this , is , a , streaming , demo!];let index 0;const timer setInterval(() {if (index messages.length) {res.write(data: ${messages[index]}\n\n); // SSE 格式要求} else {clearInterval(timer);res.end();}}, 500);
});app.listen(3000, () {console.log(Server running on http://localhost:3000);
}); 2. 客户端Vue 组件
templatediv classstream-containerdiv classcontent{{ streamText }}/div/div
/templatescript
import { ref, onMounted, onUnmounted } from vue;export default {setup() {const streamText ref();let eventSource null;// 初始化 EventSource 连接const initStream () {eventSource new EventSource(http://localhost:3000/stream);eventSource.onmessage (event) {streamText.value event.data; // 逐步拼接文本};eventSource.onerror (error) {console.error(EventStream error:, error);eventSource.close();};};onMounted(() {initStream();});onUnmounted(() {if (eventSource) eventSource.close();});return { streamText };}
};
/scriptstyle
.stream-container {padding: 20px;border: 1px solid #eee;
}
.content {white-space: pre-wrap;
}
/style 四、优化与扩展
1. 添加加载状态
const isLoading ref(false);eventSource.onopen () {isLoading.value true;
};
eventSource.onmessage () {isLoading.value false;
};
2. 实现逐字输出动画
// 修改 onmessage 逻辑
eventSource.onmessage (event) {const chars event.data.split();chars.forEach((char, i) {setTimeout(() {streamText.value char;}, i * 50); // 每个字符间隔50ms});
};
3. 错误处理与重连
const reconnect () {if (eventSource) eventSource.close();setTimeout(initStream, 3000); // 3秒后重连
};eventSource.onerror (error) {console.error(Connection error, reconnecting..., error);reconnect();
};
4. 使用第三方库优化 vue-use集成 useEventSource 快速实现 SSE axios通过 CancelToken 管理流式请求 五、注意事项 跨域问题确保服务端设置 CORS 头如 Access-Control-Allow-Origin。 性能优化避免频繁 DOM 操作大数据量时考虑虚拟滚动。 兼容性SSE 不支持 IE可使用 Polyfill如 eventsource 库。 数据格式遵循 SSE 规范每条消息以 data: 开头结尾加 \n\n。 结语
通过 EventStream 实现流式输出不仅能提升用户体验还能减少不必要的带宽消耗。本文提供的方案可扩展至实时日志监控、AI 对话等场景。如果你有更好的实现思路欢迎在评论区交流
文章转载自: http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.qshxh.cn.gov.cn.qshxh.cn http://www.morning.phxdc.cn.gov.cn.phxdc.cn http://www.morning.nwllb.cn.gov.cn.nwllb.cn http://www.morning.jfxth.cn.gov.cn.jfxth.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn http://www.morning.qyllw.cn.gov.cn.qyllw.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.c7500.cn.gov.cn.c7500.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.hmbxd.cn.gov.cn.hmbxd.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.fpczq.cn.gov.cn.fpczq.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.mdplm.cn.gov.cn.mdplm.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.dnls.cn.gov.cn.dnls.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.qbfkz.cn.gov.cn.qbfkz.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.dpflt.cn.gov.cn.dpflt.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.nfpct.cn.gov.cn.nfpct.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.qsbcg.cn.gov.cn.qsbcg.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn