{"id":911,"date":"2024-02-21T09:54:51","date_gmt":"2024-02-21T01:54:51","guid":{"rendered":"http:\/\/gjweb.top\/?p=911"},"modified":"2024-03-01T15:44:18","modified_gmt":"2024-03-01T07:44:18","slug":"sse%e6%9c%8d%e5%8a%a1%e7%ab%af%e6%8e%a8%e9%80%81%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=911","title":{"rendered":"SSE\u670d\u52a1\u7aef\u63a8\u9001\u6280\u672f"},"content":{"rendered":"\n<p>\u767e\u5ea6\u4e0a\u6709\u5f88\u591a\u5173\u4e8e SSE \u7684\u6982\u5ff5\u63cf\u8ff0\uff0c\u8be6\u7ec6\u4e86\u89e3\u53ef\u4ee5\u81ea\u884c\u67e5\u9605\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u603b\u7ed3 SSE\u662f\u57fa\u4e8e HTTP \u534f\u8bae\u7684\u670d\u52a1\u7aef\u63a8\u9001\u6280\u672f\uff0c\u5176\u4e2d\u5e76\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684 HTTP \u8bf7\u6c42 \/ \u54cd\u5e94\u6a21\u578b\uff0cSSE \u53ef\u4fdd\u6301\u4e00\u6b21\u8bf7\u6c42\u957f\u4e45\u8fde\u63a5\uff0c\u4f46\u4e5f\u4e0d\u662f WebSocket \u53cc\u5de5\u901a\u4fe1\u6a21\u578b\uff1bSSE \u53ea\u662f\u652f\u6301\u670d\u52a1\u7aef\u4e3b\u52a8\u63a8\u9001\u5230\u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u7406\u89e3\u6210 \u5355\u9879\u957f\u8fde\u63a5\uff1b<\/p>\n\n\n\n<p>\u5e94\u7528\u573a\u666f\uff1aGPT \u95ee\u7b54\u540e\u4f1a\u9010\u6b65\u7ed9\u51fa\u56de\u590d\uff0c\u4e0b\u9762\u4f1a\u901a\u8fc7 node + react \u7b80\u5355\u6a21\u62df\u4e00\u4e0b\uff0c\u5148\u770b\u6548\u679c\uff1b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/gjweb.top\/wp-content\/uploads\/2024\/02\/Video_2024-02-21_093540-00_00_00-00_00_30.gif'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"928\" height=\"480\" data-original=\"http:\/\/gjweb.top\/wp-content\/uploads\/2024\/02\/Video_2024-02-21_093540-00_00_00-00_00_30.gif\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-912\"\/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">SSE\u7279\u70b9\u603b\u7ed3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u57fa\u4e8e HTTP \u534f\u8bae<\/li>\n\n\n\n<li>\u5355\u5de5\u957f\u8fde\u63a5<\/li>\n\n\n\n<li>\u5185\u7f6e\u65ad\u7ebf\u91cd\u8fde\u673a\u5236<\/li>\n\n\n\n<li>\u8fde\u63a5\u6570\u91cf HTTP1.1 \/ 6\u4e2a HTTP2 \/ \u53ef\u534f\u5546\uff08\u9ed8\u8ba4100\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ee3\u7801\u5b9e\u8df5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">node\u7aef\u57fa\u4e8eKOA2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const router = require('koa-router')()\nconst { PassThrough } = require(\"stream\");\n\nrouter.get('\/', async (ctx, next) =&gt; {  \n  \/**\u914d\u7f6e HTTP \u76f8\u5173\u8bf7\u6c42\u65b9\u5f0f *\/\n  ctx.request.socket.setTimeout(0);  \n  ctx.req.socket.setNoDelay(true);  \n  ctx.req.socket.setKeepAlive(true); \n  \/**\u8bbe\u7f6e SSE \u8bf7\u6c42\u5934 *\/\n  ctx.set({  \n    \"Content-Type\": \"text\/event-stream\",  \n    \"Cache-Control\": \"no-cache\",  \n    \"Connection\": \"keep-alive\",  \n  });  \n\n  const stream = new PassThrough();  \n\n  ctx.status = 200;  \n  ctx.body = stream;  \n\n  const content = \"\u7b80\u5355\u603b\u7ed3 SSE\u662f\u57fa\u4e8e HTTP \u534f\u8bae\u7684\u670d\u52a1\u7aef\u63a8\u9001\u6280\u672f\uff0c\u5176\u4e2d\u5e76\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684 HTTP \u8bf7\u6c42 \/ \u54cd\u5e94\u6a21\u578b\uff0cSSE \u53ef\u4fdd\u6301\u4e00\u6b21\u8bf7\u6c42\u957f\u4e45\u8fde\u63a5\uff0c\u4f46\u4e5f\u4e0d\u662f WebSockets \u53cc\u5de5\u901a\u4fe1\u6a21\u578b\uff1bSSE \u53ea\u662f\u652f\u6301\u670d\u52a1\u7aef\u4e3b\u52a8\u63a8\u9001\u5230\u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u7406\u89e3\u6210 \u5355\u9879\u957f\u8fde\u63a5\uff1b\";  \n\n  \/\/ \u5c06\u5185\u5bb9\u5206\u5272\u6210\u591a\u4e2a\u4e8b\u4ef6\u6d88\u606f  \n  const messages = content.split(''); \/\/ \u6bcf\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u4e00\u4e2a\u6d88\u606f  \n\n  const sendMessage = () =&gt; {  \n    if (messages.length &gt; 0) {  \n      const message = messages.shift(); \/\/ \u83b7\u53d6\u5e76\u79fb\u9664\u7b2c\u4e00\u4e2a\u6d88\u606f  \n      stream.write(`data: ${message}\\n\\n`); \/\/ \u53d1\u9001\u6d88\u606f\uff0c\u5305\u62ec\u6362\u884c\u7b26  \n      setTimeout(sendMessage, 100); \/\/ \u5ef6\u8fdf\u540e\u53d1\u9001\u4e0b\u4e00\u4e2a\u6d88\u606f  \n    } else {  \n      stream.end(); \/\/ \u6ca1\u6709\u66f4\u591a\u6d88\u606f\u65f6\u7ed3\u675f\u6d41  \n    }  \n  };  \n\n  sendMessage(); \/\/ \u5f00\u59cb\u53d1\u9001\u6d88\u606f  \n\n  stream.on(\"close\", () =&gt; {  \n    console.log(\"\u5df2\u5173\u95ed\");  \n  });  \n});<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d<\/p>\n\n\n\n<p><code>ctx.request.socket.setTimeout(0);<\/code> \u8bbe\u7f6e\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4 \uff0c0 \u8868\u793a\u4e0d\u4f1a\u8d85\u65f6<\/p>\n\n\n\n<p><code>ctx.req.socket.setNoDelay(true);<\/code> \u7981\u7528 Nagle \u7b97\u6cd5\uff0cTCP \u9ed8\u8ba4\u4f1a\u5408\u5e76\u5c0f\u7684\u6570\u636e\u5305\uff0c\u4ee5\u51cf\u5c11\u7a7a\u95f4\u4e2d\u6570\u636e\u5305\u7684\u6570\u91cf\uff0c\u4f46SSE\u7684\u7279\u70b9\u5c31\u662f\u53ea\u8981\u6709\u6570\u636e\u4f1a\u8981\u5feb\u901f\u8fd4\u56de\uff0c\u56e0\u6b64\u7981\u7528\u6b64\u7b97\u6cd5\uff1b<\/p>\n\n\n\n<p><code>ctx.req.socket.setKeepAlive(true);<\/code> \u542f\u7528 TCP \u7684 keep-alive \u673a\u5236\u3002\u5f53 TCP \u8fde\u63a5\u7684\u4e00\u65b9\u68c0\u6d4b\u5230\u957f\u65f6\u95f4\u6ca1\u6709\u6d3b\u52a8\u65f6\uff0c\u53ef\u4ee5\u53d1\u9001\u4e00\u4e2a keep-alive \u63a2\u6d4b\u5305\u7ed9\u5bf9\u65b9\uff0c\u4ee5\u786e\u8ba4\u8fde\u63a5\u662f\u5426\u4ecd\u7136\u6709\u6548;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5ba2\u6237\u7aef\u57fa\u4e8ereact<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useState } from \"react\"\nimport styled, { keyframes } from \"styled-components\"\n\nconst blinkAnimation = keyframes`\n  0% {\n    opacity: 1;\n  }\n  100% {\n    opacity: 0;\n  }\n`;\nconst CursorContent = styled.span`\n  display: inline-block;\n  line-height: 20px;\n  width: 2px;\n  height: 20px;\n  background: #000;\n  animation: ${blinkAnimation} 0.5s linear infinite;\n`\nconst TextContainer = styled.div`\n\n`\n\nconst Home:React.FC&lt;any&gt; = ()=&gt;{\n  const handleClick = ()=&gt;{\n    const eventSource = new EventSource('\/api\/') \/*\u6b64\u5904\u9700\u8981\u914d\u7f6ereact\u53cd\u5411\u4ee3\u7406*\/\n\n    \/\/ \u76d1\u542c\u6d88\u606f\u4e8b\u4ef6\n    eventSource.onmessage  = (event:any) =&gt; {\n      \/\/ const result = JSON.parse(event)\n      console.log(event);\n      setContent((state)=&gt;{\n        return state+event.data\n      })\n    }\n\n    eventSource.onopen = () =&gt; {\n      console.log(\"SSE \u8fde\u63a5\u6210\u529f\uff0c\u72b6\u6001${eventSource.readyState}&lt;br \/&gt;\")\n    }\n\n    eventSource.onerror = () =&gt; {\n      console.log(\"SSE \u8fde\u63a5\u9519\u8bef\uff0c\u72b6\u6001${eventSource.readyState}&lt;br \/&gt;\")\n      eventSource.close();\n    }\n  }\n  const &#91;content,setContent] = useState(\"\")\n\n  return (\n    &lt;div&gt;\n      &lt;button onClick={()=&gt;handleClick()}&gt;\u4ec0\u4e48\u662fSSE&lt;\/button&gt;\n      &lt;TextContainer&gt;\n        {content}\n        &lt;CursorContent&gt;&lt;\/CursorContent&gt;\n      &lt;\/TextContainer&gt;\n    &lt;\/div&gt;\n  )\n}\n\nexport default Home<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u767e\u5ea6\u4e0a\u6709\u5f88\u591a\u5173\u4e8e SSE \u7684\u6982\u5ff5\u63cf\u8ff0\uff0c\u8be6\u7ec6\u4e86\u89e3\u53ef\u4ee5\u81ea\u884c\u67e5\u9605\u3002 \u7b80\u5355\u603b\u7ed3 SSE\u662f\u57fa\u4e8e HTTP \u534f\u8bae\u7684\u670d\u52a1\u7aef\u63a8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-911","post","type-post","status-publish","format-standard","hentry","category-http"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=911"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/911\/revisions"}],"predecessor-version":[{"id":913,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/911\/revisions\/913"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}