{"id":26832,"date":"2025-04-19T17:26:13","date_gmt":"2025-04-19T09:26:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/26832.html"},"modified":"2025-04-19T17:26:13","modified_gmt":"2025-04-19T09:26:13","slug":"sse%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%bb%e5%8a%a8%e6%8e%a8%e9%80%81%e8%87%b3%e6%b5%8f%e8%a7%88%e5%99%a8%e5%ae%a2%e6%88%b7%e7%ab%af%ef%bc%8c%e8%ae%a9%e4%bd%a0%e4%b8%8d%e5%86%8d%e9%9c%80%e8%a6%81websocke","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/26832.html","title":{"rendered":"SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket"},"content":{"rendered":"<p>Server-Sent Events&#xff08;SSE&#xff09;\u662f\u4e00\u79cd\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u7684\u6280\u672f&#xff0c;\u57fa\u4e8eHTTP\u534f\u8bae\u3002\u5ba2\u6237\u7aef\u901a\u8fc7EventSource API\u6765\u63a5\u6536\u4e8b\u4ef6\u6d41&#xff0c;\u800c\u670d\u52a1\u5668\u5219\u4fdd\u6301\u4e00\u4e2a\u957f\u8fde\u63a5&#xff0c;\u6301\u7eed\u53d1\u9001\u6570\u636e\u3002\u8fd9\u4e0e\u4f20\u7edf\u7684\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\u4e0d\u540c&#xff0c;\u5141\u8bb8\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u6570\u636e&#xff0c;\u800c\u4e0d\u662f\u7b49\u5f85\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002<\/p>\n<p>\u5ba2\u6237\u7aef\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aEventSource\u5bf9\u8c61&#xff0c;\u6307\u5b9a\u670d\u52a1\u5668\u7684URL\u3002\u670d\u52a1\u5668\u8bbe\u7f6e\u7279\u5b9a\u7684HTTP\u5934&#xff08;\u5982Content-Type: text\/event-stream&#xff09;\u5e76\u4fdd\u6301\u8fde\u63a5\u6253\u5f00\u3002\u670d\u52a1\u5668\u53ef\u4ee5\u5468\u671f\u6027\u6216\u6839\u636e\u4e8b\u4ef6\u53d1\u9001\u6570\u636e&#xff0c;\u6570\u636e\u683c\u5f0f\u9075\u5faaSSE\u7684\u89c4\u8303&#xff0c;\u6bcf\u6761\u6d88\u606f\u4ee5&#034;data: &#034;\u5f00\u5934&#xff0c;\u540e\u8ddf\u6570\u636e\u5185\u5bb9&#xff0c;\u7528\u4e24\u4e2a\u6362\u884c\u7ed3\u675f\u3002<\/p>\n<p>\u7136\u540e&#xff0c;\u4f18\u7f3a\u70b9\u5206\u6790\u3002\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528&#xff0c;\u57fa\u4e8eHTTP&#xff0c;\u517c\u5bb9\u6027\u597d&#xff0c;\u9002\u5408\u4f4e\u9891\u66f4\u65b0\u3002\u7f3a\u70b9\u662f\u5355\u5411\u901a\u4fe1&#xff0c;\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef&#xff0c;\u4e0d\u652f\u6301\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e&#xff1b;\u540c\u65f6&#xff0c;HTTP\u534f\u8bae\u672c\u8eab\u4e0d\u662f\u4e3a\u957f\u8fde\u63a5\u8bbe\u8ba1\u7684&#xff0c;\u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6548\u7387\u4e0d\u5982WebSocket\u3002<\/p>\n<p>\u9002\u7528\u573a\u666f\u5305\u62ec\u5b9e\u65f6\u65b0\u95fb\u63a8\u9001\u3001\u80a1\u7968\u884c\u60c5\u66f4\u65b0\u3001\u65e5\u5fd7\u76d1\u63a7\u7b49\u4e0d\u9700\u8981\u9ad8\u9891\u53cc\u5411\u901a\u4fe1\u7684\u573a\u666f\u3002\u800c\u5982\u679c\u5e94\u7528\u9700\u8981\u9ad8\u9891\u901a\u4fe1\u6216\u53cc\u5411\u901a\u4fe1&#xff0c;WebSocket\u53ef\u80fd\u66f4\u5408\u9002\u3002<\/p>\n<p>\u6700\u540e&#xff0c;\u53ef\u80fd\u9700\u8981\u63d0\u5230\u4e00\u4e9b\u5b9e\u73b0\u7ec6\u8282&#xff0c;\u6bd4\u5982\u6570\u636e\u683c\u5f0f\u3001\u4e8b\u4ef6\u7c7b\u578b\u3001\u91cd\u8bd5\u673a\u5236\u7b49&#xff0c;\u4ee5\u53ca\u5982\u4f55\u5728\u4e0d\u540c\u540e\u7aef\u8bed\u8a00\u4e2d\u5b9e\u73b0SSE&#xff0c;\u6bd4\u5982java\u3001PHP\u3001Node.js\u7b49\u3002<\/p>\n<h3>1. \u5de5\u4f5c\u539f\u7406<\/h3>\n<ul>\n<li>\u5ba2\u6237\u7aef&#xff1a;\u901a\u8fc7 JavaScript \u7684\u00a0EventSource\u00a0API \u5efa\u7acb\u957f\u8fde\u63a5<\/li>\n<li>\u670d\u52a1\u7aef&#xff1a;\u8bbe\u7f6e\u00a0Content-Type: text\/event-stream\u00a0\u54cd\u5e94\u5934&#xff0c;\u4fdd\u6301\u8fde\u63a5\u6301\u7eed\u6253\u5f00<\/li>\n<li>\u901a\u4fe1\u65b9\u5f0f&#xff1a;\u670d\u52a1\u5668\u6309\u9700\u53d1\u9001\u6587\u672c\u6570\u636e\u6d41&#xff08;UTF-8 \u7f16\u7801&#xff09;&#xff0c;\u5ba2\u6237\u7aef\u81ea\u52a8\u89e3\u6790<\/li>\n<\/ul>\n<h3 style=\"background-color:transparent\">2. \u6838\u5fc3\u4f18\u52bf<\/h3>\n<ul>\n<li>\u8f7b\u91cf\u7ea7&#xff1a;\u57fa\u4e8e HTTP \u534f\u8bae&#xff0c;\u65e0\u9700\u590d\u6742\u63e1\u624b<\/li>\n<li>\u81ea\u52a8\u91cd\u8fde&#xff1a;\u5185\u7f6e\u65ad\u7ebf\u91cd\u8bd5\u673a\u5236<\/li>\n<li>\u517c\u5bb9\u6027\u4f73&#xff1a;\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u5747\u652f\u6301<\/li>\n<li>\u7b80\u5355\u5b9e\u73b0&#xff1a;\u670d\u52a1\u7aef\u53ea\u9700\u4fdd\u6301\u957f\u8fde\u63a5\u5e76\u53d1\u9001\u6587\u672c\u6d41<\/li>\n<\/ul>\n<h3 style=\"background-color:transparent\">3. \u5178\u578b\u5e94\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u5b9e\u65f6\u6570\u636e\u770b\u677f&#xff08;\u5982\u80a1\u7968\u884c\u60c5\u3001\u7cfb\u7edf\u76d1\u63a7&#xff09;<\/li>\n<li>\u65e5\u5fd7\u6d41\u5b9e\u65f6\u67e5\u770b<\/li>\n<li>\u901a\u77e5\u63a8\u9001\u7cfb\u7edf<\/li>\n<li>\u534f\u4f5c\u5de5\u5177\u4e2d\u7684\u5b9e\u65f6\u66f4\u65b0<\/li>\n<\/ul>\n<h3 style=\"background-color:transparent\">4. \u4e0e WebSocket \u5bf9\u6bd4<\/h3>\n<table>\n<tbody>\n<tr>\u7279\u6027SSEWebSocket<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td>\u901a\u4fe1\u65b9\u5411<\/td>\n<td>\u5355\u5411&#xff08;\u670d\u52a1\u5668\u2192\u5ba2\u6237\u7aef&#xff09;<\/td>\n<td>\u53cc\u5411<\/td>\n<\/tr>\n<tr>\n<td>\u534f\u8bae<\/td>\n<td>HTTP<\/td>\n<td>\u72ec\u7acb\u534f\u8bae<\/td>\n<\/tr>\n<tr>\n<td>\u8fde\u63a5\u6570<\/td>\n<td>\u5929\u7136\u652f\u6301\u591a\u6807\u7b7e\u9875\u5171\u4eab<\/td>\n<td>\u9700\u81ea\u884c\u5904\u7406\u591a\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u683c\u5f0f<\/td>\n<td>\u6587\u672c\u6d41<\/td>\n<td>\u4e8c\u8fdb\u5236\/\u6587\u672c<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u4f4e\u9891\u66f4\u65b0<\/td>\n<td>\u9ad8\u9891\/\u53cc\u5411\u901a\u4fe1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>5. \u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<ul>\n<li>\u9891\u7387\u63a7\u5236&#xff1a;\u5efa\u8bae\u81f3\u5c11\u95f4\u9694 1 \u79d2\u53d1\u9001\u6570\u636e&#xff0c;\u907f\u514d\u9ad8\u9891\u89e6\u53d1\u91cd\u7ed8<\/li>\n<li>\u6570\u636e\u538b\u7f29&#xff1a;\u4f7f\u7528\u00a0gzip\u00a0\u6216\u00a0deflate\u00a0\u538b\u7f29\u4f20\u8f93\u5185\u5bb9<\/li>\n<li>\u9519\u8bef\u5904\u7406&#xff1a;\u76d1\u542c\u00a0onerror\u00a0\u4e8b\u4ef6\u5e76\u5b9e\u73b0\u81ea\u52a8\u91cd\u8fde\u903b\u8f91<\/li>\n<li>\u6027\u80fd\u4f18\u5316&#xff1a;\u7ed3\u5408\u00a0Last-Event-ID\u00a0\u5b9e\u73b0\u65ad\u70b9\u7eed\u4f20<\/li>\n<li>\u5b89\u5168\u9000\u51fa&#xff1a;\u7ed3\u675f\u65f6\u53d1\u9001\u7a7a\u6d88\u606f\u6216\u7279\u6b8a\u6807\u8bc6&#xff08;\u5982\u00a0data: END&#xff09;<\/li>\n<\/ul>\n<h3>6. \u4ee3\u7801\u793a\u4f8b<\/h3>\n<h4>\u670d\u52a1\u7aef\u6e90\u7801<\/h4>\n<p>const express &#061; require(&#039;express&#039;);<br \/>\nconst app &#061; express();<br \/>\nconst port &#061; 3000;<\/p>\n<p>\/\/ \u9759\u6001\u6587\u4ef6\u670d\u52a1<br \/>\napp.use(express.static(&#039;public&#039;));<\/p>\n<p>\/\/ SSE\u8def\u7531<br \/>\napp.get(&#039;\/events&#039;, (req, res) &#061;&gt; {<br \/>\n  \/\/ \u8bbe\u7f6eSSE\u6240\u9700\u7684\u5934\u4fe1\u606f<br \/>\n  res.writeHead(200, {<br \/>\n    &#039;Content-Type&#039;: &#039;text\/event-stream&#039;,<br \/>\n    &#039;Cache-Control&#039;: &#039;no-cache&#039;,<br \/>\n    &#039;Connection&#039;: &#039;keep-alive&#039;<br \/>\n  });<\/p>\n<p>  \/\/ \u53d1\u9001\u521d\u59cb\u6d88\u606f<br \/>\n  res.write(&#039;data: Connected to SSE server\\\\n\\\\n&#039;);<\/p>\n<p>  \/\/ \u5b9a\u65f6\u53d1\u9001\u6d88\u606f<br \/>\n  let counter &#061; 0;<br \/>\n  const intervalId &#061; setInterval(() &#061;&gt; {<br \/>\n    counter&#043;&#043;;<br \/>\n    const data &#061; {<br \/>\n      message: &#096;Server time: ${new Date().toISOString()}&#096;,<br \/>\n      counter: counter<br \/>\n    };<br \/>\n    res.write(&#096;data: ${JSON.stringify(data)}\\\\n\\\\n&#096;);<\/p>\n<p>    \/\/ \u6a21\u62df\u7ed3\u675f\u6761\u4ef6<br \/>\n    if (counter &gt;&#061; 10) {<br \/>\n      res.write(&#039;event: end\\\\ndata: Stream ended\\\\n\\\\n&#039;);<br \/>\n      clearInterval(intervalId);<br \/>\n      res.end();<br \/>\n    }<br \/>\n  }, 1000);<\/p>\n<p>  \/\/ \u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u65f6\u6e05\u7406<br \/>\n  req.on(&#039;close&#039;, () &#061;&gt; {<br \/>\n    console.log(&#039;Client disconnected&#039;);<br \/>\n    clearInterval(intervalId);<br \/>\n    res.end();<br \/>\n  });<br \/>\n});<\/p>\n<p>app.listen(port, () &#061;&gt; {<br \/>\n  console.log(&#096;SSE server running at http:\/\/localhost:${port}&#096;);<br \/>\n}); <\/p>\n<h4>\u5ba2\u6237\u7aefH5\u9875\u9762\u7aef<\/h4>\n<p>&lt;!DOCTYPE html&gt;<br \/>\n&lt;html lang&#061;&#034;en&#034;&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;meta charset&#061;&#034;UTF-8&#034;&gt;<br \/>\n    &lt;meta name&#061;&#034;viewport&#034; content&#061;&#034;width&#061;device-width, initial-scale&#061;1.0&#034;&gt;<br \/>\n    &lt;title&gt;SSE Demo&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n    &lt;h1&gt;Server-Sent Events Demo&lt;\/h1&gt;<br \/>\n    &lt;div id&#061;&#034;events&#034;&gt;&lt;\/div&gt;<\/p>\n<p>    &lt;script&gt;<br \/>\n        const eventSource &#061; new EventSource(&#039;\/events&#039;);<br \/>\n        const eventsDiv &#061; document.getElementById(&#039;events&#039;);<\/p>\n<p>        \/\/ \u76d1\u542c\u9ed8\u8ba4\u6d88\u606f\u4e8b\u4ef6<br \/>\n        eventSource.onmessage &#061; function(e) {<br \/>\n            const data &#061; JSON.parse(e.data);<br \/>\n            const p &#061; document.createElement(&#039;p&#039;);<br \/>\n            p.textContent &#061; &#096;${data.message} (Count: ${data.counter})&#096;;<br \/>\n            eventsDiv.appendChild(p);<br \/>\n        };<\/p>\n<p>        \/\/ \u76d1\u542c\u81ea\u5b9a\u4e49\u4e8b\u4ef6<br \/>\n        eventSource.addEventListener(&#039;end&#039;, function(e) {<br \/>\n            const p &#061; document.createElement(&#039;p&#039;);<br \/>\n            p.textContent &#061; &#096;Event: ${e.type}, Data: ${e.data}&#096;;<br \/>\n            eventsDiv.appendChild(p);<br \/>\n            eventSource.close();<br \/>\n            console.log(&#039;EventSource closed&#039;);<br \/>\n        });<\/p>\n<p>        \/\/ \u9519\u8bef\u5904\u7406<br \/>\n        eventSource.onerror &#061; function(e) {<br \/>\n            console.error(&#039;EventSource failed:&#039;, e);<br \/>\n            eventSource.close();<br \/>\n        };<br \/>\n    &lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt; <\/p>\n<h3>7. \u6ce8\u610f\u4e8b\u9879<\/h3>\n<li>\n<p>\u8fde\u63a5\u7ba1\u7406&#xff1a;\u786e\u4fdd\u5728\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u65f6\u6e05\u7406\u5b9a\u65f6\u5668\u548c\u8d44\u6e90<\/p>\n<\/li>\n<li>\n<p>\u91cd\u8fde\u673a\u5236&#xff1a;SSE\u5ba2\u6237\u7aef\u4f1a\u81ea\u52a8\u5c1d\u8bd5\u91cd\u65b0\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>CORS&#xff1a;\u5982\u679c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e0d\u540c\u6e90&#xff0c;\u9700\u8981\u8bbe\u7f6eCORS\u5934<\/p>\n<\/li>\n<li>\n<p>\u6027\u80fd\u8003\u8651&#xff1a;\u5bf9\u4e8e\u5927\u91cf\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u8003\u8651\u4f7f\u7528\u4e13\u95e8\u7684\u89e3\u51b3\u65b9\u6848\u5982Redis PUB\/SUB<\/p>\n<\/li>\n<li>\n<p>\u6d4f\u89c8\u5668\u652f\u6301&#xff1a;\u5927\u591a\u6570\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u652f\u6301SSE&#xff0c;\u4f46\u4e0d\u5305\u62ecIE<\/p>\n<\/li>\n<h3><\/h3>\n<p>SSE \u9002\u7528\u4e8e\u5927\u591a\u6570\u5b9e\u65f6\u6027\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f&#xff0c;\u5982\u5b9e\u65f6\u901a\u77e5\u3001\u80a1\u7968\u884c\u60c5\u3001\u65b0\u95fb\u63a8\u9001\u7b49\u3002\u5176\u7b80\u6d01\u6027\u548c\u517c\u5bb9\u6027\u4f7f\u5176\u6210\u4e3a\u8f7b\u91cf\u7ea7\u5b9e\u65f6\u901a\u4fe1\u7684\u9996\u9009\u65b9\u6848\u3002\u5bf9\u4e8e\u9700\u8981\u53cc\u5411\u901a\u4fe1\u6216\u9ad8\u9891\u66f4\u65b0\u7684\u573a\u666f&#xff0c;\u5efa\u8bae\u8003\u8651 WebSocket \u6280\u672f\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb809\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf14\u6b21\u3002Server-Sent Events\uff08SSE\uff09\u662f\u4e00\u79cd\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u7684\u6280\u672f\uff0c\u57fa\u4e8eHTTP\u534f\u8bae\u3002\u5ba2\u6237\u7aef\u901a\u8fc7EventSource API\u6765\u63a5\u6536\u4e8b\u4ef6\u6d41\uff0c\u800c\u670d\u52a1\u5668\u5219\u4fdd\u6301\u4e00\u4e2a\u957f\u8fde\u63a5\uff0c\u6301\u7eed\u53d1\u9001\u6570\u636e\u3002\u8fd9\u4e0e\u4f20\u7edf\u7684\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\u4e0d\u540c\uff0c\u5141\u8bb8\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u6570\u636e\uff0c\u800c\u4e0d\u662f\u7b49\u5f85\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002\u5ba2\u6237\u7aef\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aEventSource\u5bf9\u8c61\uff0c\u6307\u5b9a\u670d\u52a1\u5668\u7684URL\u3002\u670d\u52a1\u5668\u8bbe\u7f6e\u7279\u5b9a\u7684HTTP\u5934\uff08\u5982Content-Type: text\/event-stream\uff09\u5e76\u4fdd\u6301\u8fde\u63a5\u6253\u5f00\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[947,43,44],"topic":[],"class_list":["post-26832","post","type-post","status-publish","format-standard","hentry","category-server","tag-websocket","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket - \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\/26832.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb809\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf14\u6b21\u3002Server-Sent Events\uff08SSE\uff09\u662f\u4e00\u79cd\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u7684\u6280\u672f\uff0c\u57fa\u4e8eHTTP\u534f\u8bae\u3002\u5ba2\u6237\u7aef\u901a\u8fc7EventSource API\u6765\u63a5\u6536\u4e8b\u4ef6\u6d41\uff0c\u800c\u670d\u52a1\u5668\u5219\u4fdd\u6301\u4e00\u4e2a\u957f\u8fde\u63a5\uff0c\u6301\u7eed\u53d1\u9001\u6570\u636e\u3002\u8fd9\u4e0e\u4f20\u7edf\u7684\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\u4e0d\u540c\uff0c\u5141\u8bb8\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u6570\u636e\uff0c\u800c\u4e0d\u662f\u7b49\u5f85\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002\u5ba2\u6237\u7aef\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aEventSource\u5bf9\u8c61\uff0c\u6307\u5b9a\u670d\u52a1\u5668\u7684URL\u3002\u670d\u52a1\u5668\u8bbe\u7f6e\u7279\u5b9a\u7684HTTP\u5934\uff08\u5982Content-Type: text\/event-stream\uff09\u5e76\u4fdd\u6301\u8fde\u63a5\u6253\u5f00\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/26832.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-19T09:26:13+00:00\" \/>\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=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/26832.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/26832.html\",\"name\":\"SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-19T09:26:13+00:00\",\"dateModified\":\"2025-04-19T09:26:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/26832.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/26832.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/26832.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket\"}]},{\"@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":"SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket - \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\/26832.html","og_locale":"zh_CN","og_type":"article","og_title":"SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb809\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf14\u6b21\u3002Server-Sent Events\uff08SSE\uff09\u662f\u4e00\u79cd\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u7684\u6280\u672f\uff0c\u57fa\u4e8eHTTP\u534f\u8bae\u3002\u5ba2\u6237\u7aef\u901a\u8fc7EventSource API\u6765\u63a5\u6536\u4e8b\u4ef6\u6d41\uff0c\u800c\u670d\u52a1\u5668\u5219\u4fdd\u6301\u4e00\u4e2a\u957f\u8fde\u63a5\uff0c\u6301\u7eed\u53d1\u9001\u6570\u636e\u3002\u8fd9\u4e0e\u4f20\u7edf\u7684\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\u4e0d\u540c\uff0c\u5141\u8bb8\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u6570\u636e\uff0c\u800c\u4e0d\u662f\u7b49\u5f85\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002\u5ba2\u6237\u7aef\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aEventSource\u5bf9\u8c61\uff0c\u6307\u5b9a\u670d\u52a1\u5668\u7684URL\u3002\u670d\u52a1\u5668\u8bbe\u7f6e\u7279\u5b9a\u7684HTTP\u5934\uff08\u5982Content-Type: text\/event-stream\uff09\u5e76\u4fdd\u6301\u8fde\u63a5\u6253\u5f00\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/26832.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-19T09:26:13+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/26832.html","url":"https:\/\/www.wsisp.com\/helps\/26832.html","name":"SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-19T09:26:13+00:00","dateModified":"2025-04-19T09:26:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/26832.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/26832.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/26832.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"SSE\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u81f3\u6d4f\u89c8\u5668\u5ba2\u6237\u7aef\uff0c\u8ba9\u4f60\u4e0d\u518d\u9700\u8981websocket"}]},{"@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\/26832","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=26832"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/26832\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=26832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=26832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=26832"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=26832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}