{"id":15617,"date":"2024-06-16T22:03:54","date_gmt":"2024-06-16T14:03:54","guid":{"rendered":"https:\/\/92it.top\/?p=15617"},"modified":"2024-06-16T22:03:54","modified_gmt":"2024-06-16T14:03:54","slug":"%e6%b6%88%e6%81%af%e6%8e%a8%e9%80%81%e6%8a%80%e6%9c%af%ef%bc%8c%e9%99%a4%e4%ba%86websocket%e8%bf%98%e7%9f%a5%e9%81%93%e9%82%a3%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=15617","title":{"rendered":"\u6d88\u606f\u63a8\u9001\u6280\u672f\uff0c\u9664\u4e86websocket\u8fd8\u77e5\u9053\u90a3\u4e9b\uff1f"},"content":{"rendered":"\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u674e\u5c0f\u840c\u548c\u738b\u5927\u9524\u662f\u597d\u670b\u53cb\u3002\u4ed6\u4eec\u90fd\u4ece\u4e8b\u7a0b\u5e8f\u5f00\u53d1\u5de5\u4f5c\u3002\u4e24\u4eba\u6709\u7a7a\u7ecf\u5e38\u4f1a\u5728\u4e00\u8d77\u8ba8\u8bba\u6280\u672f\u3002\u5c0f\u840c\u6700\u8fd1\u6b63\u5728\u505a\u4e00\u4e2a\u7ba1\u7406\u7cfb\u7edf\uff0c\u9996\u9875\u8981\u6c42\u7528\u67f1\u72b6\u56fe\u548c\u6298\u7ebf\u56fe\u5b9e\u65f6\u5c55\u793a\u83dc\u54c1\u4fe1\u606f\u53d8\u5316\u3002\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/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=\"674\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98-1024x674.png\" alt=\"\" class=\"wp-image-15618\" style=\"width:488px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98-1024x674.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98-300x197.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98-768x505.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98-830x546.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98-230x151.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98-350x230.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98-480x316.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-98.png 1274w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u63a8\u9001\u6280\u672f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5c0f\u840c\u9996\u5148\u60f3\u5230\u7684\u662f\u4f7f\u7528websocket\u6280\u672f\u5b9e\u73b0\uff0c\u4f46\u4ee5\u524d\u6ca1\u6709\u8fd9\u5757\u5f00\u53d1\u7ecf\u5386\uff0c\u4e8e\u662f\u548c\u5927\u9524\u5c55\u5f00\u8ba8\u8bba\u3002<\/p>\n\n\n\n<p>WebSocket\u662f\u4e00\u79cd\u5728\u5355\u4e2aTCP\u8fde\u63a5\u4e0a\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u534f\u8bae\u3002WebSocket\u901a\u4fe1\u534f\u8bae\u4e8e2011\u5e74\u88abIETF\u5b9a\u4e3a\u6807\u51c6\u3002<\/p>\n\n\n\n<p><strong>\u6280\u672f\u56de\u987e<\/strong><\/p>\n\n\n\n<p>\u5927\u9524\u662f\u540d\u7ecf\u9a8c\u4e30\u5bcc\u7684\u8001\u7a0b\u5e8f\u5458\uff0c\u4ed6\u634b\u634b\u80e1\u5b50\u8bf4\uff1a\u6d88\u606f\u63a8\u9001\u5728\u5f88\u4e45\u5f88\u4e45\u4ee5\u524d\u624b\u673a\u90ae\u7bb1APP\u5f00\u53d1\u4e2d\u505a\u8fc7\u3002\u7528\u6237\u6536\u5230\u65b0\u90ae\u4ef6\u65f6\uff0c\u90ae\u7bb1\u670d\u52a1\u5668\u63a8\u9001\u90ae\u4ef6\u6d88\u606f\uff0c\u901a\u77e5\u7528\u6237\u67e5\u770b\u3002<\/p>\n\n\n\n<p>\u5f53\u65f6\u6d88\u606f\u63a8\u9001\u6280\u672f\u53d1\u5c55\u7f13\u6162\uff0c\u7528\u7684\u662f\u5ba2\u6237\u7aef\u8f6e\u5faa\u6280\u672f\uff0c\u6bcf\u9694\u51e0\u79d2\u8c03\u7528\u670d\u52a1\u7aef\u63a5\u53e3\u83b7\u53d6\u90ae\u4ef6\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7b80\u5355\uff0c\u4f46\u6548\u7387\u4f4e\u4e0b\uff0c\u6d88\u606f\u63a5\u6536\u4e0d\u53ca\u65f6\u3002<\/p>\n\n\n\n<p>\u540e\u6765\u91c7\u7528\u77ed\u4fe1\u901a\u77e5\u65b9\u5f0f\u3002\u5f53\u7528\u6237\u6709\u65b0\u90ae\u4ef6\u65f6\uff0c\u90ae\u7bb1\u670d\u52a1\u5668\u5411\u7528\u6237\u53d1\u9001\u4e00\u6761\u77ed\u4fe1\uff0c\u7528\u6237\u624b\u673a\u90ae\u7bb1app\u6536\u5230\u540e\uff0c\u8c03\u7528\u90ae\u4ef6\u63a5\u53e3\u83b7\u53d6\u90ae\u4ef6\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u77ed\u4fe1\u901a\u77e5\u89e3\u51b3\u4e86\u8f6e\u5faa\u6548\u7387\u4f4e\u4e0b\uff0c\u63a5\u6536\u6d88\u606f\u4e0d\u53ca\u65f6\u95ee\u9898\u3002\u4f46\u5b83\u4ecd\u7136\u662f\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u7aef\u53d1\u8d77\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p>websocket\u57282011\u5e74\u6210\u4e3a\u6807\u51c6\u540e\uff0c\u53d1\u5c55\u8fc5\u901f\uff0c\u73b0\u5982\u4eca\u5f88\u591a\u5e94\u7528\u90fd\u5728\u4f7f\u7528\u8fd9\u95e8\u6280\u672f\u3002\u5927\u9524\u5efa\u8bae\u5c0f\u840c\u5c1d\u8bd5websocket\u3002<\/p>\n\n\n\n<p><strong>websocket<\/strong><\/p>\n\n\n\n<p>WebSocket\u662f\u4e00\u79cd\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\uff0c\u5b83\u63d0\u4f9b\u4e86\u5728\u5355\u4e2aTCP\u8fde\u63a5\u4e0a\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u80fd\u529b\u3002\u8fd9\u610f\u5473\u7740\u6570\u636e\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u53cc\u5411\u6d41\u52a8\uff0c\u800c\u65e0\u9700\u5ba2\u6237\u7aef\u901a\u8fc7\u8f6e\u8be2\u6216\u91cd\u590d\u8bf7\u6c42\u6765\u83b7\u53d6\u66f4\u65b0\u3002<\/p>\n\n\n\n<p>WebSocket\u7684\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u5b9e\u65f6\u6e38\u620f<\/strong>\uff1aWebSocket\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u5728\u7ebf\u591a\u4eba\u6e38\u620f\u7684\u5b9e\u65f6\u4ea4\u4e92\u3002<\/li>\n\n\n\n<li><strong>\u804a\u5929\u5e94\u7528<\/strong>\uff1a\u5373\u65f6\u901a\u8baf\u548c\u804a\u5929\u5ba4\u53ef\u4ee5\u901a\u8fc7WebSocket\u5b9e\u73b0\u5b9e\u65f6\u6d88\u606f\u4f20\u9012\u3002<\/li>\n\n\n\n<li><strong>\u80a1\u7968\u884c\u60c5<\/strong>\uff1a\u5b9e\u65f6\u80a1\u7968\u4ea4\u6613\u5e73\u53f0\u53ef\u4ee5\u5229\u7528WebSocket\u63a8\u9001\u6700\u65b0\u7684\u5e02\u573a\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u534f\u4f5c\u5de5\u5177<\/strong>\uff1a\u5728\u7ebf\u6587\u6863\u7f16\u8f91\u6216\u5b9e\u65f6\u7ed8\u56fe\u5de5\u5177\u7b49\u534f\u4f5c\u5e73\u53f0\u53ef\u4ee5\u4f7f\u7528WebSocket\u6765\u540c\u6b65\u7528\u6237\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>WebSocket\u6280\u672f\u5b9e\u73b0\u804a\u5929\u5e94\u7528:<\/p>\n\n\n\n<p>\u5ba2\u6237\u7aef\u5b9e\u73b0<\/p>\n\n\n\n<ol>\n<li><strong>\u521b\u5efaWebSocket\u8fde\u63a5<\/strong>\uff1a\u4f7f\u7528<code>new WebSocket(url)<\/code>\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684WebSocket\u5bf9\u8c61\uff0c\u5176\u4e2d<code>url<\/code>\u662fWebSocket\u670d\u52a1\u5668\u7684\u5730\u5740\u3002<\/li>\n\n\n\n<li><strong>\u8bbe\u7f6e\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f<\/strong>\uff1a\u4e3aWebSocket\u5bf9\u8c61\u8bbe\u7f6e\u5404\u79cd\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5982<code>onopen<\/code>\u3001<code>onmessage<\/code>\u3001<code>onerror<\/code>\u548c<code>onclose<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u53d1\u9001\u6d88\u606f<\/strong>\uff1a\u5f53WebSocket\u8fde\u63a5\u6210\u529f\u5efa\u7acb\u540e\uff08\u5373<code>onopen<\/code>\u4e8b\u4ef6\u89e6\u53d1\u65f6\uff09\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>send<\/code>\u65b9\u6cd5\u53d1\u9001\u6d88\u606f\u3002<\/li>\n\n\n\n<li><strong>\u63a5\u6536\u6d88\u606f<\/strong>\uff1a\u5f53\u670d\u52a1\u5668\u53d1\u9001\u6d88\u606f\u65f6\uff08\u5373<code>onmessage<\/code>\u4e8b\u4ef6\u89e6\u53d1\u65f6\uff09\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u63a5\u6536\u6d88\u606f\u3002<\/li>\n\n\n\n<li><strong>\u5173\u95ed\u8fde\u63a5<\/strong>\uff1a\u5f53\u4e0d\u518d\u9700\u8981WebSocket\u8fde\u63a5\u65f6\uff0c\u53ef\u4ee5\u8c03\u7528<code>close<\/code>\u65b9\u6cd5\u5173\u95ed\u8fde\u63a5\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u793a\u4f8b\u4ee3\u7801\uff08HTML + JavaScript\uff09\uff1a<\/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=\"\"> \n\n&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;title>WebSocket Chat Example&lt;\/title>\n    &lt;script>\n        \/\/ \u5f53\u6587\u6863\u52a0\u8f7d\u5b8c\u6bd5\u65f6\u6267\u884c\n        document.addEventListener('DOMContentLoaded', function() {\n            var ws;\n            var chatBox = document.getElementById('chatBox');\n            var messageInput = document.getElementById('messageInput');\n            var sendButton = document.getElementById('sendButton');\n\n            \/\/ \u5c1d\u8bd5\u8fde\u63a5\u5230WebSocket\u670d\u52a1\u5668\n            function connect() {\n                ws = new WebSocket(\"ws:\/\/localhost:8080\");\n                ws.onopen = function() {\n                    console.log('WebSocket \u8fde\u63a5\u6210\u529f');\n                };\n                ws.onmessage = function(event) {\n                    console.log('\u6536\u5230\u6d88\u606f\uff1a', event.data);\n                    chatBox.textContent += event.data + '\\n';\n                };\n                ws.onerror = function(error) {\n                    console.error('WebSocket \u51fa\u73b0\u9519\u8bef\uff1a', error);\n                };\n                ws.onclose = function() {\n                    console.log('WebSocket \u8fde\u63a5\u5173\u95ed');\n                };\n            }\n\n            \/\/ \u53d1\u9001\u6d88\u606f\n            function sendMessage() {\n                if (ws.readyState === WebSocket.OPEN) {\n                    ws.send(messageInput.value);\n                    messageInput.value = '';\n                } else {\n                    console.error('WebSocket \u8fde\u63a5\u672a\u5efa\u7acb');\n                }\n            }\n\n            \/\/ \u7ed1\u5b9a\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\n            sendButton.onclick = function() {\n                sendMessage();\n            };\n\n            \/\/ \u81ea\u52a8\u8fde\u63a5WebSocket\n            connect();\n        });\n    &lt;\/script>\n&lt;\/head>\n&lt;body>\n    &lt;h1>WebSocket Chat&lt;\/h1>\n    &lt;pre id=\"chatBox\">&lt;\/pre>\n    &lt;input type=\"text\" id=\"messageInput\">\n    &lt;button id=\"sendButton\">\u53d1\u9001&lt;\/button>\n&lt;\/body>\n&lt;\/html>\n\n\n<\/pre>\n\n\n\n<p>\u670d\u52a1\u5668\u7aef\u5b9e\u73b0<\/p>\n\n\n\n<p>\u670d\u52a1\u5668\u7aef\u7684\u5b9e\u73b0\u4f1a\u4f9d\u8d56\u4e8e\u4f60\u9009\u62e9\u7684\u540e\u7aef\u6280\u672f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Node.js\u548cws\u5e93\u7684\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>\u793a\u4f8b\u4ee3\u7801\uff08Node.js + ws\uff09\uff1a<\/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=\"\">const WebSocket = require('ws');\nconst server = new WebSocket.Server({ port: 8080 });\n\nserver.on('connection', function(socket) {\n    console.log('\u65b0\u5ba2\u6237\u7aef\u5df2\u8fde\u63a5');\n\n    \/\/ \u76d1\u542c\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6d88\u606f\n    socket.on('message', function(message) {\n        console.log('\u6536\u5230\u6d88\u606f\uff1a', message);\n        \/\/ \u5c06\u6536\u5230\u7684\u6d88\u606f\u5e7f\u64ad\u7ed9\u6240\u6709\u5ba2\u6237\u7aef\n        server.clients.forEach(function(client) {\n            if (client.readyState === WebSocket.OPEN) {\n                client.send(message);\n            }\n        });\n    });\n\n    socket.on('close', function() {\n        console.log('\u5ba2\u6237\u7aef\u5df2\u65ad\u5f00\u8fde\u63a5');\n    });\n});<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u670d\u52a1\u5668\u4f1a\u76d1\u542c8080\u7aef\u53e3\u4e0a\u7684WebSocket\u8fde\u63a5\u3002\u6bcf\u5f53\u6709\u65b0\u6d88\u606f\u65f6\uff0c\u5b83\u5c06\u6d88\u606f\u5e7f\u64ad\u7ed9\u6240\u6709\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u7b80\u5355\u7684\u5b9e\u4f8b\u5c55\u793a\u4e86WebSocket\u5982\u4f55\u5b9e\u73b0\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u5b9e\u65f6\u53cc\u5411\u901a\u4fe1\u3002<\/p>\n\n\n\n<p><strong>\u8f7b\u91cf\u7ea7\u63a8\u9001\u6280\u672fSSE<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5c0f\u840c\u7ecf\u8fc7\u5bf9websocket\u7684\u7814\u7a76\uff0c\u5df2\u7ecf\u6210\u529f\u5c06\u5176\u5e94\u7528\u5230\u9879\u76ee\u5f00\u53d1\u4e2d\u3002\u4f5c\u4e3a\u8f6f\u4ef6\u5f00\u53d1\u5de5\u7a0b\u5e08\u7684\u5c0f\u840c\u53c8\u5229\u7528\u4e1a\u4f59\u65f6\u95f4\u7814\u7a76\u4e86\u53e6\u5916\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u63a8\u9001\u6280\u672f-SSE\u3002<\/p>\n\n\n\n<p>Server-Sent Events\uff08SSE\uff09\u662f\u4e00\u79cd\u5141\u8bb8\u670d\u52a1\u5668\u5411\u6d4f\u89c8\u5668\u63a8\u9001\u5b9e\u65f6\u6570\u636e\u7684\u6280\u672f\u3002\u5b83\u662f\u57fa\u4e8eHTTP\u534f\u8bae\u7684\uff0c\u5e76\u4e14\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u9002\u7528\u4e8e\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef\u7684\u5355\u5411\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u5173\u4e8eSSE\u7684\u4e00\u4e9b\u5173\u952e\u70b9\uff1a<\/p>\n\n\n\n<p>\u5de5\u4f5c\u539f\u7406<\/p>\n\n\n\n<p>SSE\u5229\u7528HTTP\u8fde\u63a5\u6765\u5b9e\u73b0\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef\u7684\u5355\u5411\u901a\u4fe1\u3002\u4e00\u65e6\u5ba2\u6237\u7aef\u901a\u8fc7EventSource\u63a5\u53e3\u8fde\u63a5\u5230\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u5c31\u53ef\u4ee5\u53d1\u9001\u6570\u636e\u5230\u5ba2\u6237\u7aef\u3002\u5ba2\u6237\u7aef\u63a5\u6536\u5230\u6570\u636e\u540e\uff0c\u9ed8\u8ba4\u4f1a\u89e6\u53d1message\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<p>\u7279\u70b9<\/p>\n\n\n\n<ul>\n<li><strong>\u57fa\u4e8eHTTP<\/strong>\uff1aSSE\u4f7f\u7528\u6807\u51c6\u7684HTTP\u534f\u8bae\uff0c\u56e0\u6b64\u6613\u4e8e\u5b9e\u73b0\u548c\u90e8\u7f72\u3002<\/li>\n\n\n\n<li><strong>\u5355\u5411\u901a\u4fe1<\/strong>\uff1aSSE\u4e3b\u8981\u7528\u4e8e\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u7684\u5355\u5411\u6570\u636e\u63a8\u9001\uff0c\u4e0d\u652f\u6301\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u7684\u63a8\u9001\u3002<\/li>\n\n\n\n<li><strong>\u8f7b\u91cf\u7ea7<\/strong>\uff1a\u4e0eWebSocket\u76f8\u6bd4\uff0cSSE\u66f4\u7b80\u5355\uff0c\u4e0d\u9700\u8981\u590d\u6742\u7684\u63e1\u624b\u8fc7\u7a0b\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u91cd\u8fde<\/strong>\uff1a\u5982\u679c\u8fde\u63a5\u65ad\u5f00\uff0cSSE\u4f1a\u81ea\u52a8\u5c1d\u8bd5\u91cd\u8fde\u3002<\/li>\n\n\n\n<li><strong>\u6587\u672c\u6570\u636e<\/strong>\uff1aSSE\u4e3b\u8981\u63a8\u9001\u6587\u672c\u6570\u636e\uff0c\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6570\u636e\u9700\u8981\u8fdb\u884c\u7f16\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f7f\u7528\u573a\u666f<\/p>\n\n\n\n<ul>\n<li><strong>\u5b9e\u65f6\u66f4\u65b0<\/strong>\uff1a\u5982\u80a1\u7968\u4ef7\u683c\u3001\u4f53\u80b2\u6bd4\u8d5b\u5f97\u5206\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u793e\u4ea4\u5a92\u4f53<\/strong>\uff1a\u5b9e\u65f6\u663e\u793a\u597d\u53cb\u52a8\u6001\u3001\u6d88\u606f\u901a\u77e5\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u65b0\u95fb\u7f51\u7ad9<\/strong>\uff1a\u5b9e\u65f6\u63a8\u9001\u65b0\u95fb\u5934\u6761\u3002<\/li>\n\n\n\n<li><strong>\u5728\u7ebf\u6e38\u620f<\/strong>\uff1a\u63a8\u9001\u6e38\u620f\u72b6\u6001\u66f4\u65b0\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6570\u636e\u683c\u5f0f<br>SSE \u534f\u8bae\u975e\u5e38\u7b80\u5355\uff0c\u6b63\u5e38\u7684Http\u8bf7\u6c42\uff0c\u66f4\u6539\u8bf7\u8d77\u5934\u76f8\u5173\u914d\u7f6e\u5373\u53ef<\/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=\"\">Content-Type: text\/event-stream,utf-8\nCache-Control: no-cache\nConnection: keep-alive<\/pre>\n\n\n\n<p>\u53d1\u9001\u7684\u6587\u672c\u6d41\uff0c\u7528UTF8\u683c\u5f0f\u7f16\u7801\u3002\u6587\u672c\u4e8b\u4ef6\u6d41\u7684\u6d88\u606f\u7531\u4e24\u4e2a\u6362\u884c\u7b26\u5206\u5f00\uff0c\u4ee5\u5192\u53f7\u5f00\u5934\u7684\u4e3a\u6ce8\u91ca\u884c\uff0c\u4f1a\u88ab\u5ffd\u7565\u3002<\/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=\"891\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99-1024x891.png\" alt=\"\" class=\"wp-image-15619\" style=\"width:422px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99-1024x891.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99-300x261.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99-768x668.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99-830x722.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99-230x200.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99-350x305.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99-480x418.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-99.png 1294w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u6587\u672c\u6d41\u5b57\u6bb5<\/strong><\/p>\n\n\n\n<p>event: \u7528\u4e8e\u6807\u8bc6\u4e8b\u4ef6\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9aevent\uff0c\u6d4f\u89c8\u5668\u9ed8\u8ba4\u8ba4\u4e3a\u662fmessage\u3002<\/p>\n\n\n\n<p>data: \u6d88\u606f\u7684\u6570\u636e\u5b57\u6bb5\uff0c\u5f53EventSource\u6536\u5230\u591a\u4e2a&#8220;data:&#8220;\u5f00\u5934\u7684\u8fde\u7eed\u884c\u65f6\uff0c\u4f1a\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\uff0c\u5728\u5b83\u4eec\u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u6362\u884c\u7b26\u3002\u672b\u5c3e\u7684\u6362\u884c\u7b26\u4e5f\u4f1a\u88ab\u5220\u9664\u3002<\/p>\n\n\n\n<p>id: \u4e8b\u4ef6ID\uff0c\u4f1a\u88ab\u8bbe\u7f6e\u4e3a\u5f53\u524dEventSource\u5bf9\u8c61\u7684\u5185\u90e8\u5c5e\u6027\u201c\u6700\u540e\u4e00\u4e2a\u4e8b\u4ef6ID\u201d\u7684\u503c\u3002<\/p>\n\n\n\n<p>retry: \u91cd\u65b0\u8fde\u63a5\u7684\u65f6\u95f4\u3002\u5982\u679c\u4e0e\u670d\u52a1\u5668\u7684\u8fde\u63a5\u4e22\u5931\uff0c\u6d4f\u89c8\u5668\u4f1a\u7b49\u5f85\u6307\u5b9a\u7684\u65f6\u95f4\uff0c\u7136\u540e\u91cd\u65b0\u8fde\u63a5\u3002retry\u5fc5\u987b\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u5b83\u7684\u5355\u4f4d\u662f\u6beb\u79d2\u3002\u00a0<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"845\" height=\"1024\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100-845x1024.png\" alt=\"\" class=\"wp-image-15620\" style=\"width:464px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100-845x1024.png 845w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100-248x300.png 248w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100-768x931.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100-830x1006.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100-230x279.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100-350x424.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100-480x582.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/06\/\u56fe\u7247-100.png 1038w\" sizes=\"(max-width: 845px) 100vw, 845px\" \/><\/figure><\/div>\n\n\n<p><strong>\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<p>\u670d\u52a1\u5668\u7aef<\/p>\n\n\n\n<p>\u670d\u52a1\u5668\u7aef\u4f7f\u7528express\u6846\u67b6\u521b\u5efa\u4e00\u4e2a\u6301\u4e45\u7684HTTP\u8fde\u63a5\uff0c\u5e76\u5728\u6709\u65b0\u6570\u636e\u65f6\u53d1\u9001\u6570\u636e\u5230\u5ba2\u6237\u7aef\u3002\u6570\u636e\u901a\u5e38\u4ee5\u7eaf\u6587\u672c\u683c\u5f0f\u53d1\u9001\uff0c\u5e76\u4e14\u6bcf\u6761\u6d88\u606f\u4e4b\u95f4\u4ee5\u4e00\u5bf9\u6362\u884c\u7b26\u5206\u9694\u3002<\/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=\"\">const express = require('express') \/\/\u5f15\u7528\u6846\u67b6\nconst app = express() \/\/\u521b\u5efa\u670d\u52a1\nconst port = 8088 \/\/\u9879\u76ee\u542f\u52a8\u7aef\u53e3\n\n\/\/\u8bbe\u7f6e\u8de8\u57df\u8bbf\u95ee\napp.all('*', function (req, res, next) {\n    \/\/\u8bbe\u7f6e\u5141\u8bb8\u8de8\u57df\u7684\u57df\u540d\uff0c*\u4ee3\u8868\u5141\u8bb8\u4efb\u610f\u57df\u540d\u8de8\u57df\n    res.header('Access-Control-Allow-Origin', '*')\n    \/\/\u5141\u8bb8\u7684header\u7c7b\u578b\n    res.header(\n        'Access-Control-Allow-Headers',\n        'Content-Type, Authorization, X-Requested-With'\n    )\n    \/\/\u8de8\u57df\u5141\u8bb8\u7684\u8bf7\u6c42\u65b9\u5f0f\n    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')\n    \/\/ \u53ef\u4ee5\u5e26cookies\n    res.header('Access-Control-Allow-Credentials', true)\n    if (req.method == 'OPTIONS') {\n        res.sendStatus(200)\n    } else {\n        next()\n    }\n})\n\napp.get('\/sse', (req, res) => {\n    res.set({\n        'Content-Type': 'text\/event-stream', \/\/\u8bbe\u5b9a\u6570\u636e\u7c7b\u578b\n        'Cache-Control': 'no-cache', \/\/ \u957f\u94fe\u63a5\u62d2\u7edd\u7f13\u5b58\n        'Connection': 'keep-alive', \/\/\u8bbe\u7f6e\u957f\u94fe\u63a5\n    })\n\n    console.log('\u8fdb\u5165\u5230\u957f\u8fde\u63a5\u4e86')\n    \/\/\u6301\u7eed\u8fd4\u56de\u6570\u636e\n    setInterval(() => {\n        console.log('\u6b63\u5728\u6301\u7eed\u8fd4\u56de\u6570\u636e\u4e2ding')\n        const data = {\n            message: `\u5f53\u524d\u65f6\u95f4: ${new Date().toLocaleTimeString()}`,\n        }\n        res.write(`data: ${JSON.stringify(data)}\\n\\n`)\n    }, 1000)\n\n    \/\/ \u5f53\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u65f6\uff0c\u6e05\u7406\u8d44\u6e90\n    req.on('close', () => {\n        clearInterval() \/\/ \u6e05\u9664\u5b9a\u65f6\u5668\n        res.end() \/\/ \u7ed3\u675f\u54cd\u5e94\n    })\n})\n\n\/\/\u521b\u5efa\u9879\u76ee\napp.listen(port, () => {\n    console.log(`\u9879\u76ee\u542f\u52a8\u6210\u529f-http:\/\/localhost:${port}`)\n})\n\n<\/pre>\n\n\n\n<p><strong>\u5ba2\u6237\u7aef<\/strong><\/p>\n\n\n\n<p>\u5ba2\u6237\u7aef\u4f7f\u7528EventSource\u63a5\u53e3\u6765\u63a5\u6536\u670d\u52a1\u5668\u63a8\u9001\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u5ba2\u6237\u7aef\u5b9e\u73b0\u793a\u4f8b\uff1a<\/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;!DOCTYPE html>\n&lt;html lang=\"en\">\n\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    &lt;title>Document&lt;\/title>\n&lt;\/head>\n\n&lt;body>\n    &lt;ul id=\"ul\">\n\n    &lt;\/ul>\n&lt;\/body>\n&lt;script>\n\n    \/\/\u751f\u6210li\u5143\u7d20\n    function createLi(data) {\n        let li = document.createElement(\"li\");\n        li.innerHTML = String(data.message);\n        return li;\n    }\n\n    \/\/\u5224\u65ad\u5f53\u524d\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301SSE\n    let source = ''\n    if (!!window.EventSource) {\n        source = new EventSource('http:\/\/localhost:8088\/sse\/');\n    } else {\n        throw new Error(\"\u5f53\u524d\u6d4f\u89c8\u5668\u4e0d\u652f\u6301SSE\")\n    }\n\n    \/\/\u5bf9\u4e8e\u5efa\u7acb\u94fe\u63a5\u7684\u76d1\u542c\n    source.onopen = function (event) {\n        console.log(source.readyState);\n        console.log(\"\u957f\u8fde\u63a5\u6253\u5f00\");\n    };\n\n    \/\/\u5bf9\u670d\u52a1\u7aef\u6d88\u606f\u7684\u76d1\u542c\n    source.onmessage = function (event) {\n        console.log(JSON.parse(event.data));\n        console.log(\"\u6536\u5230\u957f\u8fde\u63a5\u4fe1\u606f\");\n        let li = createLi(JSON.parse(event.data));\n        document.getElementById(\"ul\").appendChild(li)\n    };\n\n\n    \/\/\u5bf9\u65ad\u5f00\u94fe\u63a5\u7684\u76d1\u542c\n    source.onerror = function (event) {\n        console.log(source.readyState);\n        console.log(\"\u957f\u8fde\u63a5\u4e2d\u65ad\");\n    };\n\n&lt;\/script>\n\n&lt;\/html><\/pre>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u6570\u5b57\u5316\u65f6\u4ee3\uff0c\u4fe1\u606f\u7684\u5373\u65f6\u4f20\u9012\u53d8\u5f97\u81f3\u5173\u91cd\u8981\u3002\u5b9e\u65f6\u6d88\u606f\u63a8\u9001\u6280\u672f\u4f5c\u4e3a\u8fde\u63a5\u7528\u6237\u4e0e\u670d\u52a1\u7684\u91cd\u8981\u6865\u6881\uff0c\u4f7f\u5f97\u4fe1\u606f\u80fd\u591f\u8fc5\u901f\u3001\u51c6\u786e\u5730\u4f20\u9012\u7ed9\u7528\u6237\u3002\u65e0\u8bba\u662f\u793e\u4ea4\u5a92\u4f53\u7684\u901a\u77e5\u3001\u65b0\u95fb\u66f4\u65b0\u3001\u8fd8\u662f\u5e94\u7528\u5185\u7684\u6d88\u606f\u63d0\u9192\uff0c\u5b9e\u65f6\u6d88\u606f\u63a8\u9001\u6280\u672f\u90fd\u626e\u6f14\u7740\u4e0d\u53ef\u6216\u7f3a\u7684\u89d2\u8272\u3002<\/p>\n\n\n\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86web\u63a8\u9001\u6280\u672f\u7684\u51e0\u79cd\u5b9e\u73b0\uff1a\u8f6e\u5faa\u3001\u77ed\u4fe1\u901a\u77e5\u3001websocket\u3001SSE\uff0c\u91cd\u70b9\u4ecb\u7ecdwebsocket\u548cSSE\u6280\u672f\u5b9e\u73b0\u3002\u7ed9\u5c0f\u7f16\u6765\u4e2a\u70b9\u8d5e\uff0c\u5728\u770b\uff0c\u8f6c\u53d1\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u674e\u5c0f\u840c\u548c\u738b\u5927\u9524\u662f\u597d\u670b\u53cb\u3002\u4ed6\u4eec\u90fd\u4ece\u4e8b\u7a0b\u5e8f\u5f00\u53d1\u5de5\u4f5c\u3002\u4e24\u4eba\u6709\u7a7a\u7ecf\u5e38\u4f1a\u5728\u4e00\u8d77\u8ba8\u8bba\u6280\u672f\u3002\u5c0f\u840c\u6700\u8fd1\u6b63\u5728\u505a\u4e00\u4e2a\u7ba1\u7406\u7cfb\u7edf [&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\/15617"}],"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=15617"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15617\/revisions"}],"predecessor-version":[{"id":15622,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15617\/revisions\/15622"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}