{"id":396,"date":"2026-03-27T10:53:00","date_gmt":"2026-03-27T02:53:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=396"},"modified":"2026-06-14T10:55:38","modified_gmt":"2026-06-14T02:55:38","slug":"sse%e6%b5%81%e5%bc%8f%e8%bf%94%e5%9b%9e%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/27\/sse%e6%b5%81%e5%bc%8f%e8%bf%94%e5%9b%9e%e4%bf%a1%e6%81%af\/","title":{"rendered":"SSE\u6d41\u5f0f\u8fd4\u56de\u4fe1\u606f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>SSE\uff08Server-Sent Events\uff09<\/strong> \u662f\u4e00\u79cd<strong>\u6d4f\u89c8\u5668\u539f\u751f\u652f\u6301\u7684\u3001\u5355\u5411\u7684\u6d41\u5f0f\u901a\u4fe1\u534f\u8bae<\/strong>\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u670d\u52a1\u5668 \u2192 \u5ba2\u6237\u7aef \u6301\u7eed\u4e0d\u65ad\u63a8\u9001\u6570\u636e\uff08\u6bd4\u5982\uff1a\u6d88\u606f\u3001token\u3001\u8fdb\u5ea6\uff09<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u57fa\u4e8e <strong>HTTP \u534f\u8bae<\/strong>\uff0c\u4f7f\u7528 MIME \u7c7b\u578b <code><strong>text\/event-stream<\/strong><\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u7279\u6027<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5355\u5411\u901a\u4fe1\uff1a\u53ea\u80fd\u4ece\u670d\u52a1\u7aef\u63a8\u9001\u5230\u5ba2\u6237\u7aef\uff08\u4e0d\u80fd\u53cd\u5411\uff09<\/li>\n\n\n\n<li>\u57fa\u4e8e HTTP\/1.x\uff1a\u65e0\u9700\u989d\u5916\u534f\u8bae\uff0c\u6d4f\u89c8\u5668\u5929\u7136\u652f\u6301<\/li>\n\n\n\n<li>\u4fdd\u6301\u8fde\u63a5\u3001\u5b9e\u65f6\u63a8\u9001\uff1a\u670d\u52a1\u7aef\u4e3b\u52a8\u53d1\u9001\u6570\u636e\uff0c\u5ba2\u6237\u7aef\u81ea\u52a8\u63a5\u6536<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6d41\u5f0f\u5927\u6a21\u578b\u8f93\u51fa\uff08\u5982 ChatGPT\/Ollama\uff09<\/li>\n\n\n\n<li>\u5b9e\u65f6\u65e5\u5fd7\u3001\u76d1\u63a7\u4fe1\u606f\u63a8\u9001<\/li>\n\n\n\n<li>\u80a1\u7968\/\u5929\u6c14\/\u8ba2\u5355\u72b6\u6001\u5b9e\u65f6\u66f4\u65b0<\/li>\n\n\n\n<li>\u6253\u5b57\u673a\u5f0f\u7684\u95ee\u7b54\u673a\u5668\u4eba<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d41\u5f0f\u5904\u7406<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6d41\u5f0f\u8bf7\u6c42\u4e2d\uff08\u5982 Ollama\u3001OpenAI\u3001Claude \u7684 <code>stream: true<\/code> \u6a21\u5f0f\uff09\uff0c\u54cd\u5e94\u4f53\u4e0d\u662f\u4e00\u6b21\u6027\u5b8c\u6574\u8fd4\u56de\uff0c\u800c\u662f<strong>\u6309\u5757\u5206\u6279\u8fd4\u56de<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982\u4f60\u8bf7\u6c42\u4e86\u5927\u6a21\u578b\u56de\u7b54\u95ee\u9898\uff0c\u5b83\u4e0d\u4f1a\u4e00\u4e0b\u5b50\u8fd4\u56de\u6574\u6bb5\u6587\u5b57\uff0c\u800c\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\u4f60\"<br>\"\u597d\"<br>\"\uff0c\"<br>\"\u8bf7\u95ee\"<br>\"\u9700\u8981\"<br>\"\u5e2e\u52a9\u5417\uff1f\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u5b57\u7b26\u88ab\u62c6\u6210\u82e5\u5e72\u4e2a\u201cchunk\u201d\uff0c\u9010\u4e2a\u8fd4\u56de\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u5f00\u542f stream \u6d41\u5f0f\u6a21\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const response = await fetch(\"\/api\/generate\", {<br> &nbsp;method: \"POST\",<br> &nbsp;body: JSON.stringify({ prompt: \"\u4f60\u662f\u8c01\uff1f\", stream: true }),<br>});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>response.body<\/code> \u5c31\u662f ReadableStream \u53ef\u8bfb\u6d41\uff0c\u53ef\u8bfb\u6d41\u6709\u4e00\u4e2a <code>getReader<\/code> \u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u62ff\u5230\u4e00\u4e2a Reader \u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const reader = response.body.getReader();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5 Reader \u5bf9\u8c61\u4e0a\u9762\u6709\u4e00\u4e2a read \u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bfb\u53d6\u6bcf\u4e00\u4e2a chunk\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (true) {<br> &nbsp;\/\/ \u4e0d\u65ad\u7684\u53bb\u8bfb\u6bcf\u4e00\u4e2a\u5757\uff0c\u53d6\u51fa\u5f53\u524d\u5757\u6240\u5bf9\u5e94\u7684\u6570\u636e<br> &nbsp;const { done, value } = await reader.read();<br> &nbsp;if (done) break; \/\/ \u8fdb\u5165\u8be5 if\uff0c\u8bf4\u660e\u6ca1\u6709\u6570\u636e\u4e86<br>}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>value<\/code>: \u4e00\u4e2a <code>Uint8Array<\/code>\uff0c\u4ee3\u8868\u5f53\u524d\u7684<strong>\u5b57\u8282\u6570\u636e\u5757<\/strong>\u3002<\/li>\n\n\n\n<li><code>done<\/code>: \u662f\u5426\u5df2\u7ecf\u8bfb\u5b8c\uff08<code>true<\/code> \u8868\u793a\u6240\u6709\u6570\u636e\u5df2\u8bfb\u53d6\u5b8c\u6bd5\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u73b0\u5728\u8bfb\u53d6\u5230\u7684\u662f<strong>\u4e8c\u8fdb\u5236\u6570\u636e<\/strong>\uff0c\u6211\u4eec\u9700\u8981\u53bb\u89e3\u7801\uff0c\u56e0\u6b64\u6211\u4eec\u521b\u5efa\u4e00\u4e2a UTF-8 \u89e3\u7801\u5668\uff0c\u5c06\u4ece <code>reader.read()<\/code> \u62ff\u5230\u7684 \u4e8c\u8fdb\u5236 Uint8Array \u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const decoder = new TextDecoder(\"utf-8\");<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>TextDecoder<\/code> \u4f1a\u81ea\u52a8\u5904\u7406\u4e2d\u6587\u3001emoji \u8fd9\u7c7b\u591a\u5b57\u8282\u5b57\u7b26\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u7801\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const chunk = decoder.decode(value, { stream: true });<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\u5176\u4e2d\u4e00\u4e2a value \u89e3\u7801\u540e\u7ed3\u679c\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chunk = '{\"response\":\"\u4f60\u597d\"}\\n{\"response\":\"\uff0c\"}\\n'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u8bbe\u7f6e <code>stream: true<\/code> \u7684\u4f5c\u7528\u662f\uff1a<strong>\u4fdd\u7559\u672a\u5b8c\u6574\u5b57\u7b26<\/strong>\u5230\u4e0b\u4e00\u6b21\u89e3\u7801\u7ee7\u7eed\u62fc\u63a5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982\u5982\u679c\u53ea\u8bfb\u5230 <code>{\"response\":\"\u4f60<\/code>\uff0c\u5b83\u4e0d\u4f1a\u62a5\u9519\uff0c\u800c\u662f\u7b49\u5f85\u4e0b\u6b21\u8865\u4e0a <code>\u597d\"}<\/code> \u518d\u7ec4\u5408\u8d77\u6765\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u5c06 stream \u8bbe\u7f6e\u4e3a false\uff0c\u9047\u5230\u4ee5\u4e0b\u60c5\u51b5\u4f1a\u51fa bug\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Uint8Array.from(&#91;123, 34, 114, 101, 115, 112, 111, 110, 115, 101, 34, 58, 34, 228])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u90e8\u5206\u5b57\u7b26 <code>\"\u4f60\"<\/code> \u7684 utf-8\uff0c\u4f46\u8fd8\u6ca1\u5b8c\u6574\uff1b<code>stream: false<\/code> \u4f1a\u62a5\u9519\uff08\u56e0\u4e3a\u5b57\u7b26\u4e0d\u5b8c\u6574\uff09\uff1b<code>stream: true<\/code> \u4f1a\u201c\u8bb0\u4f4f\u201d\u8fd9\u4e2a\u672a\u5b8c\u6574\u5b57\u7b26\uff0c\u7b49\u4e0b\u4e00\u6b21\u62fc\u63a5\u56de\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SSE\uff08Server-Sent Events\uff09 \u662f..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,6,7],"tags":[],"class_list":["post-396","post","type-post","status-publish","format-standard","hentry","category-ai","category-news","category-keji"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/ai\/\" rel=\"category tag\">AI<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/news\/\" rel=\"category tag\">\u65b0\u95fb<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/keji\/\" rel=\"category tag\">\u79d1\u6280<\/a>","tag_info":"\u79d1\u6280","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"predecessor-version":[{"id":397,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/396\/revisions\/397"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}