{"id":10064,"date":"2026-06-02T16:56:02","date_gmt":"2026-06-02T08:56:02","guid":{"rendered":"https:\/\/sdeno.com\/?p=10064"},"modified":"2026-06-02T17:02:20","modified_gmt":"2026-06-02T09:02:20","slug":"sse%e9%95%bf%e8%bf%9e%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=10064","title":{"rendered":"SSE\u957f\u8fde\u63a5"},"content":{"rendered":"<p>\u5237\u65b0\u9875\u9762\u4f1a\u4e3b\u52a8\u65ad\u5f00\uff0c\u670d\u52a1\u5668\u5355\u5411\u5bf9\u5ba2\u6237\u7aef\u63a8\u9001\uff0c\u57fa\u4e8ehttp\u534f\u8bae<\/p>\n<p>\u7b80\u5355\u6848\u4f8b\u4e0d\u6d89\u53ca\u5230token<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const es = new EventSource('http:\/\/localhost:3000\/sse\/events');\r\nes.onmessage = (e) =&gt; console.log('\u6536\u5230:', e.data);\r\nes.onerror = () =&gt; console.log('\u8fde\u63a5\u51fa\u9519\uff0c\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u91cd\u8fde');\r\n\r\n\u6216\u8005\u8ddf\u540e\u7aef\u534f\u8c03token\u8fd9\u6837\u4f20\r\nconst es = new EventSource('http:\/\/localhost:3000\/sse\/events?token=xxx');<\/pre>\n<p>&nbsp;<\/p>\n<p>\u7531\u4e8e\u539f\u751f EventSource \u65e0\u6cd5\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\uff0c\u8981\u643a\u5e26 token \u5fc5\u987b\u4f7f\u7528 fetch + \u6d41\u5f0f\u8bfb\u53d6\uff08\u6216 @microsoft\/fetch-event-source \u5e93\uff09\u3002\u4e0d\u8003\u8651\u5b89\u88c5\u5e93<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const subscribeUrl = '\/api\/hx-policy\/policy\/common\/subscribe'\r\nconst controller = new AbortController()\r\n\r\nconst connect = async () =&gt; {\r\n  try {\r\n    const response = await fetch(subscribeUrl, {\r\n      headers: {\r\n                  'X-Authorization': '\u4f60\u7684token',\r\n                  'Content-type': 'application\/json;charset=UTF-8'\r\n      },\r\n      signal: controller.signal\r\n    })\r\n    if (!response.ok || !response.body) {\r\n      \/\/\u62a5\u9519\u65f6\u8bf7\u6c42\u522b\u7684\u903b\u8f91\r\n      return\r\n    }\r\n    const reader = response.body.getReader()\r\n    const decoder = new TextDecoder('utf-8')\r\n    let buffer = ''\r\n    while (true) {\r\n      const { value, done } = await reader.read()\r\n      if (done) {\r\n        break\r\n      }\r\n      buffer += decoder.decode(value, { stream: true })\r\n      const parts = buffer.split('\\n\\n')\r\n      buffer = parts.pop() || ''\r\n      parts.forEach(part =&gt; {\r\n        const dataLines = part\r\n          .split('\\n')\r\n          .filter(line =&gt; line.startsWith('data:'))\r\n          .map(line =&gt; line.slice(5).trimStart())\r\n        if (dataLines.length) {\r\n          const dataStr = dataLines.join('\\n')\r\n          try {\r\n            const data = JSON.parse(dataStr)\r\n            \/\/ data \u7ed3\u6784\u793a\u4f8b\r\n            \/\/ {\r\n            \/\/   \"pendingPolicyCount\": 19,  \/\/ \u5f85\u5ba1\r\n            \/\/   \"returnedTasksCount\": 2    \/\/ \u5448\u62a5\r\n            \/\/ }\r\n            console.log('\u8bf7\u6c42\u5417')\r\n            syncMenuCounts(data)\r\n\r\n          } catch (e) {\r\n            \/\/ ignore non-JSON payloads\r\n            console.log('\u8bf7\u6c42\u5931\u8d25')\r\n          }\r\n        }\r\n      })\r\n    }\r\n  } catch (e) {\r\n    if (!controller.signal.aborted) {\r\n      \/\/\u62a5\u9519\u65f6\u8bf7\u6c42\u522b\u7684\u903b\u8f91\r\n    }\r\n  }\r\n}\r\n\r\nconnect()<\/pre>\n<figure class=\"mdx-lazyload-container\" style=\"max-width:887px\"><div style=\"padding-top:27.621195039459%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img width=\"887\" height=\"245\" class=\"alignnone size-full wp-image-10066 lazyload\" title=\"SSE\u957f\u8fde\u63a5\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/111ScreenShot_2026-06-02_165925_001.png\" alt=\"https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/111ScreenShot_2026-06-02_165925_001.png\" data-srcset=\"https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/111ScreenShot_2026-06-02_165925_001.png 887w, https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/111ScreenShot_2026-06-02_165925_001-300x83.png 300w, https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/111ScreenShot_2026-06-02_165925_001-768x212.png 768w\" sizes=\"auto, (max-width: 887px) 100vw, 887px\"><\/figure>\n<p>\u53ea\u6709\u521d\u59cb\u5316\u8bf7\u6c42\u4e00\u6b21\u63a5\u53e3\u5c31\u53ef\u4ee5\uff0c\u5269\u4e0b\u7684\u670d\u52a1\u5668\u4f1a\u4e3b\u52a8\u63a8\u9001\u6570\u636e<\/p>\n<p>\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\u7684\u6548\u679c\uff1a<\/p>\n<figure class=\"mdx-lazyload-container\" style=\"max-width:890px\"><div style=\"padding-top:24.494382022472%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img width=\"890\" height=\"218\" class=\"alignnone size-full wp-image-10068 lazyload\" title=\"SSE\u957f\u8fde\u63a5\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/222ScreenShot_2026-06-02_170028_860.png\" alt=\"https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/222ScreenShot_2026-06-02_170028_860.png\" data-srcset=\"https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/222ScreenShot_2026-06-02_170028_860.png 890w, https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/222ScreenShot_2026-06-02_170028_860-300x73.png 300w, https:\/\/sdeno.com\/wp-content\/uploads\/2026\/06\/222ScreenShot_2026-06-02_170028_860-768x188.png 768w\" sizes=\"auto, (max-width: 890px) 100vw, 890px\"><\/figure>\n<p>\u670d\u52a1\u5668\u4e3b\u52a8\u63a8\u9001\uff0c\u63a5\u53e3\u8fd8\u662f\u521d\u59cb\u5316\u7b2c\u4e00\u6b21\u8bf7\u6c42\u7684\u63a5\u53e3<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5237\u65b0\u9875\u9762\u4f1a\u4e3b\u52a8\u65ad\u5f00\uff0c\u670d\u52a1\u5668\u5355\u5411\u5bf9\u5ba2\u6237\u7aef\u63a8\u9001\uff0c\u57fa\u4e8ehttp\u534f\u8bae \u7b80\u5355\u6848\u4f8b\u4e0d\u6d89\u53ca\u5230token const es = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10064","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/10064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10064"}],"version-history":[{"count":3,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/10064\/revisions"}],"predecessor-version":[{"id":10069,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/10064\/revisions\/10069"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}