{"id":1922,"date":"2024-06-02T03:38:27","date_gmt":"2024-06-02T03:38:27","guid":{"rendered":"https:\/\/www.nicekj.com\/?p=1922"},"modified":"2024-06-02T03:38:54","modified_gmt":"2024-06-02T03:38:54","slug":"stream-response-liushifanhui","status":"publish","type":"post","link":"https:\/\/www.nicekj.com\/stream-response-liushifanhui.html","title":{"rendered":"Stream Response \u6d41\u5f0f\u8fd4\u56de"},"content":{"rendered":"<h2 data-id=\"heading-0\">\u524d\u8a00<\/h2>\n<p>\u5728 OpenAI \u63d0\u4f9b\u7684 API \u4e2d\u5bf9\u4e8e\u5e94\u7528\u7684\u54cd\u5e94\u6709\u4e00\u79cd\u79f0\u4e3a Stream Response \u7684\u683c\u5f0f\uff0c\u5373\u6d41\u5f0f\u8fd4\u56de\u3002Chat Completions API \u548c Assistants API \u8fd9\u4e24\u79cd\u5f62\u5f0f\u7684\u63a5\u53e3\u90fd\u652f\u6301\u6d41\u5f0f\u8fd4\u56de\u3002<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/9e04dd5f9750df87d46eda11ab6aa26c.png\" alt=\"open-ai-api.png\" \/><\/figure>\n<\/p>\n<p>\u901a\u8fc7\u8fd9\u91cc\u7684\u89e3\u91ca\u8bf4\u660e\uff0c\u6211\u4eec\u77e5\u9053\u6d41\u5f0f\u8fd4\u56de\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u6574\u4f53\u7684\u4e00\u90e8\u5206\uff0c\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u9075\u5faa\u4e86 Server-sent events \u6807\u51c6\u3002\u4e3a\u4ec0\u4e48\u8981\u8fd4\u56de\u90e8\u5206\uff1f\u4ec0\u4e48\u662f Sever-sent events \uff1f\u4e0b\u9762\u5c31\u6765\u4e86\u89e3\u4e00\u4e0b\u3002<\/p>\n<h2 data-id=\"heading-1\">\u4e3a\u4ec0\u4e48\u9700\u8981\u6d41\u5f0f\u8fd4\u56de<\/h2>\n<p>\u6d41\u5f0f\u8fd4\u56de\uff0c\u5c31\u662f\u53ef\u4ee5\u4f18\u5148\u8fd4\u56de\u4e00\u4e2a\u54cd\u5e94\u4e2d\u7684\u4e00\u90e8\u5206\u3002\u4f7f\u7528 ChatGPT \u6216\u8005\u662f\u7c7b\u4f3c\u4ea7\u54c1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u804a\u5929\u754c\u9762\u7684\u56de\u7b54\u5e76\u4e0d\u662f\u4e00\u6b21\u6027\u8fd4\u56de\u6240\u6709\u7684\u5185\u5bb9,\u800c\u662f\u51e0\u4e2a\u5b57\u8fde\u7eed\u4e0d\u65ad\u7684\u8e66\u51fa\u6765\uff0c\u6709\u70b9\u50cf\u6253\u5b57\u673a\u7684\u6548\u679c\u3002<\/p>\n<p>\u8fd9\u5176\u5b9e\u662f\u7531\u4e8e ChatGPT \u7684\u7279\u6027\u5bfc\u81f4\u7684\uff0cChatGPT \u672c\u8d28\u4e0a\u662f\u57fa\u4e8e\u7528\u6237\u7684\u8f93\u5165\u731c\u6d4b\u4e0b\u4e00\u4e2a\u8bcd\uff0c\u6bcf\u4e00\u6b21\u7684\u8f93\u51fa\u4e5f\u662f\u8f93\u5165\uff0c\u4ed6\u4f1a\u8fde\u5e26\u7740\u8fd9\u4e2a\u8f93\u5165\u518d\u6b21\u53bb\u83b7\u53d6\u4e0b\u4e00\u4e2a\u6700\u5408\u9002\u7684\u8bcd\uff08\u8fd9\u91cc\u7684\u8bcd\u6cdb\u6307\u4e00\u4e2a token\uff09\u3002\u8fd9\u6837\u4e00\u6765\u4e00\u5f80\uff0c\u5c31\u4f1a\u6709\u4e24\u4e2a\u975e\u5e38\u660e\u663e\u7684\u7ed3\u8bba\u3002<\/p>\n<ol>\n<li>\u6a21\u578b\u8fd4\u56de\u5b8c\u6574\u54cd\u5e94\u4f1a\u6bd4\u8f83\u8017\u65f6\uff0c\u8fd4\u56de\u7ed3\u679c\u8d8a\u957f\uff0c\u9700\u8981\u7684\u65f6\u95f4\u5c31\u8d8a\u591a\u3002<\/li>\n<li>\u6a21\u578b\u8fd4\u56de\u7684\u54cd\u5e94\uff0c\u5df2\u7ecf\u8f93\u51fa\u7684\u5185\u5bb9\u662f\u4e0d\u4f1a\u53d8\u7684\u3002<\/li>\n<\/ol>\n<p>\u57fa\u4e8e\u8fd9\u4e24\u4e2a\u7279\u70b9\uff0c\u6d41\u5f0f\u8fd4\u56de\u5c31\u663e\u5f97\u5f88\u5fc5\u8981\u4e86\u3002<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u63a5\u53e3\u4f7f\u7528\u6d41\u5f0f\u8fd4\u56de\u7684\u5f62\u5f0f\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u4f7f\u7528\u7684\u8fc7\u7a0b\u4e2d\u4f18\u5148\u770b\u5230\u4e00\u90e8\u5206\u5185\u5bb9\uff0c\u4e0d\u4f1a\u9677\u5165\u7b49\u5f85\u7684\u7126\u8651\u60c5\u7eea\u4e2d\u3002<\/li>\n<li>\u5176\u6b21\uff0c\u901a\u8fc7\u6d41\u5f0f\u8fd4\u56de\u90e8\u5206\u7ed3\u679c\uff0c\u7528\u6237\u53ef\u4ee5\u63d0\u524d\u5f97\u77e5\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u5426\u662f\u81ea\u5df1\u60f3\u8981\u7684\uff0c\u53ef\u4ee5\u53ca\u65f6\u4e2d\u65ad\u6216\u8005\u4fee\u6539 prompt \u4e4b\u518d\u6b21\u91cd\u65b0\u8bf7\u6c42\u3002<\/li>\n<li>\u6700\u540e\uff0c\u901a\u8fc7\u6d41\u5f0f\u8fd4\u56de\u53ef\u4ee5\u51cf\u8f7b\u670d\u52a1\u5668\u7684\u538b\u529b\u3002\u6839\u636e\u7b2c\u4e8c\u70b9\u7684\u60c5\u51b5\uff0c\u7528\u6237\u7531\u4e8e\u5bf9\u8f93\u51fa\u7ed3\u679c\u7684\u4e0d\u6ee1\u610f\u63d0\u524d\u7ec8\u6b62\u672c\u6b21\u4f1a\u8bdd\uff0c\u65e2\u8282\u7701\u4e86\u7528\u6237\u65f6\u95f4\uff0c\u53c8\u907f\u514d\u4e86\u65e0\u6548\u7684\u8ba1\u7b97\u3002\u518d\u6709\uff0c\u901a\u8fc7\u6d41\u5f0f\u8fd4\u56de\u53ef\u4ee5\u6781\u5927\u7684\u51cf\u8f7b\u5e26\u5bbd\u538b\u529b\uff0c\u51cf\u5c11\u4e86\u5927\u91cf\u6d41\u91cf\u5c16\u523a\u7684\u51fa\u73b0\u51e0\u7387\u3002<\/li>\n<\/ul>\n<p><em>\u672a\u6765\u968f\u7740 LLM \u7684\u9010\u6e10\u53d1\u5c55\uff0c\u7b97\u529b\u6210\u672c\u4e0b\u964d\u4e4b\u540e\uff0c\u54cd\u5e94\u901f\u5ea6\u7406\u8bba\u4e0a\u4f1a\u8d8a\u6765\u8d8a\u5feb\uff0c\u957f\u6587\u672c\u80fd\u591f\u5feb\u901f\u8f93\u51fa\u7684\u8bdd\uff0c\u6d41\u5f0f\u8fd4\u56de\u53ef\u80fd\u5c31\u6ca1\u6709\u592a\u5927\u7684\u5fc5\u8981\u4e86<\/em><\/p>\n<h2 data-id=\"heading-2\">\u6d41\u5f0f\u8fd4\u56de SSE<\/h2>\n<h3 data-id=\"heading-3\">SSE \u662f\u4ec0\u4e48<\/h3>\n<p>\u56de\u5230\u4f7f\u7528 ChatGPT \u804a\u5929\u7684\u771f\u5b9e\u573a\u666f\uff0c\u5bf9\u4e8e\u7528\u6237\u7684\u4e00\u6b21\u8bf7\u6c42\uff0c\u6a21\u578b\u6bcf\u6b21\u53ea\u80fd\u8fd4\u56de\u6700\u7ec8\u54cd\u5e94\u7684\u4e00\u90e8\u5206\uff0c\u76f4\u5230\u6a21\u578b\u5185\u90e8\u7684\u795e\u7ecf\u7f51\u7edc<strong>\u8ba4\u4e3a<\/strong>\u81ea\u5df1\u5b8c\u6210\u4e86\u8fd9\u4e2a\u95ee\u9898\u7684\u56de\u7b54\u4e3a\u6b62\u3002\u5bf9\u4e8e\u6a21\u578b\u4e4b\u4e0a\u7684\u5e94\u7528\u5c42\u670d\u52a1\u5668\u6765\u8bf4\uff0c\u8981\u4e48\u7b49\u6a21\u578b\u8f93\u51fa\u6240\u6709\u7ed3\u679c\u4e4b\u540e\u7edf\u4e00\u8fd4\u56de\uff0c\u8981\u4e48\u4e0d\u65ad\u628a\u6a21\u578b\u7684\u8f93\u51fa\u7ed3\u679c\u5b9e\u65f6\u8fd4\u56de\u7ed9\u53d1\u8d77\u8bf7\u6c42\u7684\u5ba2\u6237\u7aef\u3002\u9274\u4e8e\u54cd\u5e94\u901f\u5ea6\u548c\u7528\u6237\u4f53\u9a8c\u7684\u8003\u91cf\uff0c\u5b9e\u65f6\u8fd4\u56de\u7ed3\u679c\u65e0\u7591\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u8fd9\u91cc\u5c31\u6d89\u53ca\u5230 <strong>\u670d\u52a1\u7aef\u5982\u4f55\u628a\u6d88\u606f\u5b9e\u65f6\u63a8\u9001\u7ed9\u5ba2\u6237\u7aef<\/strong> \u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0c\u5178\u578b\u7684\u89e3\u51b3\u65b9\u5f0f\u662f\u7ef4\u62a4\u957f\u94fe\u63a5\uff0c\u5b9e\u65f6\u63a8\u9001\u6d88\u606f\uff0c\u4f7f\u7528 MQTT\uff0cWebSocket \u8fd9\u7c7b\u6807\u51c6\u7684\u7ec4\u4ef6\u3002\u4f46\u662f\u7528\u8fd9\u4e9b\u7ec4\u4ef6\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6709\u663e\u5f97\u8fc7\u4e8e\u91cd\u548c\u590d\u6742\u3002\u800c\u4f7f\u7528 SSE \u8fd9\u79cd\u66f4\u52a0\u8f7b\u91cf\u7ea7\u7684\u534f\u8bae\u53ef\u4ee5\u66f4\u7b80\u5355\u4e00\u70b9\u3002<\/p>\n<h4 data-id=\"heading-4\">SSE\uff08Server-Send Events\uff09<\/h4>\n<p>SSE \u662f\u4e00\u79cd\u5728\u57fa\u4e8e\u6d4f\u89c8\u5668\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4ec5\u4ece\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6587\u672c\u6d88\u606f\u7684\u6280\u672f\u3002SSE\u57fa\u4e8e HTTP \u534f\u8bae\u4e2d\u7684\u6301\u4e45\u8fde\u63a5\uff0c \u5177\u6709\u7531 W3C \u6807\u51c6\u5316\u7684\u7f51\u7edc\u534f\u8bae\u548c EventSource \u5ba2\u6237\u7aef\u63a5\u53e3\uff0c\u4f5c\u4e3a HTML5 \u6807\u51c6\u5957\u4ef6\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<h4 data-id=\"heading-5\">SSE \u548c WebSocket \u5bf9\u6bd4<\/h4>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<table><thead><tr><th>Server-Sent Events AP<\/th><th>IWebSockets API<\/th><\/tr><\/thead><tbody><tr><td>\u57fa\u4e8e HTTP \u534f\u8bae<\/td><td>\u57fa\u4e8e TCP \u534f\u8bae<\/td><\/tr><tr><td>\u5355\u5de5\uff0c\u53ea\u80fd\u670d\u52a1\u7aef\u5355\u5411\u53d1\u9001\u6d88\u606f<\/td><td>\u5168\u53cc\u5de5\uff0c\u53ef\u4ee5\u540c\u65f6\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f<\/td><\/tr><tr><td>\u8f7b\u91cf\u7ea7\uff0c\u4f7f\u7528\u7b80\u5355<\/td><td>\u76f8\u5bf9\u590d\u6742<\/td><\/tr><tr><td>\u5185\u7f6e\u65ad\u7ebf\u91cd\u8fde\u548c\u6d88\u606f\u8ffd\u8e2a\u7684\u529f\u80fd<\/td><td>\u4e0d\u5728\u534f\u8bae\u8303\u56f4\u5185\uff0c\u9700\u624b\u52a8\u5b9e\u73b0<\/td><\/tr><tr><td>\u6587\u672c\u6216\u4f7f\u7528 Base64 \u7f16\u7801\u548c gzip \u538b\u7f29\u7684\u4e8c\u8fdb\u5236\u6d88\u606f<\/td><td>\u7c7b\u578b\u5e7f\u6cdb<\/td><\/tr><tr><td>\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b<\/td><td>\u4e0d\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b<\/td><\/tr><tr><td>\u8fde\u63a5\u6570 HTTP\/1.1 6 \u4e2a\uff0cHTTP\/2 \u53ef\u534f\u5546\uff08\u9ed8\u8ba4 100\uff09<\/td><td>\u8fde\u63a5\u6570\u65e0\u9650\u5236<\/td><\/tr><\/tbody><\/table>\n<h3 data-id=\"heading-6\">\u670d\u52a1\u7aef\u534f\u8bae<\/h3>\n<blockquote>\n<p>\u53d1\u9001\u4e8b\u4ef6\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u9700\u8981\u4f7f\u7528 text\/event-stream MIME \u7c7b\u578b\u54cd\u5e94\u5185\u5bb9\u3002\u6bcf\u4e2a\u901a\u77e5\u4ee5\u6587\u672c\u5757\u5f62\u5f0f\u53d1\u9001\uff0c\u5e76\u4ee5\u4e00\u5bf9\u6362\u884c\u7b26\u7ed3\u5c3e\u3002<\/p>\n<\/blockquote>\n<p>\u8fd9\u91cc\u5f15\u7528<a href=\"https:\/\/link.juejin.cn?target=https%3A%2F%2Fwww.zhihu.com%2Fquestion%2F65436677\" target=\"_blank\" title=\"https:\/\/www.zhihu.com\/question\/65436677\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">\u77e5\u4e4e\u56de\u7b54<\/a>\u4e2d\u5bf9\u534f\u8bae\u7684\u4ecb\u7ecd<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/2c5d39675d1ebeb2785a5a3a93fb5e39.png\" alt=\"sever_protocol.png\" \/><\/figure>\n<\/p>\n<p>\u57fa\u4e8e\u4e0a\u9762\u8fd9\u4e2a\u7b80\u4ecb\u660e\u4e86\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6a21\u62df\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301 SSE \u7684\u670d\u52a1\u7aef\u3002<\/p>\n<h3 data-id=\"heading-7\">SSE \u670d\u52a1\u7aef<\/h3>\n<ul>\n<li>\u6784\u9020 SSE \u54cd\u5e94<\/li>\n<\/ul>\n\n<pre class=\"wp-block-code\"><code><span class=\"hljs-keyword\">type<\/span> SSEResponse <span class=\"hljs-keyword\">struct<\/span> {\n\tCode    <span class=\"hljs-type\">int<\/span>    <span class=\"hljs-string\">`json:\"code\"`<\/span>\n\tMessage <span class=\"hljs-type\">string<\/span> <span class=\"hljs-string\">`json:\"message\"`<\/span>\n\tEvent   Event  <span class=\"hljs-string\">`json:\"event\"`<\/span>\n}\n\n<span class=\"hljs-keyword\">type<\/span> Event <span class=\"hljs-keyword\">struct<\/span> {\n\tId   <span class=\"hljs-type\">string<\/span> <span class=\"hljs-string\">`json:\"id\"`<\/span>\n\tData <span class=\"hljs-type\">string<\/span> <span class=\"hljs-string\">`json:\"data\"`<\/span>\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">genSSEEvent<\/span><span class=\"hljs-params\">(event Event)<\/span><\/span> <span class=\"hljs-type\">string<\/span> {\n\tsseResponse := SSEResponse{<span class=\"hljs-number\">200<\/span>, <span class=\"hljs-string\">\"success\"<\/span>, event}\n\tresult, err := json.Marshal(sseResponse)\n\t<span class=\"hljs-keyword\">if<\/span> err != <span class=\"hljs-literal\">nil<\/span> {\n\t\tlog.Fatal(err)\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"\"<\/span>\n\t}\n\t<span class=\"hljs-keyword\">return<\/span> fmt.Sprintf(<span class=\"hljs-string\">\"data: %snn\"<\/span>, result)\n}\n<\/code><\/pre>\n\n<p>\u8fd9\u91cc\u5b9a\u4e49 SSE \u54cd\u5e94\u7684\u6570\u636e\u7ed3\u6784\u548c\u6784\u5efa SSE \u7684\u65b9\u5f0f\uff0c\u7531\u4e8e\u5ba2\u6237\u7aef\u89e3\u6790 SSE \u683c\u5f0f\u7684\u6570\u636e\u9700\u8981\u57fa\u4e8e\u6362\u884c\u7b26\u786e\u5b9a\uff0c\u56e0\u6b64\u4e00\u5b9a\u8981\u8981\u5728\u6bcf\u4e00\u6761\u6d88\u606f\u7684\u672b\u5c3e\u6dfb\u52a0\u6362\u884c\u7b26\u3002<\/p>\n<ul>\n<li>\u8fd4\u56de\u54cd\u5e94<\/li>\n<\/ul>\n\n<pre class=\"wp-block-code\"><code><span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">streamHandler<\/span><span class=\"hljs-params\">(w http.ResponseWriter, r *http.Request)<\/span><\/span> {\n\tflusher, ok := w.(http.Flusher)\n\t<span class=\"hljs-keyword\">if<\/span> !ok {\n\t\thttp.Error(w, <span class=\"hljs-string\">\"streaming not supported\"<\/span>, http.StatusInternalServerError)\n\t}\n\t<span class=\"hljs-comment\">\/\/ \u8bbe\u7f6e\u54cd\u5e94\u5934\uff0c\u8868\u660e\u54cd\u5e94\u662f\u6d41\u5f0f\u7684<\/span>\n\tw.Header().Set(<span class=\"hljs-string\">\"Content-Type\"<\/span>, <span class=\"hljs-string\">\"text\/event-stream\"<\/span>)\n\tw.Header().Set(<span class=\"hljs-string\">\"Cache-Control\"<\/span>, <span class=\"hljs-string\">\"no-cache\"<\/span>)\n\tw.Header().Set(<span class=\"hljs-string\">\"Connection\"<\/span>, <span class=\"hljs-string\">\"keep-alive\"<\/span>)\n\tw.WriteHeader(http.StatusOK)\n\n\t<span class=\"hljs-comment\">\/\/<\/span>\n\tdata := <span class=\"hljs-string\">\"\u670d\u52a1\u7aef\u5b9e\u65f6\u63a8\u9001\u6570\u636e\uff0c\u9664\u4e86\u7528 WebSocket \u5916\uff0c\u8fd8\u53ef\u4ee5\u7528 HTTP \u7684 Server Send Event\u3002\"<\/span> +\n\t\t<span class=\"hljs-string\">\"\u53ea\u8981 http \u8fd4\u56de Content-Type \u4e3a text\/event-stream \u7684 header\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7 stream \u7684\u65b9\u5f0f\u591a\u6b21\u8fd4\u56de\u6d88\u606f\u4e86\u3002\"<\/span>\n\teventID := strings.ReplaceAll(uuid.New().String(), <span class=\"hljs-string\">\"-\"<\/span>, <span class=\"hljs-string\">\"\"<\/span>)\n\trunes := []<span class=\"hljs-type\">rune<\/span>(data)\n\tsize := <span class=\"hljs-number\">0<\/span>\n\tisEnd := <span class=\"hljs-literal\">false<\/span>\n\t<span class=\"hljs-keyword\">for<\/span> {\n\t\t<span class=\"hljs-keyword\">select<\/span> {\n\t\t<span class=\"hljs-keyword\">case<\/span> <-r.Context().Done():\n\t\t\tlog.Println(<span class=\"hljs-string\">\"req done..\"<\/span>)\n\t\t\t<span class=\"hljs-keyword\">return<\/span>\n\t\t<span class=\"hljs-keyword\">case<\/span> <-time.After(<span class=\"hljs-number\">30<\/span> * time.Millisecond):\n\t\t\t<span class=\"hljs-keyword\">if<\/span> size < <span class=\"hljs-built_in\">len<\/span>(runes) {\n\t\t\t\tvalue := <span class=\"hljs-type\">string<\/span>(runes[size])\n\t\t\t\tevent := Event{eventID, value}\n\t\t\t\tresult := genSSEEvent(event)\n\t\t\t\t_, err := w.Write([]<span class=\"hljs-type\">byte<\/span>(result))\n\t\t\t\t<span class=\"hljs-keyword\">if<\/span> err != <span class=\"hljs-literal\">nil<\/span> {\n\t\t\t\t\tfmt.Println(err.Error())\n\t\t\t\t}\n\t\t\t\tsize++\n\n\t\t\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t\t\tfmt.Fprintf(w, io.EOF.Error())\n\t\t\t\tisEnd = <span class=\"hljs-literal\">true<\/span>\n\t\t\t}\n\t\t\tflusher.Flush()\n\t\t\t<span class=\"hljs-keyword\">if<\/span> isEnd {\n\t\t\t\tlog.Println(<span class=\"hljs-string\">\"finish\"<\/span>)\n\t\t\t\t<span class=\"hljs-keyword\">return<\/span>\n\t\t\t}\n\t\t}\n\n\t}\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">StartStreamServer<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n\thttp.HandleFunc(<span class=\"hljs-string\">\"\/stream_chat\"<\/span>, streamHandler)\n\tport := Port\n\t<span class=\"hljs-comment\">\/\/ \u542f\u52a8HTTP\u670d\u52a1\u5668<\/span>\n\terr := http.ListenAndServe(port, <span class=\"hljs-literal\">nil<\/span>)\n\t<span class=\"hljs-keyword\">if<\/span> err != <span class=\"hljs-literal\">nil<\/span> {\n\t\tfmt.Println(<span class=\"hljs-string\">\"Error:\"<\/span>, err)\n\t}\n}\n\n<\/code><\/pre>\n\n<p>\u8fd9\u91cc\u901a\u8fc7 select \u8d85\u65f6\u673a\u5236\u5ef6\u65f6\u8fd4\u56de\u4e00\u6bb5\u56fa\u5b9a\u7684\u5185\u5bb9\uff0c\u6a21\u62df LLM \u5355\u5b57\u8fd4\u56de\u6570\u636e\u7684\u6548\u679c\u3002<\/p>\n<h3 data-id=\"heading-8\">SSE \u6d4f\u89c8\u5668\u7aef<\/h3>\n<p>\u7531\u4e8e SSE \u662f HTML5 \u6807\u51c6\u5957\u4ef6\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5bf9\u4e8e\u670d\u52a1\u7aef SSE \u7c7b\u578b\u7684\u6d88\u606f\uff0c\u6d4f\u89c8\u5668\u6709\u5929\u7136\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7279\u5b9a\u7684\u7ec4\u4ef6\u975e\u5e38\u65b9\u4fbf\u5904\u7406\u7ed3\u679c\u3002<\/p>\n<p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u9875\u9762<\/p>\n\n<pre class=\"wp-block-code\"><code><span class=\"hljs-tag\"><<span class=\"hljs-name\">body<\/span>><\/span>\n<span class=\"hljs-tag\"><<span class=\"hljs-name\">form<\/span> <span class=\"hljs-attr\">method<\/span>=<span class=\"hljs-string\">\"post\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"simple_form\"<\/span>><\/span>\n    <span class=\"hljs-tag\"><<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"input\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"input\"<\/span>\/><\/span>\n    <span class=\"hljs-tag\"><<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"submit\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"send\"<\/span>\/><\/span>\n<span class=\"hljs-tag\"><\/<span class=\"hljs-name\">form<\/span>><\/span>\n<span class=\"hljs-tag\"><<span class=\"hljs-name\">br<\/span>\/><\/span>\n<span class=\"hljs-tag\"><<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"result\"<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"width: 600px\"<\/span>><\/span><span class=\"hljs-tag\"><\/<span class=\"hljs-name\">div<\/span>><\/span>\n<span class=\"hljs-tag\"><<span class=\"hljs-name\">script<\/span>><\/span><span class=\"javascript\">\n\n    <span class=\"hljs-keyword\">const<\/span> send = <span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\"><\/span>) {\n        <span class=\"hljs-keyword\">let<\/span> name = <span class=\"hljs-variable language_\">document<\/span>.<span class=\"hljs-title function_\">getElementById<\/span>(<span class=\"hljs-string\">\"input\"<\/span>).<span class=\"hljs-property\">value<\/span>\n        <span class=\"hljs-keyword\">let<\/span> resultArea = <span class=\"hljs-variable language_\">document<\/span>.<span class=\"hljs-title function_\">getElementById<\/span>(<span class=\"hljs-string\">\"result\"<\/span>)\n        resultArea.<span class=\"hljs-property\">innerText<\/span> = <span class=\"hljs-string\">\"\"<\/span>\n        <span class=\"hljs-keyword\">let<\/span> url = <span class=\"hljs-string\">\"stream_chat?name=\"<\/span> + name\n        <span class=\"hljs-keyword\">let<\/span> eventSource = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">EventSource<\/span>(url)\n\n        eventSource.<span class=\"hljs-property\">onopen<\/span> = <span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">event<\/span>) {\n            <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">\"open \"<\/span>, event)\n        }\n        eventSource.<span class=\"hljs-property\">onmessage<\/span> = <span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">event<\/span>) {\n            <span class=\"hljs-comment\">\/\/ console.log(event.data)<\/span>\n            <span class=\"hljs-keyword\">const<\/span> data = <span class=\"hljs-title class_\">JSON<\/span>.<span class=\"hljs-title function_\">parse<\/span>(event.<span class=\"hljs-property\">data<\/span>);\n            resultArea.<span class=\"hljs-property\">innerText<\/span> += data.<span class=\"hljs-property\">event<\/span>.<span class=\"hljs-property\">data<\/span>\n\n        }\n        eventSource.<span class=\"hljs-property\">onerror<\/span> = <span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">e<\/span>) {\n            <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(e)\n            eventSource.<span class=\"hljs-title function_\">close<\/span>()\n        }\n    };\n    <span class=\"hljs-variable language_\">document<\/span>.<span class=\"hljs-title function_\">getElementById<\/span>(<span class=\"hljs-string\">\"simple_form\"<\/span>).<span class=\"hljs-title function_\">addEventListener<\/span>(<span class=\"hljs-string\">\"submit\"<\/span>, <span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">event<\/span>) {\n        event.<span class=\"hljs-title function_\">preventDefault<\/span>()\n        <span class=\"hljs-title function_\">send<\/span>()\n    })\n<\/span><span class=\"hljs-tag\"><\/<span class=\"hljs-name\">script<\/span>><\/span>\n<span class=\"hljs-tag\"><\/<span class=\"hljs-name\">body<\/span>><\/span>\n<span class=\"hljs-tag\"><\/<span class=\"hljs-name\">html<\/span>><\/span>\n<\/code><\/pre>\n\n<p>\u5728\u8fd9\u91cc\u6211\u4eec\u901a\u8fc7 <code>event.preventDefault()<\/code> \u62e6\u622a\u8868\u5355\u63d0\u4ea4\u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u800c\u662f\u7528\u81ea\u5b9a\u4e49\u7684 send \u65b9\u6cd5\uff0c\u901a\u8fc7 EvevtSoource \u7ec4\u4ef6\u5b9e\u73b0\u5bf9 SSE \u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5904\u7406\u3002EventSource \u53ef\u4ee5\u76d1\u542c <code>open<\/code> ,<code>message<\/code> ,<code>error<\/code> \u8fd9\u51e0\u4e2a\u4e8b\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 <code>addEventListener<\/code> \u6dfb\u52a0\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3002<\/p>\n<p>\u6ce8\u518c\u4e00\u4e2a\u8def\u7531\u6e32\u67d3\u8fd9\u4e2a\u9875\u9762<\/p>\n\n<pre class=\"wp-block-code\"><code>\thttp.HandleFunc(<span class=\"hljs-string\">\"\/\"<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span><span class=\"hljs-params\">(writer http.ResponseWriter, request *http.Request)<\/span><\/span> {\n\t\tt, err := template.ParseFiles(<span class=\"hljs-string\">\"web\/stream\/index.html\"<\/span>)\n\t\t<span class=\"hljs-keyword\">if<\/span> err != <span class=\"hljs-literal\">nil<\/span> {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tlog.Println(t.Execute(writer, <span class=\"hljs-literal\">nil<\/span>))\n\t})\n<\/code><\/pre>\n\n<p>\u53ef\u4ee5\u770b\u4e00\u4e0b\u5177\u4f53\u7684\u6548\u679c<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/d2848d360da99bda46ce67a7cd7d595e.png\" alt=\"client.gif\" \/><\/figure>\n<\/p>\n<p>\u540c\u65f6\u4e5f\u53ef\u4ee5\u770b\u5230\u8fd4\u56de\u7684\u5185\u5bb9<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/23e6fe86683b075b42632380daff32b5.png\" alt=\"client_log.png\" \/><\/figure>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u901a\u8fc7 EventSource \u5df2\u7ecf\u53ef\u4ee5\u6b63\u5e38\u63a5\u6536\u670d\u52a1\u7aef\u8fd4\u56de\u7684 SSE \u683c\u5f0f\u7684\u6d88\u606f\u4e86\u3002<\/p>\n<h3 data-id=\"heading-9\">Fetch<\/h3>\n<p>\u867d\u7136\u4f7f\u7528 EventSource \u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u662f ChatGPT \u5e76\u6ca1\u6709\u4f7f\u7528\uff0c\u8fd9\u4e00\u70b9\u901a\u8fc7\u6d4f\u89c8\u5668\u67e5\u770b\u8bf7\u6c42\u65f6\u7684\u7f51\u7edc\u8c03\u7528\u5c31\u53ef\u4ee5\u770b\u5230\uff0c\u800c\u662f\u4f7f\u7528\u4e86 Fetch\u3002\u56e0\u4e3a\u6d4f\u89c8\u5668\u7aef\u7684 EventSource \u6709\u4e00\u4e9b\u9650\u5236\u3002\u5305\u62ec\u963f\u91cc\u7684\u901a\u4e49\u5343\u95ee\u4e5f\u662f\u7c7b\u4f3c\u7684\u5b9e\u73b0\uff0c\u6bcf\u4e00\u6b21\u8bf7\u6c42\u90fd\u662f\u901a\u8fc7 Post \u53d1\u8d77\u3002<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/ee6ec6f483ef027b044ec3224f8d1910.png\" alt=\"tongyi.png\" \/><\/figure>\n<\/p>\n<h4 data-id=\"heading-10\">SSE \u7684\u7f3a\u70b9<\/h4>\n<ul>\n<li>\u53ea\u652f\u6301 GET \u8bf7\u6c42,\u90a3\u4e48\u8bf7\u6c42\u957f\u5ea6\u662f\u6709\u9650\u5236\u7684\uff0c\u8fd9\u5bf9\u4e8e\u4f7f\u7528\u573a\u666f\u5c31\u6709\u5f88\u6253\u7684\u7ea6\u675f\u4e86\uff0c\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4f1a\u53d7\u9650\u5236\u3002<\/li>\n<li>\u65e0\u6cd5\u81ea\u5b9a\u4e49 Header<\/li>\n<li>\u517c\u5bb9\u6027\uff0c\u5f53\u4e0d\u901a\u8fc7 HTTP\/2 \u4f7f\u7528\u65f6\uff0cSSE\uff08server-sent events\uff09\u4f1a\u53d7\u5230\u6700\u5927\u8fde\u63a5\u6570\u7684\u9650\u5236\uff0c\u8fd9\u5728\u6253\u5f00\u591a\u4e2a\u9009\u9879\u5361\u65f6\u7279\u522b\u9ebb\u70e6\uff0c\u56e0\u4e3a\u8be5\u9650\u5236\u662f\u9488\u5bf9\u6bcf\u4e2a\u6d4f\u89c8\u5668\u7684\uff0c\u5e76\u4e14\u88ab\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u975e\u5e38\u4f4e\u7684\u6570\u5b57\uff086\uff09\u3002\u8be5\u95ee\u9898\u5728 Chrome \u548c Firefox \u4e2d\u88ab\u6807\u8bb0\u4e3a\u201c\u4e0d\u4f1a\u89e3\u51b3\u201d\u3002\u6b64\u9650\u5236\u662f\u9488\u5bf9\u6bcf\u4e2a\u6d4f\u89c8\u5668 + \u57df\u7684\uff0c\u56e0\u6b64\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u8de8\u6240\u6709\u9009\u9879\u5361\u6253\u5f00 6 \u4e2a SSE \u8fde\u63a5\u5230 <a href=\"https:\/\/link.juejin.cn?target=http%3A%2F%2Fwww.example1.com%25EF%25BC%258C%25E5%25B9%25B6%25E6%2589%2593%25E5%25BC%2580\" target=\"_blank\" title=\"http:\/\/www.example1.com%EF%BC%8C%E5%B9%B6%E6%89%93%E5%BC%80\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">www.example1.com\uff0c\u5e76\u6253\u5f00<\/a> 6 \u4e2a SSE \u8fde\u63a5\u5230 <a href=\"https:\/\/link.juejin.cn?target=http%3A%2F%2Fwww.example2.com%25E3%2580%2582%25EF%25BC%2588%25E6%259D%25A5%25E8%2587%25AA\" target=\"_blank\" title=\"http:\/\/www.example2.com%E3%80%82%EF%BC%88%E6%9D%A5%E8%87%AA\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">www.example2.com\u3002\uff08\u6765\u81ea<\/a> Stackoverflow\uff09\u3002\u4f7f\u7528 HTTP\/2 \u65f6\uff0c\u540c\u4e00\u65f6\u95f4\u5185 HTTP \u6700\u5927\u8fde\u63a5\u6570\u7531\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e4b\u95f4\u534f\u5546\uff08\u9ed8\u8ba4\u4e3a 100\uff09\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u81ea\u5e26\u7684 fetch \u8fdb\u884c\u8bf7\u6c42\uff0c\u53ea\u4e0d\u8fc7\u4f1a\u7a0d\u5fae\u6709\u70b9\u9ebb\u70e6\u3002<\/p>\n<h4 data-id=\"heading-11\">\u7528 fetch \u5904\u7406 SSE \u6d88\u606f<\/h4>\n\n<pre class=\"wp-block-code\"><code><script>\n    <span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title function_\">loopRead<\/span> = (<span class=\"hljs-params\">reader<\/span>) => {\n        <span class=\"hljs-keyword\">return<\/span> reader.<span class=\"hljs-title function_\">read<\/span>().<span class=\"hljs-title function_\">then<\/span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">{done, value}<\/span>) =><\/span> {\n            <span class=\"hljs-keyword\">if<\/span> (done) {\n                <span class=\"hljs-keyword\">return<\/span>\n            }\n            <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">TextDecoder<\/span>().<span class=\"hljs-title function_\">decode<\/span>(value))\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-title function_\">loopRead<\/span>(reader)\n        })\n    }\n\n    <span class=\"hljs-variable language_\">document<\/span>.<span class=\"hljs-title function_\">getElementById<\/span>(<span class=\"hljs-string\">\"test_fetch\"<\/span>).<span class=\"hljs-title function_\">addEventListener<\/span>(<span class=\"hljs-string\">\"click\"<\/span>, <span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\"><\/span>) {\n        <span class=\"hljs-title function_\">fetch<\/span>(<span class=\"hljs-string\">\"stream_chat\"<\/span>)\n            .<span class=\"hljs-title function_\">then<\/span>(<span class=\"hljs-function\"><span class=\"hljs-params\">respose<\/span> =><\/span> {\n                <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">\"response \"<\/span>, respose)\n                <span class=\"hljs-keyword\">if<\/span> (respose.<span class=\"hljs-property\">status<\/span> === <span class=\"hljs-number\">200<\/span>) {\n                    <span class=\"hljs-keyword\">return<\/span> respose.<span class=\"hljs-property\">body<\/span>\n                }\n            })\n            .<span class=\"hljs-title function_\">then<\/span>(<span class=\"hljs-function\"><span class=\"hljs-params\">rb<\/span> =><\/span> {\n                <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-title function_\">loopRead<\/span>(rb.<span class=\"hljs-title function_\">getReader<\/span>())\n            })\n            .<span class=\"hljs-title function_\">catch<\/span>(<span class=\"hljs-function\"><span class=\"hljs-params\">error<\/span> =><\/span> {\n                <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">\"error is \"<\/span>, error)\n            })\n    })\n<\/script>\n<\/code><\/pre>\n\n<p>\u53ef\u4ee5\u770b\u4e00\u4e0b\u8f93\u51fa<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/d18c882272cb4aa57ff8016c5b2ba519.png\" alt=\"fetch_log.png\" \/><\/figure>\n<\/p>\n<p>\u901a\u8fc7\u65e5\u5fd7\u53ef\u4ee5\u770b\u5230\uff0c\u76f4\u63a5\u901a\u8fc7 fetch \u8bfb\u53d6\u54cd\u5e94\u65f6\uff0c\u662f\u6309\u7167\u670d\u52a1\u7aef\u751f\u6210 SSE \u54cd\u5e94\u65f6 <code>fmt.Sprintf(\"data: %snn\", result)<\/code> \u7684\u683c\u5f0f\u8fd4\u56de\u7684\u3002\u8fd9\u91cc\u5176\u5b9e\u5c31\u4e0d\u662f\u4e00\u4e2a\u6807\u51c6\u7684 json \u683c\u5f0f\uff0c\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\u4e86\u3002\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u5728\u670d\u52a1\u7aef\u5bf9\u8f93\u51fa\u683c\u5f0f\u8fdb\u884c\u7b80\u5316\uff0c\u4f46\u662f\u8fd9\u6837\u53c8\u65e0\u6cd5\u88ab EventSource \u5904\u7406\u4e86\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 SSE \u8981\u7ed3\u5408\u5b9e\u9645\u7684\u573a\u666f\u3002<\/p>\n<h2 data-id=\"heading-12\">\u5c0f\u7ed3<\/h2>\n<p>SSE \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5b9e\u65f6\u901a\u4fe1\u6280\u672f\uff0c\u53ea\u652f\u6301\u670d\u52a1\u7aef\u5411\u5ba2\u6237\u7aef\u5355\u5411\u63a8\u9001\u3002\u540c\u65f6\u7531\u4e8e\u6d4f\u89c8\u5668\u3001HTTP \u534f\u8bae\u7248\u672c\u7b49\u56e0\u7d20\u4f1a\u6709\u8bf8\u591a\u9650\u5236\uff0c\u5e76\u4e0d\u662f WebSocket \u7684\u66ff\u4ee3\u54c1\u3002\u5bf9\u5916\u66b4\u9732\u63a5\u53e3\u3001API \u53ef\u4ee5\u901a\u8fc7 SSE \u5c01\u88c5\uff0c\u4f46\u662f\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u5f53\u9047\u5230\u9650\u5236\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u8fdb\u884c\u6070\u5f53\u7684\u6539\u9020\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u573a\u666f\u3002\u5f53\u7136\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u5bf9\u5b9e\u65f6\u6027\u8981\u6c42\u66f4\u9ad8\u7684\u573a\u666f\uff0c\u90a3\u4e48\u5c31\u8981\u8003\u8651\u4f7f\u7528 WebSocket \u4e4b\u7c7b\u5176\u4ed6\u7684\u7ec4\u4ef6\u4e86\u3002<\/p>\n<h2 data-id=\"heading-13\">\u53c2\u8003\u6587\u6863<\/h2>\n<ul>\n<li><a href=\"https:\/\/link.juejin.cn?target=https%3A%2F%2Fwww.zhihu.com%2Fquestion%2F65436677\" target=\"_blank\" title=\"https:\/\/www.zhihu.com\/question\/65436677\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">\u6709\u4eba\u5bf9sse(server sent events)\u4e86\u89e3\u7684\u5417\uff1f<\/a><\/li>\n<li><a href=\"https:\/\/link.juejin.cn?target=https%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F591732908\" target=\"_blank\" title=\"https:\/\/zhuanlan.zhihu.com\/p\/591732908\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">MQTT\u548cWebsocket\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/a><\/li>\n<li><a href=\"https:\/\/link.juejin.cn?target=https%3A%2F%2Fdeveloper.mozilla.org%2Fzh-CN%2Fdocs%2FWeb%2FAPI%2FServer-sent_events%2FUsing_server-sent_events\" target=\"_blank\" title=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/Server-sent_events\/Using_server-sent_events\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">\u670d\u52a1\u7aefSSE<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>SSE \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5b9e\u65f6\u901a\u4fe1\u6280\u672f\uff0c\u53ea\u652f\u6301\u670d\u52a1\u7aef\u5411\u5ba2\u6237\u7aef\u5355\u5411\u63a8\u9001\u3002\u540c\u65f6\u7531\u4e8e\u6d4f\u89c8\u5668\u3001HTTP \u534f\u8bae\u7248\u672c\u7b49\u56e0\u7d20\u4f1a\u6709\u8bf8\u591a\u9650\u5236\uff0c\u5e76\u4e0d\u662f WebSocket \u7684\u66ff\u4ee3\u54c1\u3002<\/p>\n","protected":false},"author":1,"featured_media":12097,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","views":"6","footnotes":""},"categories":[3],"tags":[127,128,129,136,126],"collection":[],"class_list":["post-1922","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fenlei2","tag-ai","tag-128","tag-129","tag-136","tag-gpt"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/posts\/1922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/comments?post=1922"}],"version-history":[{"count":0,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/posts\/1922\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/media\/12097"}],"wp:attachment":[{"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/media?parent=1922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/categories?post=1922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/tags?post=1922"},{"taxonomy":"collection","embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/collection?post=1922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}