MCPcopy Create free account
hub / github.com/PGshen/thinking-map / demoTextStreamParser

Function demoTextStreamParser

web/src/lib/sse-parser.ts:1011–1054  ·  view source on GitHub ↗

* 演示如何使用纯文本流解析器

()

Source from the content-addressed store, hash-verified

1009 * 演示如何使用纯文本流解析器
1010 */
1011function demoTextStreamParser(): void {
1012 const textParser = new SseTextStreamParser();
1013
1014 // 方式1:使用配置对象
1015 textParser.connect({
1016 method: 'POST',
1017 url: '/api/stream/text',
1018 param: { prompt: 'Hello, world!' },
1019 onText: (newText, fullText) => {
1020 console.log('新文本片段:', newText);
1021 console.log('完整文本:', fullText);
1022 },
1023 onComplete: (fullText) => {
1024 console.log('文本接收完成:', fullText);
1025 },
1026 close: () => {
1027 console.log('连接已关闭');
1028 },
1029 error: (err) => {
1030 console.error('连接错误:', err);
1031 }
1032 });
1033
1034 // 方式2:使用参数形式
1035 // textParser.connect(
1036 // 'POST',
1037 // '/api/stream/text',
1038 // { prompt: 'Hello, world!' },
1039 // (newText, fullText) => console.log('新文本:', newText),
1040 // (fullText) => console.log('完成:', fullText),
1041 // () => console.log('关闭'),
1042 // (err) => console.error('错误:', err)
1043 // );
1044
1045 // 获取当前累积的文本
1046 setTimeout(() => {
1047 console.log('当前文本:', textParser.getFullText());
1048 }, 5000);
1049
1050 // 10秒后断开连接
1051 setTimeout(() => {
1052 textParser.disconnect();
1053 }, 10000);
1054}
1055
1056function demoWithSSE(): void {
1057 const jsonStream = new SseJsonStreamParser(true);

Callers

nothing calls this directly

Calls 3

connectMethod · 0.95
getFullTextMethod · 0.95
disconnectMethod · 0.95

Tested by

no test coverage detected