{"id":17864,"date":"2025-06-21T16:15:06","date_gmt":"2025-06-21T08:15:06","guid":{"rendered":"https:\/\/92it.top\/?p=17864"},"modified":"2025-06-21T16:15:06","modified_gmt":"2025-06-21T08:15:06","slug":"%e8%81%8a%e8%81%8a-sse%ef%bc%9a%e8%ae%a9%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%bb%e5%8a%a8%e5%bc%80%e5%8f%a3%e8%af%b4%e8%af%9d%e7%9a%84%e9%80%9a%e4%bf%a1%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17864","title":{"rendered":"\u804a\u804a SSE\uff1a\u8ba9\u670d\u52a1\u5668\u201c\u4e3b\u52a8\u5f00\u53e3\u8bf4\u8bdd\u201d\u7684\u901a\u4fe1\u65b9\u5f0f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIxNzc1OTU4OA==&amp;mid=2247488256&amp;idx=1&amp;sn=d6b9bb4eef2004b3dcc793ad20f413d3&amp;chksm=96ec624e91be5432ce3979f4aa1ee3fa75e66a426f24398c6ebae7ca72703d551aee306c30f5&amp;mpshare=1&amp;scene=1&amp;srcid=0621XeWxgWS4RZnK1XuO7EVn&amp;sharer_shareinfo=3f27e8a0ee7c2f77d5e278f9d0328145&amp;sharer_shareinfo_first=955f26a62ecd23b503a45d92fd612521&amp;exportkey=n_ChQIAhIQZ7LMQL1a040EktLvG4lthRL0AQIE97dBBAEAAAAAAKXHCJKIx%2F8AAAAOpnltbLcz9gKNyK89dVj0kjaup6JZNp3rXJe0ioGaXNKt7p3GFJRD2Pyf6ltkXqF4%2FE4Q18UgaS%2FGHQ%2BPaPSoipIikEVnVupKAwmpg2wVBtpDJTyP8wABW5239WRtP%2BZE5aogJmMLwQ%2FJQSPcVo5G6DP2R4y7QFLT1VncA7P8YzdL%2FfW3b%2BB37vsQiNs%2B4MHDYIX3QU0ayRiZKvYS9jT%2B5PJsbI%2FGTuFt5lDbgA664QcNK5g%2F5K7ExC5Tkkl%2FpLcnl7stfgyHKRmisHmUWKiFd%2BOOnNS2OlfDe%2Bb2XF8%3D&amp;acctmode=0&amp;pass_ticket=Ef34xrXVUc%2B7HU%2BpEyRL5utkpeGxICDIYLA6i0tzGMavRGKNbg%2F6QEPecewWcuIn&amp;wx_header=0#rd\">\u804a\u804a SSE\uff1a\u8ba9\u670d\u52a1\u5668\u201c\u4e3b\u52a8\u5f00\u53e3\u8bf4\u8bdd\u201d\u7684\u901a\u4fe1\u65b9\u5f0f<\/a><\/p>\n\n\n\n<p>\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7\uff0c\u4e3a\u4ec0\u4e48 ChatGPT \u80fd\u591f\u50cf\u4eba\u7c7b\u804a\u5929\u4e00\u6837\uff0c\u4e00\u4e2a\u5b57\u4e00\u4e2a\u5b57\u5730&#8221;\u8e66&#8221;\u51fa\u6765\u56de\u7b54\u4f60\u7684\u95ee\u9898\uff1f\u4e3a\u4ec0\u4e48\u80a1\u7968\u8f6f\u4ef6\u80fd\u591f\u5b9e\u65f6\u66f4\u65b0\u4ef7\u683c\uff0c\u800c\u4e0d\u9700\u8981\u4f60\u75af\u72c2\u5237\u65b0\u9875\u9762\uff1f<\/p>\n\n\n\n<p>\u7b54\u6848\u5c31\u85cf\u5728\u4eca\u5929\u6211\u4eec\u8981\u804a\u7684\u6280\u672f\u91cc\u2014\u2014<strong>SSE\uff08Server-Sent Events\uff09!<\/strong><\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e00\u3001\u4ec0\u4e48\u662f SSE\uff1f\u901a\u4fd7\u6bd4\u55bb\u4e86\u89e3\u5b83<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6211\u4eec\u7528\u4e00\u4e2a\u996d\u5e97\u7684\u6bd4\u55bb\u6765\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u8f6e\u8be2<\/strong>\uff1a\u4f60\u70b9\u4e86\u83dc\u4e4b\u540e\uff0c\u6bcf\u9694\u4e00\u4f1a\u513f\u5c31\u8dd1\u53bb\u95ee\u670d\u52a1\u5458\uff1a\u201c\u6211\u7684\u83dc\u597d\u4e86\u6ca1\uff1f\u201d<\/li>\n\n\n\n<li><strong>SSE<\/strong>\uff1a\u4f60\u70b9\u4e86\u83dc\uff0c\u5b89\u5fc3\u5750\u7740\u3002\u996d\u505a\u597d\u4e86\uff0c\u670d\u52a1\u5458\u4e3b\u52a8\u6765\u544a\u8bc9\u4f60\uff1a\u201c\u4e0a\u83dc\u5566\uff01\u201d<\/li>\n\n\n\n<li><strong>WebSocket<\/strong>\uff1a\u4f60\u548c\u670d\u52a1\u5458\u4e4b\u95f4\u88c5\u4e86\u4e2a\u5bf9\u8bb2\u673a\uff0c\u968f\u65f6\u53ef\u4ee5\u4e92\u76f8\u8bf4\u8bdd\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>SSE<\/strong>\uff08Server-Sent Events\uff09\u662f\u4e00\u79cd<strong>\u57fa\u4e8eHTTP\u7684\u5355\u5411\u901a\u4fe1\u534f\u8bae<\/strong>\uff0c\u5141\u8bb8\u670d\u52a1\u5668\u4e3b\u52a8\u5411\u6d4f\u89c8\u5668\u63a8\u9001\u6570\u636e\u3002\u5b83\u5c31\u50cf\u4e00\u6839\u4ece\u670d\u52a1\u5668\u8fde\u5230\u6d4f\u89c8\u5668\u7684\u201c\u6570\u636e\u6c34\u7ba1\u201d\uff0c\u6253\u5f00\u540e\u670d\u52a1\u5668\u53ef\u4ee5\u968f\u65f6\u901a\u8fc7\u8fd9\u6839\u7ba1\u5b50\u201c\u6d47\u6c34\u201d\uff08\u63a8\u9001\u6570\u636e\uff09\u3002<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u7279\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul>\n<li>\u2705\u00a0<strong>\u5355\u5411\u901a\u4fe1<\/strong>\uff1a\u670d\u52a1\u5668 \u2192 \u6d4f\u89c8\u5668\uff08\u50cf\u5e7f\u64ad\u7535\u53f0\uff09<\/li>\n\n\n\n<li>\u2705\u00a0<strong>\u57fa\u4e8eHTTP<\/strong>\uff1a\u65e0\u9700\u7279\u6b8a\u534f\u8bae<\/li>\n\n\n\n<li>\u2705\u00a0<strong>\u81ea\u52a8\u91cd\u8fde<\/strong>\uff1a\u7f51\u7edc\u4e2d\u65ad\u4f1a\u81ea\u52a8\u6062\u590d<\/li>\n\n\n\n<li>\u2705\u00a0<strong>\u8f7b\u91cf\u7ea7<\/strong>\uff1a\u539f\u751f\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u65e0\u9700\u989d\u5916\u5e93<\/li>\n\n\n\n<li>\u23f1\ufe0f\u00a0<strong>\u4f4e\u5ef6\u8fdf<\/strong>\uff1a\u6570\u636e\u5b9e\u65f6\u5230\u8fbe\uff08\u6beb\u79d2\u7ea7\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e8c\u3001SSE \u548c\u5176\u4ed6\u901a\u4fe1\u65b9\u5f0f\u7684\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6211\u4eec\u6765\u770b\u4e00\u5f20\u76f4\u89c2\u7684\u5bf9\u6bd4\u8868\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>\u6280\u672f<\/th><th>\u901a\u4fe1\u65b9\u5411<\/th><th>\u534f\u8bae<\/th><th>\u4f7f\u7528\u590d\u6742\u5ea6<\/th><th>\u9002\u7528\u573a\u666f<\/th><th>\u662f\u5426\u652f\u6301\u6d4f\u89c8\u5668\u539f\u751f<\/th><\/tr><\/thead><tbody><tr><td><strong>\u8f6e\u8be2\uff08HTTP Polling\uff09<\/strong><\/td><td>\u5ba2\u6237\u7aef \u279c \u670d\u52a1\u7aef<\/td><td>HTTP<\/td><td>\u7b80\u5355<\/td><td>\u6d88\u606f\u4e0d\u9891\u7e41\u7684\u5e94\u7528<\/td><td>\u2705<\/td><\/tr><tr><td><strong>\u957f\u8f6e\u8be2\uff08HTTP Long Polling\uff09<\/strong><\/td><td>\u5ba2\u6237\u7aef \u279c \u670d\u52a1\u7aef<\/td><td>HTTP<\/td><td>\u4e2d\u7b49<\/td><td>\u4f4e\u9891\u4f46\u5bf9\u5b9e\u65f6\u6027\u6709\u8981\u6c42<\/td><td>\u2705<\/td><\/tr><tr><td><strong>SSE<\/strong><\/td><td>\u670d\u52a1\u7aef \u279c \u5ba2\u6237\u7aef<\/td><td>HTTP<\/td><td>\u7b80\u5355<\/td><td>\u6d88\u606f\u6d41\u5b9e\u65f6\u63a8\u9001<\/td><td>\u2705\uff08\u9664 IE\uff09<\/td><\/tr><tr><td><strong>WebSocket<\/strong><\/td><td>\u53cc\u5411<\/td><td>\u81ea\u5b9a\u4e49\u534f\u8bae<\/td><td>\u590d\u6742<\/td><td>\u804a\u5929\u3001\u6e38\u620f\u3001IoT \u7b49<\/td><td>\u2705<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd0d&nbsp;\u573a\u666f\u9009\u62e9\u6307\u5357\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\ud83d\udea8\u00a0<strong>\u9009SSE<\/strong>\uff1a\u5f53\u53ea\u9700\u8981\u670d\u52a1\u5668\u5355\u5411\u63a8\u9001\u65f6\uff08\u5982\u65b0\u95fb\u63a8\u9001\u3001\u76d1\u63a7\u4eea\u8868\u76d8\uff09\uff0c<strong>SSE \u662f\u6700\u7b80\u5355\u7701\u4e8b\u7684\u9009\u62e9<\/strong>\u3002<\/li>\n\n\n\n<li>\ud83c\udfae\u00a0<strong>\u9009WebSocket<\/strong>\uff1a\u9700\u8981<strong>\u53cc\u5411\u5b9e\u65f6\u901a\u4fe1<\/strong>\uff08\u5982\u5728\u7ebf\u6e38\u620f\uff09\uff0c\u90a3\u5c31\u8bf7 WebSocket \u767b\u573a\u3002<\/li>\n\n\n\n<li>\u23f3\u00a0<strong>\u9009\u8f6e\u8be2\/\u957f\u8f6e\u8be2<\/strong>\uff1a\u5982\u679c\u53ea\u662f\u5076\u5c14\u6709\u6570\u636e\u53d8\u5316\uff0c\u4f7f\u7528\u8f6e\u8be2\u4e5f\u8bb8\u66f4\u7b80\u5355\u7c97\u66b4\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\ud83d\udca1 \u7cbe\u5999\u6bd4\u55bb\uff1a<br><br>    SSE \u50cf\u6536\u97f3\u673a\uff08\u53ea\u80fd\u63a5\u6536\u4fe1\u53f7\uff09<br>    WebSocket \u50cf\u7535\u8bdd\uff08\u53cc\u5411\u901a\u8bdd\uff09<br>    \u8f6e\u8be2 \u50cf\u4e0d\u65ad\u7ffb\u4fe1\u7bb1\u67e5\u4fe1<br>    \u957f\u8f6e\u8be2 \u50cf\u5b88\u5728\u4fe1\u7bb1\u65c1\u7b49\u90ae\u5dee<br>\u3000<\/pre>\n\n\n\n<p><strong>\u4e09\u3001SSE \u7684\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6211\u4eec\u7528\u4e00\u4e2a\u56fe\u6765\u7b80\u5355\u63cf\u7ed8 SSE \u7684\u5de5\u4f5c\u8fc7\u7a0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"443\" height=\"1024\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-443x1024.webp\" alt=\"\" class=\"wp-image-17866\" style=\"width:481px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-443x1024.webp 443w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-130x300.webp 130w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-768x1774.webp 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-665x1536.webp 665w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-887x2048.webp 887w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-830x1917.webp 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-230x531.webp 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-350x808.webp 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1-480x1109.webp 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/640-1.webp 930w\" sizes=\"(max-width: 443px) 100vw, 443px\" \/><\/figure><\/div>\n\n\n<p><strong>\ud83d\udce6 \u5173\u952e\u8981\u7d20\u89e3\u6790<\/strong><\/p>\n\n\n\n<ol>\n<li><strong>HTTP \u8bf7\u6c42\u5934<\/strong>\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u00a0<code>Accept: text\/event-stream<\/code>\u00a0\u544a\u8bc9\u670d\u52a1\u5668&#8221;\u6211\u8981\u63a5\u6536\u4e8b\u4ef6\u6d41&#8221;<\/li>\n\n\n\n<li><strong>\u54cd\u5e94\u683c\u5f0f<\/strong>\uff1a\u670d\u52a1\u5668\u8fd4\u56de\u00a0<code>Content-Type: text\/event-stream<\/code>\uff0c\u7136\u540e\u6301\u7eed\u53d1\u9001\u6570\u636e<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\u683c\u5f0f<\/strong>\uff1a\u6bcf\u4e2a\u4e8b\u4ef6\u4ee5\u00a0<code>data: \u5185\u5bb9\\n\\n<\/code>\u00a0\u7ed3\u675f\uff0c\u4e24\u4e2a\u6362\u884c\u7b26\u8868\u793a\u4e8b\u4ef6\u7ed3\u675f<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u91cd\u8fde<\/strong>\uff1a\u8fde\u63a5\u65ad\u5f00\u540e\uff0c\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u53d1\u8d77\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42<\/li>\n<\/ol>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u56db\u3001SSE \u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>SSE \u5df2\u7ecf\u5728\u5f88\u591a\u4ea7\u54c1\u4e2d\u843d\u5730\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5178\u578b\u573a\u666f\uff1a<\/p>\n\n\n\n<p><strong>1. \ud83e\udd16 ChatGPT \u7684\u56de\u7b54\u663e\u793a\u673a\u5236<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"147\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230-1024x147.png\" alt=\"\" class=\"wp-image-17867\" style=\"width:532px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230-1024x147.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230-300x43.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230-768x111.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230-830x119.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230-230x33.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230-350x50.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230-480x69.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-230.png 1348w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>ChatGPT \u5728\u56de\u7b54\u4f60\u95ee\u9898\u65f6\uff0c\u662f\u4e00\u53e5\u4e00\u53e5\u201c\u6253\u5b57\u5f0f\u201d\u8f93\u51fa\u7684\uff0c\u6ca1\u9519\uff01\u5c31\u662f\u7528\u4e86 SSE \u6765\u6d41\u5f0f\u4f20\u8f93\u751f\u6210\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>2. \ud83d\udea8 \u540e\u53f0\u7cfb\u7edf\u6d88\u606f\u63d0\u9192<\/strong><\/p>\n\n\n\n<p>\u6bd4\u5982\uff1a<\/p>\n\n\n\n<ul>\n<li>\u6709\u65b0\u7684\u8ba2\u5355\uff1b<\/li>\n\n\n\n<li>\u7528\u6237\u63d0\u4ea4\u4e86\u65b0\u8bc4\u8bba\uff1b<\/li>\n\n\n\n<li>\u540e\u53f0\u5de5\u5355\u66f4\u65b0\u4e86\u72b6\u6001\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u63d0\u9192\u90fd\u53ef\u4ee5\u7528 SSE \u6765<strong>\u5b9e\u65f6\u63a8\u9001<\/strong>\u3002<\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>3. \ud83d\udcc8 \u5b9e\u65f6\u6570\u636e\u9762\u677f<\/strong><\/p>\n\n\n\n<ul>\n<li>\u80a1\u7968\u4ef7\u683c\u53d8\u52a8<\/li>\n\n\n\n<li>\u533a\u5757\u94fe\u4ea4\u6613\u52a8\u6001<\/li>\n\n\n\n<li>\u8bbe\u5907\u6e29\u6e7f\u5ea6\u66f4\u65b0<\/li>\n<\/ul>\n\n\n\n<p>\u53ea\u9700\u8981\u540e\u7aef\u6bcf\u9694\u51e0\u79d2\u63a8\u9001\u4e00\u6b21\uff0c\u524d\u7aef\u5c31\u80fd\u4e0d\u65ad\u5c55\u793a\u6700\u65b0\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e94\u3001\u7b80\u5355\u4ee3\u7801\u793a\u4f8b\uff08\u524d\u7aef + \u540e\u7aef\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u524d\u7aef\u4ee3\u7801\uff08\u6d4f\u89c8\u5668 JS\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script>\n  const eventSource = new EventSource('\/sse\/stream');\n\n  eventSource.onmessage = function(event) {\n    console.log('\u6536\u5230\u6d88\u606f\uff1a', event.data);\n    \/\/ \u53ef\u66f4\u65b0\u5230\u9875\u9762\u4e0a\n  };\n\n  eventSource.onerror = function(err) {\n    console.error('\u8fde\u63a5\u51fa\u9519', err);\n    \/\/ \u53ef\u4ee5\u5c55\u793a\u8fde\u63a5\u65ad\u5f00\u7684\u63d0\u793a\n  };\n&lt;\/script>\n<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u540e\u7aef\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n\n\n\n<p><strong>\u540e\u7aef\uff08Java\uff0c\u4f7f\u7528 Spring Boot \u793a\u4f8b\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RestController\npublicclass SseController {\n    privatestaticfinal Map&lt;String, SseEmitter> emitters = new ConcurrentHashMap&lt;>();\n\n    \/\/ \u6d4f\u89c8\u5668\u8fde\u63a5\u5165\u53e3\n    @GetMapping(\"\/sse-connect\")\n    public SseEmitter connect(@RequestParam String userId) {\n        SseEmitter emitter = new SseEmitter(30 * 60 * 1000L); \/\/ 30\u5206\u949f\u8d85\u65f6\n        emitters.put(userId, emitter);\n\n        \/\/ \u53d1\u9001\u6b22\u8fce\u6d88\u606f\n        try {\n            emitter.send(SseEmitter.event()\n                    .name(\"welcome\")\n                    .data(\"\ud83c\udf89 \u8fde\u63a5\u6210\u529f\uff01\u6b22\u8fce\u4f7f\u7528 SSE \u670d\u52a1\"));\n        } catch (IOException e) {\n            System.err.println(\"\u53d1\u9001\u6b22\u8fce\u6d88\u606f\u5931\u8d25: \" + e.getMessage());\n        }\n\n        emitter.onCompletion(() -> emitters.remove(userId));\n        emitter.onTimeout(() -> emitters.remove(userId));\n\n        return emitter;\n    }\n\n    \/\/ \u6a21\u62df\u63a8\u9001\u670d\u52a1\n    @Scheduled(fixedRate = 2000)\n    public void pushData() {\n        emitters.forEach((userId, emitter) -> {\n            try {\n                String json = String.format(\n                        \"{\\\"time\\\": \\\"%s\\\", \\\"value\\\": %.2f}\",\n                        LocalTime.now(), Math.random() * 100\n                );\n\n                \/\/ \u6784\u5efa\u7b26\u5408SSE\u683c\u5f0f\u7684\u6d88\u606f\n                emitter.send(SseEmitter.event()\n                        .id(UUID.randomUUID().toString())\n                        .name(\"system-metrics\")\n                        .data(json));\n            } catch (IOException e) {\n                emitters.remove(userId);\n            }\n        });\n    }\n}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl --location 'localhost:18500\/sse-connect?userId=1' \\\n--header 'Key: Accept' \\\n--header 'Value: text\/event-stream'\n<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"830\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-1024x830.png\" alt=\"\" class=\"wp-image-17868\" style=\"width:496px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-1024x830.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-300x243.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-768x623.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-1536x1246.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-830x673.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-230x187.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-350x284.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231-480x389.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-231.png 1608w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>SSE \u5c31\u50cf\u4e00\u4e2a\u8d34\u5fc3\u7684&#8221;\u6d88\u606f\u63a8\u9001\u5458&#8221;\uff0c\u8ba9\u670d\u52a1\u5668\u80fd\u591f\u4e3b\u52a8\u628a\u6700\u65b0\u6d88\u606f\u9001\u5230\u4f60\u9762\u524d\uff0c\u800c\u4e0d\u9700\u8981\u4f60\u9891\u7e41\u5730\u53bb&#8221;\u6572\u95e8\u8be2\u95ee&#8221;\u3002\u5b83\u7b80\u5355\u6613\u7528\uff0c\u7279\u522b\u9002\u5408\u90a3\u4e9b\u9700\u8981\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u6570\u636e\u7684\u573a\u666f\u3002\ud83d\ude80<\/p>\n\n\n\n<p>\u867d\u7136 SSE \u6ca1\u6709 WebSocket \u90a3\u4e48&#8221;\u5168\u80fd&#8221;\uff08\u4e0d\u80fd\u53cc\u5411\u901a\u4fe1\uff09\uff0c\u4f46\u6b63\u662f\u8fd9\u79cd\u4e13\u4e00\u6027\u8ba9\u5b83\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u53d8\u5f97\u683c\u5916\u5b9e\u7528\u3002\u5c31\u50cf\u4e13\u95e8\u7684\u5feb\u9012\u5458\u867d\u7136\u53ea\u8d1f\u8d23\u9001\u8d27\uff0c\u4f46\u5728\u9001\u8d27\u8fd9\u4ef6\u4e8b\u4e0a\u505a\u5f97\u7279\u522b\u4e13\u4e1a\u4e00\u6837\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u9700\u8981<strong>\u5b9e\u65f6\u63a8\u9001<\/strong>\u4f46\u901a\u4fe1\u4e0d\u9700\u8981\u592a\u590d\u6742\u7684\u5e94\u7528\uff0c<strong>SSE \u662f\u4e00\u4e2a\u8f7b\u91cf\u53c8\u53ef\u9760\u7684\u9009\u62e9<\/strong>\uff0c\u7279\u522b\u9002\u5408\u73b0\u4ee3\u7f51\u9875\u3001\u540e\u53f0\u7cfb\u7edf\u3001\u6570\u636e\u5c55\u793a\u7b49\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u804a\u804a SSE\uff1a\u8ba9\u670d\u52a1\u5668\u201c\u4e3b\u52a8\u5f00\u53e3\u8bf4\u8bdd\u201d\u7684\u901a\u4fe1\u65b9\u5f0f \u4f60\u6709\u6ca1\u6709\u60f3\u8fc7\uff0c\u4e3a\u4ec0\u4e48 ChatGPT \u80fd\u591f\u50cf\u4eba\u7c7b\u804a\u5929\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17864"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17864"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17864\/revisions"}],"predecessor-version":[{"id":17869,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17864\/revisions\/17869"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}