{"id":23286,"date":"2025-04-19T07:21:19","date_gmt":"2025-04-18T23:21:19","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/23286.html"},"modified":"2025-04-19T07:21:19","modified_gmt":"2025-04-18T23:21:19","slug":"%e9%80%9a%e8%bf%87%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%8e%a8%e9%80%81%e6%b6%88%e6%81%afwebsocket%e5%92%8csse%e8%a7%a3%e9%87%8a","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/23286.html","title":{"rendered":"\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u770bLLM\u7684\u6d41\u5f0f\u8f93\u51fa\u5982\u4f55\u53cd\u6620\u5230json\u8f93\u51fa\u4e0a,\u73b0\u6709\u7684\u89e3\u51b3\u65b9\u6848\u5305\u62ec WebSocket \u6216 Server-Sent Events (SSE) \u5b9e\u65f6\u901a\u4fe1\u6280\u672f.<\/p>\n<h3>WebSocket<\/h3>\n<p>WebSocket API \u53ef\u5728\u7528\u6237\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5f00\u542f\u53cc\u5411\u4ea4\u4e92\u5f0f\u901a\u4fe1\u4f1a\u8bdd\u3002\u5229\u7528\u8be5 API&#xff0c;\u53ef\u4ee5\u5411\u670d\u52a1\u5668\u53d1\u9001\u4fe1\u606f&#xff0c;\u5e76\u63a5\u6536\u4e8b\u4ef6\u9a71\u52a8\u7684\u54cd\u5e94&#xff0c;\u800c\u65e0\u9700\u8f6e\u8be2\u670d\u52a1\u5668\u4ee5\u83b7\u5f97\u56de\u590d\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418232117-6802de6de1a2e.png\" alt=\"img\" \/><\/p>\n<p>&#xff08;1&#xff09;\u5efa\u7acb\u5728 TCP \u534f\u8bae\u4e4b\u4e0a&#xff0c;\u670d\u52a1\u5668\u7aef\u7684\u5b9e\u73b0\u6bd4\u8f83\u5bb9\u6613\u3002<\/p>\n<p>&#xff08;2&#xff09;\u4e0e HTTP \u534f\u8bae\u6709\u7740\u826f\u597d\u7684\u517c\u5bb9\u6027\u3002\u9ed8\u8ba4\u7aef\u53e3\u4e5f\u662f80\u548c443&#xff0c;\u5e76\u4e14\u63e1\u624b\u9636\u6bb5\u91c7\u7528 HTTP \u534f\u8bae&#xff0c;\u56e0\u6b64\u63e1\u624b\u65f6\u4e0d\u5bb9\u6613\u5c4f\u853d&#xff0c;\u80fd\u901a\u8fc7\u5404\u79cd HTTP \u4ee3\u7406\u670d\u52a1\u5668\u3002<\/p>\n<p>&#xff08;3&#xff09;\u6570\u636e\u683c\u5f0f\u6bd4\u8f83\u8f7b\u91cf&#xff0c;\u6027\u80fd\u5f00\u9500\u5c0f&#xff0c;\u901a\u4fe1\u9ad8\u6548\u3002<\/p>\n<p>&#xff08;4&#xff09;\u53ef\u4ee5\u53d1\u9001\u6587\u672c&#xff0c;\u4e5f\u53ef\u4ee5\u53d1\u9001\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<p>&#xff08;5&#xff09;\u6ca1\u6709\u540c\u6e90\u9650\u5236&#xff0c;\u5ba2\u6237\u7aef\u53ef\u4ee5\u4e0e\u4efb\u610f\u670d\u52a1\u5668\u901a\u4fe1\u3002<\/p>\n<p>&#xff08;6&#xff09;\u534f\u8bae\u6807\u8bc6\u7b26\u662fws&#xff08;\u5982\u679c\u52a0\u5bc6&#xff0c;\u5219\u4e3awss&#xff09;&#xff0c;\u670d\u52a1\u5668\u7f51\u5740\u5c31\u662f URL\u3002<\/p>\n<p>\u5ba2\u6237\u7aef<\/p>\n<p>let webSocket &#061; new WebSocket(url, protocols);<br \/>\nws.onopen &#061; function(evt) {<br \/>\n  console.log(&#034;Connection open &#8230;&#034;);<br \/>\n  ws.send(&#034;Hello WebSockets!&#034;);<br \/>\n};<\/p>\n<p>ws.onmessage &#061; function(evt) {<br \/>\n  console.log( &#034;Received Message: &#034; &#043; evt.data);<br \/>\n  ws.close();<br \/>\n};<\/p>\n<p>ws.onclose &#061; function(evt) {<br \/>\n  console.log(&#034;Connection closed.&#034;);<br \/>\n};    <\/p>\n<p>\u53d1\u9001JSON<\/p>\n<p><span class=\"token comment\">\/\/ \u670d\u52a1\u5668\u5411\u6240\u6709\u7528\u6237\u53d1\u9001\u6587\u672c<\/span><br \/>\n<span class=\"token keyword\">function<\/span> <span class=\"token function\">sendText<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ \u6784\u9020\u4e00\u4e2a msg \u5bf9\u8c61&#xff0c;\u5305\u542b\u4e86\u670d\u52a1\u5668\u5904\u7406\u6240\u9700\u7684\u6570\u636e<\/span><br \/>\n  <span class=\"token keyword\">var<\/span> msg <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token literal-property property\">type<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;message&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token literal-property property\">text<\/span><span class=\"token operator\">:<\/span> document<span class=\"token punctuation\">.<\/span><span class=\"token function\">getElementById<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;text&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>value<span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token literal-property property\">id<\/span><span class=\"token operator\">:<\/span> clientID<span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token literal-property property\">date<\/span><span class=\"token operator\">:<\/span> Date<span class=\"token punctuation\">.<\/span><span class=\"token function\">now<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u628a msg \u5bf9\u8c61\u4f5c\u4e3a JSON \u683c\u5f0f\u5b57\u7b26\u4e32\u53d1\u9001<\/span><br \/>\n  exampleSocket<span class=\"token punctuation\">.<\/span><span class=\"token function\">send<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">JSON<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">stringify<\/span><span class=\"token punctuation\">(<\/span>msg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u6e05\u7a7a\u6587\u672c\u8f93\u5165\u5143\u7d20&#xff0c;\u4e3a\u63a5\u6536\u4e0b\u4e00\u6761\u6d88\u606f\u505a\u597d\u51c6\u5907\u3002<\/span><br \/>\n  document<span class=\"token punctuation\">.<\/span><span class=\"token function\">getElementById<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;text&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>value <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u670d\u52a1\u7aef<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528Socket.IO,<\/p>\n<p><span class=\"token keyword\">const<\/span> <span class=\"token punctuation\">{<\/span> Server <span class=\"token punctuation\">}<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;socket.io&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> io <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Server<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* options *\/<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>io<span class=\"token punctuation\">.<\/span><span class=\"token function\">on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;connection&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token parameter\">socket<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ &#8230;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>io<span class=\"token punctuation\">.<\/span><span class=\"token function\">listen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> <span class=\"token punctuation\">{<\/span> readFileSync <span class=\"token punctuation\">}<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;fs&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token punctuation\">{<\/span> createServer <span class=\"token punctuation\">}<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;https&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token punctuation\">{<\/span> Server <span class=\"token punctuation\">}<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;socket.io&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> httpsServer <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">createServer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token literal-property property\">key<\/span><span class=\"token operator\">:<\/span> <span class=\"token function\">readFileSync<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/path\/to\/my\/key.pem&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token literal-property property\">cert<\/span><span class=\"token operator\">:<\/span> <span class=\"token function\">readFileSync<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/path\/to\/my\/cert.pem&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> io <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Server<\/span><span class=\"token punctuation\">(<\/span>httpsServer<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* options *\/<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>io<span class=\"token punctuation\">.<\/span><span class=\"token function\">on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;connection&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token parameter\">socket<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ &#8230;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>httpsServer<span class=\"token punctuation\">.<\/span><span class=\"token function\">listen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>SSE<\/h3>\n<p>HTTP \u534f\u8bae\u672c\u8eab\u4e0d\u5141\u8bb8\u670d\u52a1\u5668\u4e3b\u52a8\u7ed9\u5ba2\u6237\u7aef\u53d1\u9001\u4fe1\u606f&#xff0c;\u4f46\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u670d\u52a1\u5668\u6301\u7eed\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u6d41\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u662f\u53d1\u9001\u4e00\u6b21\u6027\u6570\u636e\u5305&#xff0c;\u800c\u662f\u4fdd\u6301\u8fde\u63a5\u5f00\u653e&#xff0c;\u50cf\u89c6\u9891\u64ad\u653e\u90a3\u6837\u8fde\u7eed\u53d1\u9001\u6570\u636e\u3002\u8fd9\u79cd\u957f\u65f6\u95f4\u7684\u4e0b\u8f7d\u8fc7\u7a0b\u5b9e\u9645\u4e0a\u662f\u4ee5\u6570\u636e\u6d41\u7684\u5f62\u5f0f\u8fdb\u884c\u7684\u3002<\/p>\n<p>\u670d\u52a1\u5668\u53d1\u9001\u4e8b\u4ef6&#xff08;SSE&#xff09;\u5c31\u662f\u5229\u7528\u4e86\u8fd9\u4e2a\u7279\u6027&#xff0c;\u901a\u8fc7HTTP\u534f\u8bae\u8ba9\u670d\u52a1\u5668\u53ef\u4ee5\u5411\u6d4f\u89c8\u5668\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u7684\u4fe1\u606f\u3002\u4f20\u7edf\u4e0a&#xff0c;\u7f51\u9875\u9700\u8981\u5148\u8bf7\u6c42\u670d\u52a1\u5668\u624d\u80fd\u83b7\u53d6\u65b0\u6570\u636e&#xff0c;\u4f46\u662f\u4f7f\u7528SSE&#xff0c;\u670d\u52a1\u5668\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u4e3b\u52a8\u5411\u7f51\u9875\u63a8\u9001\u65b0\u7684\u6570\u636e\u548c\u6d88\u606f&#xff0c;\u8fd9\u4e9b\u6d88\u606f\u53ef\u4ee5\u5728\u7f51\u9875\u5185\u4f5c\u4e3a\u4e8b\u4ef6\u6765\u5904\u7406\u3002<\/p>\n<p>\u670d\u52a1\u7aef\u63a8\u9001\u7684\u6570\u636e\u662f\u5355\u5411\u7684&#xff0c;\u53ea\u4ece\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef\u6d41\u52a8\u3002\u5f53\u4e0d\u9700\u8981\u4ece\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u9001\u4fe1\u606f\u65f6&#xff0c;\u6bd4\u5982\u66f4\u65b0\u793e\u4ea4\u5a92\u4f53\u72b6\u6001\u3001\u65b0\u95fb\u63a8\u9001\u6216\u628a\u6570\u636e\u4f20\u9001\u5230\u5ba2\u6237\u7aef\u5b58\u50a8&#xff08;\u5982IndexedDB\u6216Web Storage&#xff09;&#xff0c;SSE\u5c31\u975e\u5e38\u9002\u5408\u3002<\/p>\n<p>\u4e0eSSE\u4e0d\u540c\u7684\u662f&#xff0c;WebSocket\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u5f3a\u5927\u7684\u53cc\u5411\u901a\u4fe1\u901a\u9053&#xff0c;\u5141\u8bb8\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4e92\u76f8\u53d1\u9001\u4fe1\u606f\u3002SSE\u5219\u662f\u5355\u5411\u7684&#xff0c;\u4e3b\u8981\u7528\u4e8e\u670d\u52a1\u5668\u5411\u6d4f\u89c8\u5668\u53d1\u9001\u4fe1\u606f\u3002\u5982\u679c\u6d4f\u89c8\u5668\u9700\u8981\u5411\u670d\u52a1\u5668\u53d1\u9001\u4fe1\u606f&#xff0c;\u5b83\u5fc5\u987b\u53d1\u8d77\u4e00\u4e2a\u65b0\u7684HTTP\u8bf7\u6c42\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u51e0\u4e2a\u5173\u4e8eSSE\u548cWebSocket\u7684\u533a\u522b&#xff1a;<\/p>\n<ul>\n<li>SSE\u57fa\u4e8eHTTP&#xff0c;\u56e0\u6b64\u6240\u6709\u73b0\u6709\u7684\u670d\u52a1\u5668\u8f6f\u4ef6\u90fd\u80fd\u652f\u6301\u5b83&#xff1b;\u800cWebSocket\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u534f\u8bae\u3002<\/li>\n<li>SSE\u8bbe\u7f6e\u8d77\u6765\u7b80\u5355\u5f97\u591a&#xff0c;\u9002\u5408\u8f7b\u91cf\u7ea7\u5e94\u7528&#xff1b;WebSocket\u5219\u66f4\u4e3a\u590d\u6742\u3002<\/li>\n<li>SSE\u81ea\u5e26\u65ad\u7ebf\u91cd\u8fde\u529f\u80fd&#xff0c;\u800cWebSocket\u9700\u8981\u5f00\u53d1\u8005\u81ea\u5df1\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/li>\n<li>SSE\u4e3b\u8981\u7528\u4e8e\u4f20\u8f93\u6587\u672c\u6570\u636e&#xff0c;\u82e5\u8981\u53d1\u9001\u4e8c\u8fdb\u5236\u6570\u636e\u5219\u9700\u7f16\u7801&#xff1b;WebSocket\u76f4\u63a5\u652f\u6301\u4e8c\u8fdb\u5236\u6570\u636e\u4f20\u8f93\u3002<\/li>\n<li>SSE\u5141\u8bb8\u81ea\u5b9a\u4e49\u6d88\u606f\u7c7b\u578b&#xff0c;\u589e\u52a0\u4e86\u7075\u6d3b\u6027\u3002<\/li>\n<\/ul>\n<h4>\u4e8b\u4ef6\u6d41\u683c\u5f0f<\/h4>\n<p>\u4e8b\u4ef6\u6d41\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u6570\u636e\u6d41&#xff0c;\u6587\u672c\u5e94\u8be5\u4f7f\u7528UTF-8\u683c\u5f0f\u7684\u7f16\u7801\u3002\u4e8b\u4ef6\u6d41\u4e2d\u7684\u6d88\u606f\u7531\u4e00\u5bf9\u6362\u884c\u7b26\u5206\u5f00\u3002\u4ee5\u5192\u53f7\u5f00\u5934\u7684\u884c\u4e3a\u6ce8\u91ca\u884c&#xff0c;\u4f1a\u88ab\u5ffd\u7565\u3002<\/p>\n<p>\u5907\u6ce8&#xff1a; \u6ce8\u91ca\u884c\u53ef\u4ee5\u7528\u6765\u9632\u6b62\u8fde\u63a5\u8d85\u65f6&#xff0c;\u670d\u52a1\u5668\u53ef\u4ee5\u5b9a\u671f\u53d1\u9001\u4e00\u6761\u6d88\u606f\u6ce8\u91ca\u884c&#xff0c;\u4ee5\u4fdd\u6301\u8fde\u63a5\u4e0d\u65ad\u3002<\/p>\n<p>\u6bcf\u6761\u6d88\u606f\u7531\u4e00\u884c\u6216\u591a\u884c\u6587\u5b57\u7ec4\u6210&#xff0c;\u5217\u51fa\u8be5\u6d88\u606f\u7684\u5b57\u6bb5\u3002\u6bcf\u4e2a\u5b57\u6bb5\u7531\u5b57\u6bb5\u540d\u8868\u793a&#xff0c;\u540e\u9762\u662f\u5192\u53f7&#xff0c;\u7136\u540e\u662f\u8be5\u5b57\u6bb5\u503c\u7684\u6587\u672c\u6570\u636e\u3002<\/p>\n<p>\u89c4\u8303\u4e2d\u89c4\u5b9a\u4e86\u4e0b\u9762\u8fd9\u4e9b\u5b57\u6bb5&#xff1a;<\/p>\n<ul>\n<li>\n<p>event<\/p>\n<p>\u4e00\u4e2a\u7528\u4e8e\u6807\u8bc6\u4e8b\u4ef6\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u6307\u5b9a\u4e86\u8fd9\u4e2a\u5b57\u7b26\u4e32&#xff0c;\u6d4f\u89c8\u5668\u4f1a\u5c06\u5177\u6709\u6307\u5b9a\u4e8b\u4ef6\u540d\u79f0\u7684\u4e8b\u4ef6\u5206\u6d3e\u7ed9\u76f8\u5e94\u7684\u76d1\u542c\u5668&#xff1b;\u7f51\u7ad9\u6e90\u4ee3\u7801\u5e94\u8be5\u4f7f\u7528 addEventListener() \u6765\u76d1\u542c\u6307\u5b9a\u7684\u4e8b\u4ef6\u3002\u5982\u679c\u4e00\u4e2a\u6d88\u606f\u6ca1\u6709\u6307\u5b9a\u4e8b\u4ef6\u540d\u79f0&#xff0c;\u90a3\u4e48 onmessage \u5904\u7406\u7a0b\u5e8f\u5c31\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>data<\/p>\n<p>\u6d88\u606f\u7684\u6570\u636e\u5b57\u6bb5\u3002\u5f53 EventSource \u63a5\u6536\u5230\u591a\u4e2a\u4ee5 data: \u5f00\u5934\u7684\u8fde\u7eed\u884c\u65f6&#xff0c;\u4f1a\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765&#xff0c;\u5728\u5b83\u4eec\u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u6362\u884c\u7b26\u3002\u672b\u5c3e\u7684\u6362\u884c\u7b26\u4f1a\u88ab\u5220\u9664\u3002<\/p>\n<\/li>\n<li>\n<p>id<\/p>\n<p>\u4e8b\u4ef6 ID&#xff0c;\u4f1a\u6210\u4e3a\u5f53\u524d EventSource \u5bf9\u8c61\u7684\u5185\u90e8\u5c5e\u6027\u201c\u6700\u540e\u4e00\u4e2a\u4e8b\u4ef6 ID\u201d\u7684\u5c5e\u6027\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>retry<\/p>\n<p>\u91cd\u65b0\u8fde\u63a5\u7684\u65f6\u95f4\u3002\u5982\u679c\u4e0e\u670d\u52a1\u5668\u7684\u8fde\u63a5\u4e22\u5931&#xff0c;\u6d4f\u89c8\u5668\u5c06\u7b49\u5f85\u6307\u5b9a\u7684\u65f6\u95f4&#xff0c;\u7136\u540e\u5c1d\u8bd5\u91cd\u65b0\u8fde\u63a5\u3002\u8fd9\u5fc5\u987b\u662f\u4e00\u4e2a\u6574\u6570&#xff0c;\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u6307\u5b9a\u91cd\u65b0\u8fde\u63a5\u7684\u65f6\u95f4\u3002\u5982\u679c\u6307\u5b9a\u4e86\u4e00\u4e2a\u975e\u6574\u6570\u503c&#xff0c;\u8be5\u5b57\u6bb5\u5c06\u88ab\u5ffd\u7565\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6240\u6709\u5176\u4ed6\u7684\u5b57\u6bb5\u540d\u90fd\u4f1a\u88ab\u5ffd\u7565<\/p>\n<h4>\u4e8b\u4ef6\u6d41\u5177\u4f53\u4f8b\u5b50<\/h4>\n<p>Content-Type: text\/event-stream<br \/>\nCache-Control: no-cache<br \/>\nConnection: keep-alive<\/p>\n<p>\u6bcf\u4e00\u6b21\u53d1\u9001\u7684\u4fe1\u606f&#xff0c;\u7531\u82e5\u5e72\u4e2amessage\u7ec4\u6210&#xff0c;\u6bcf\u4e2amessage\u4e4b\u95f4\u7528\\\\n\\\\n\u5206\u9694\u3002\u6bcf\u4e2amessage\u5185\u90e8\u7531\u82e5\u5e72\u884c\u7ec4\u6210&#xff0c;\u6bcf\u4e00\u884c\u90fd\u662f\u5982\u4e0b\u683c\u5f0f\u3002<\/p>\n<p>[field]: value\\\\n<\/p>\n<p>: comment<br \/>\nid: 2025\\\\n<br \/>\nevent: foo\\\\n<br \/>\nretry: 100\\\\n \\\\\\\\\u6307\u5b9a\u6d4f\u89c8\u5668\u91cd\u65b0\u53d1\u8d77\u8fde\u63a5\u7684\u65f6\u95f4\u95f4\u9694\u3002<br \/>\ndata: This is the mesage\\\\n<br \/>\ndata: test\\\\n<br \/>\ndata: this is the end\\\\n\\\\n<\/p>\n<p>\u5728\u6d4f\u89c8\u5668\u4e0alastEventId\u5c5e\u6027\u8bfb\u53d6\u8fd9\u4e2a\u503c\u3002\u4e00\u65e6\u8fde\u63a5\u65ad\u7ebf&#xff0c;\u6d4f\u89c8\u5668\u4f1a\u53d1\u9001\u4e00\u4e2a HTTP \u5934&#xff0c;\u91cc\u9762\u5305\u542b\u4e00\u4e2a\u7279\u6b8a\u7684Last-Event-ID\u5934\u4fe1\u606f&#xff0c;\u5c06\u8fd9\u4e2a\u503c\u53d1\u9001\u56de\u6765&#xff0c;\u7528\u6765\u5e2e\u52a9\u670d\u52a1\u5668\u7aef\u91cd\u5efa\u8fde\u63a5\u3002\u56e0\u6b64&#xff0c;\u8fd9\u4e2a\u5934\u4fe1\u606f\u53ef\u4ee5\u88ab\u89c6\u4e3a\u4e00\u79cd\u540c\u6b65\u673a\u5236\u3002<\/p>\n<h5>\u547d\u540d\u4e8b\u4ef6<\/h5>\n<p>event: userconnect<br \/>\ndata: {&#034;username&#034;: &#034;bobby&#034;, &#034;time&#034;: &#034;02:33:48&#034;}<\/p>\n<p>event: usermessage<br \/>\ndata: {&#034;username&#034;: &#034;bobby&#034;, &#034;time&#034;: &#034;02:34:11&#034;, &#034;text&#034;: &#034;Hi everyone.&#034;}<\/p>\n<p>event: userdisconnect<br \/>\ndata: {&#034;username&#034;: &#034;bobby&#034;, &#034;time&#034;: &#034;02:34:23&#034;}<\/p>\n<p>event: usermessage<br \/>\ndata: {&#034;username&#034;: &#034;sean&#034;, &#034;time&#034;: &#034;02:34:36&#034;, &#034;text&#034;: &#034;Bye, bobby.&#034;}<\/p>\n<p>\u6bcf\u4e2a\u4e8b\u4ef6\u90fd\u6709\u4e00\u4e2a\u7531 event \u5b57\u6bb5\u6307\u5b9a\u7684\u4e8b\u4ef6\u540d\u79f0\u548c\u4e00\u4e2a data \u5b57\u6bb5&#xff0c;\u5176\u503c\u662f\u4e00\u4e2a\u9002\u5f53\u7684 JSON \u5b57\u7b26\u4e32&#xff0c;\u5305\u542b\u5ba2\u6237\u7aef\u5bf9\u8be5\u4e8b\u4ef6\u91c7\u53d6\u884c\u52a8\u6240\u9700\u7684\u6570\u636e\u3002data \u5b57\u6bb5\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u5b57\u7b26\u4e32\u6570\u636e&#xff0c;\u5b83\u4e0d\u4e00\u5b9a\u662f JSON\u3002<\/p>\n<h5>\u6df7\u5408\u4e24\u79cd\u4e8b\u4ef6<\/h5>\n<p>\u53ef\u4ee5\u5728\u4e00\u4e2a\u4e8b\u4ef6\u6d41\u4e2d\u540c\u65f6\u4f7f\u7528\u547d\u540d\u4e8b\u4ef6\u548c\u672a\u547d\u540d\u4e8b\u4ef6\u3002<\/p>\n<p>event: userconnect<br \/>\ndata: {&#034;username&#034;: &#034;bobby&#034;, &#034;time&#034;: &#034;02:33:48&#034;}<\/p>\n<p>data: Here&#039;s a system message of some kind that will get used<br \/>\ndata: to accomplish some task.<\/p>\n<p>event: usermessage<br \/>\ndata: {&#034;username&#034;: &#034;bobby&#034;, &#034;time&#034;: &#034;02:34:11&#034;, &#034;text&#034;: &#034;Hi everyone.&#034;}<\/p>\n<p>\u5ba2\u6237\u7aef<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u5982\u679c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u8fde\u63a5\u5173\u95ed&#xff0c;\u5219\u8fde\u63a5\u5c06\u91cd\u65b0\u542f\u52a8\u3002\u53ef\u4ee5\u4f7f\u7528 .close() \u65b9\u6cd5\u7ec8\u6b62\u8fde\u63a5\u3002<\/p>\n<p><span class=\"token keyword\">const<\/span> evtSource <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">EventSource<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;xxx&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/url\u53ef\u4ee5\u4e0e\u5f53\u524d\u7f51\u5740\u540c\u57df&#xff0c;\u4e5f\u53ef\u4ee5\u8de8\u57df\u3002<\/span><br \/>\nevtSource<span class=\"token punctuation\">.<\/span><span class=\"token function-variable function\">onmessage<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">function<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">event<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    xxx<br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\nevtSource<span class=\"token punctuation\">.<\/span><span class=\"token function-variable function\">onerror<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token parameter\">err<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  console<span class=\"token punctuation\">.<\/span><span class=\"token function\">error<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;EventSource failed:&#034;<\/span><span class=\"token punctuation\">,<\/span> err<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u81ea\u5b9a\u4e49\u4e8b\u4ef6<\/span><br \/>\nsource<span class=\"token punctuation\">.<\/span><span class=\"token function\">addEventListener<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;foo&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">function<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token parameter\">event<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">var<\/span> data <span class=\"token operator\">&#061;<\/span> event<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token comment\">\/\/ handle message<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u670d\u52a1\u7aef<\/p>\n<p><span class=\"token function\">date_default_timezone_set<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string double-quoted-string\">&#034;America\/New_York&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">header<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string double-quoted-string\">&#034;Cache-Control: no-store&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">header<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string double-quoted-string\">&#034;Content-Type: text\/event-stream&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token variable\">$counter<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">rand<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token constant boolean\">true<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ Every second, send a &#034;ping&#034; event.<\/span><br \/>\n  <span class=\"token keyword\">echo<\/span> <span class=\"token string double-quoted-string\">&#034;event: ping\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\"># \u58f0\u660e\u4e8b\u4ef6<\/span><br \/>\n  <span class=\"token variable\">$curDate<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">date<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">DATE_ISO8601<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token keyword\">echo<\/span> <span class=\"token string single-quoted-string\">&#039;data: {&#034;time&#034;: &#034;&#039;<\/span> <span class=\"token operator\">.<\/span> <span class=\"token variable\">$curDate<\/span> <span class=\"token operator\">.<\/span> <span class=\"token string single-quoted-string\">&#039;&#034;}&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token keyword\">echo<\/span> <span class=\"token string double-quoted-string\">&#034;\\\\n\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\"># \u4e00\u4e2a\u4e8b\u4ef6\u7ed3\u675f<\/span><br \/>\n  <span class=\"token comment\">\/\/ Send a simple message at random intervals.<\/span><br \/>\n  <span class=\"token variable\">$counter<\/span><span class=\"token operator\">&#8212;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span><span class=\"token variable\">$counter<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string single-quoted-string\">&#039;data: This is a message at time &#039;<\/span> <span class=\"token operator\">.<\/span> <span class=\"token variable\">$curDate<\/span> <span class=\"token operator\">.<\/span> <span class=\"token string double-quoted-string\">&#034;\\\\n\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\"># \u9ed8\u8ba4onmessage\u4e8b\u4ef6\u5904\u7406<\/span><br \/>\n    <span class=\"token variable\">$counter<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">rand<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token function\">ob_end_flush<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token function\">flush<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token comment\">\/\/ Break the loop if the client aborted the connection (closed the page)<\/span><br \/>\n  <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">connection_aborted<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u76f8\u5173\u8d44\u6599<\/h3>\n<li>Using server-sent events &#8211; Web APIs | MDN<\/li>\n<li>WebSocket &#8211; Web APIs | MDN<\/li>\n<li>WebSocket \u6559\u7a0b &#8211; \u962e\u4e00\u5cf0\u7684\u7f51\u7edc\u65e5\u5fd7<\/li>\n<li>Server-Sent Events \u6559\u7a0b &#8211; \u962e\u4e00\u5cf0\u7684\u7f51\u7edc\u65e5\u5fd7<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf22\u6b21\u3002\u6700\u8fd1\u5728\u770bLLM\u7684\u6d41\u5f0f\u8f93\u51fa\u5982\u4f55\u53cd\u6620\u5230json\u8f93\u51fa\u4e0a,\u73b0\u6709\u7684\u89e3\u51b3\u65b9\u6848\u5305\u62ec WebSocket \u6216 Server-Sent Events (SSE) \u5b9e\u65f6\u901a\u4fe1\u6280\u672f._sse\u6496\u5be1\u60c5<\/p>\n","protected":false},"author":2,"featured_media":23285,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[947,43],"topic":[],"class_list":["post-23286","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-websocket","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/23286.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf22\u6b21\u3002\u6700\u8fd1\u5728\u770bLLM\u7684\u6d41\u5f0f\u8f93\u51fa\u5982\u4f55\u53cd\u6620\u5230json\u8f93\u51fa\u4e0a,\u73b0\u6709\u7684\u89e3\u51b3\u65b9\u6848\u5305\u62ec WebSocket \u6216 Server-Sent Events (SSE) \u5b9e\u65f6\u901a\u4fe1\u6280\u672f._sse\u6496\u5be1\u60c5\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/23286.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T23:21:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418232117-6802de6de1a2e.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/23286.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/23286.html\",\"name\":\"\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T23:21:19+00:00\",\"dateModified\":\"2025-04-18T23:21:19+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/23286.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/23286.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/23286.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/23286.html","og_locale":"zh_CN","og_type":"article","og_title":"\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf22\u6b21\u3002\u6700\u8fd1\u5728\u770bLLM\u7684\u6d41\u5f0f\u8f93\u51fa\u5982\u4f55\u53cd\u6620\u5230json\u8f93\u51fa\u4e0a,\u73b0\u6709\u7684\u89e3\u51b3\u65b9\u6848\u5305\u62ec WebSocket \u6216 Server-Sent Events (SSE) \u5b9e\u65f6\u901a\u4fe1\u6280\u672f._sse\u6496\u5be1\u60c5","og_url":"https:\/\/www.wsisp.com\/helps\/23286.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T23:21:19+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418232117-6802de6de1a2e.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/23286.html","url":"https:\/\/www.wsisp.com\/helps\/23286.html","name":"\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T23:21:19+00:00","dateModified":"2025-04-18T23:21:19+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/23286.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/23286.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/23286.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u901a\u8fc7\u670d\u52a1\u5668\u63a8\u9001\u6d88\u606f:Websocket\u548cSSE\u89e3\u91ca"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/23286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=23286"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/23286\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/23285"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=23286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=23286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=23286"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=23286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}