{"id":4166,"date":"2024-05-17T17:18:11","date_gmt":"2024-05-17T09:18:11","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/spring-boot\/%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%ab%af%e6%8e%a8%e9%80%81%e6%8a%80%e6%9c%af%ef%bc%9aspring-boot%e4%b8%ad%e7%9a%84sse%e5%92%8cwebsocket%e5%ba%94%e7%94%a8%e5%ae%9e%e8%b7%b5\/"},"modified":"2024-08-02T16:49:50","modified_gmt":"2024-08-02T08:49:50","slug":"%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%ab%af%e6%8e%a8%e9%80%81%e6%8a%80%e6%9c%af%ef%bc%9aspring-boot%e4%b8%ad%e7%9a%84sse%e5%92%8cwebsocket%e5%ba%94%e7%94%a8%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/spring-boot\/%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%ab%af%e6%8e%a8%e9%80%81%e6%8a%80%e6%9c%af%ef%bc%9aspring-boot%e4%b8%ad%e7%9a%84sse%e5%92%8cwebsocket%e5%ba%94%e7%94%a8%e5%ae%9e%e8%b7%b5\/","title":{"rendered":"\u670d\u52a1\u5668\u7aef\u63a8\u9001\u6280\u672f\uff1aSpring Boot\u4e2d\u7684SSE\u548cWebSocket\u5e94\u7528\u5b9e\u8df5"},"content":{"rendered":"<div class=\"wxsyncmain\">\n<blockquote style=\"padding: 15px 15px 15px 1rem; border-left-width: 6px; border-left-color: #536dfe; color: #819198; font-size: 0.9em; line-height: inherit; overflow-wrap: normal; width: 868.797px; background: #f2f7fb; overflow: auto; word-break: normal; border-radius: 9px; box-shadow: rgba(0, 0, 0, 0.14) 0px 2px 4px -1px, rgba(0, 0, 0, 0.12) 0px 4px 5px 0px, rgba(0, 0, 0, 0.4) 0px 1px 10px 0px; font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: normal; text-align: start; text-wrap: wrap;\">\n<p style=\"font-size: inherit; color: inherit; line-height: inherit;\"><span style=\"color: inherit; font-size: 14px;\">\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e5d\u6781\u5ba2<\/span><\/p>\n<p style=\"margin-bottom: 0px; color: inherit; font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif; font-size: inherit; letter-spacing: normal; text-align: start; text-wrap: wrap; outline: 0px; line-height: inherit;\"><span style=\"font-size: 14px;\">\u6b22\u8fce\u661f\u6807\u5173\u6ce8\u4e5d\u6781\u5ba2\uff0c\u4e00\u8d77\u63a2\u8ba8\u6280\u672f\u4e0e\u67b6\u6784\uff01<\/span><\/p>\n<p style=\"margin-bottom: 0px; color: inherit; font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif; font-size: inherit; letter-spacing: normal; text-align: start; text-wrap: wrap; outline: 0px; line-height: inherit;\"><span style=\"font-size: 14px;\"><span style=\"font-size: 14px; outline: 0px;\">\u5927\u5bb6\u7684\u70b9\u8d5e\u3001\u6536\u85cf\u548c\u8bc4\u8bba\u5f88\u91cd\u8981\uff0c<\/span><span style=\"font-size: 14px; outline: 0px; color: inherit;\">\u5982\u6587\u7ae0\u5bf9\u60a8\u6709\u5e2e\u52a9\u8fd8\u8bf7\u8f6c\u53d1\u652f\u6301\u4e0b\uff0c\u8c22\u8c22\uff01<\/span><\/span><\/p>\n<\/blockquote>\n<section class=\"mp_profile_iframe_wrp\"><\/section>\n<figure style=\"font-size: 16px; color: #3e3e3e; line-height: inherit; font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: normal; text-align: start; text-wrap: wrap; background-color: #ffffff;\"><img decoding=\"async\" class=\"rich_pages wxw-img lazyload\" style=\"margin-right: auto; margin-left: auto; border-radius: 6px; font-size: inherit; color: inherit; line-height: inherit; display: block; box-shadow: rgba(0, 0, 0, 0.14) 0px 2px 4px -1px, rgba(0, 0, 0, 0.12) 0px 4px 5px 0px, rgba(0, 0, 0, 0.4) 0px 1px 10px 0px; width: 578px; border: 2px solid #eeeeee;\" title=\"\u56fe\u6ce8:\u5927\u5c11\u516c\u4f17\u53f7\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145752625.gif\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-backh=\"123\" data-backw=\"496\" data-cropselx1=\"0\" data-cropselx2=\"574\" data-cropsely1=\"0\" data-cropsely2=\"358\" data-imgfileid=\"100007420\" data-ratio=\"0.2479108635097493\" data-w=\"1077\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img\" style=\"margin-right: auto; margin-left: auto; border-radius: 6px; font-size: inherit; color: inherit; line-height: inherit; display: block; box-shadow: rgba(0, 0, 0, 0.14) 0px 2px 4px -1px, rgba(0, 0, 0, 0.12) 0px 4px 5px 0px, rgba(0, 0, 0, 0.4) 0px 1px 10px 0px; width: 578px; border: 2px solid #eeeeee;\" title=\"\u56fe\u6ce8:\u5927\u5c11\u516c\u4f17\u53f7\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145752625.gif\" data-backh=\"123\" data-backw=\"496\" data-cropselx1=\"0\" data-cropselx2=\"574\" data-cropsely1=\"0\" data-cropsely2=\"358\" data-imgfileid=\"100007420\" data-ratio=\"0.2479108635097493\" data-w=\"1077\" \/><\/noscript><\/figure>\n<section style=\"font-size: 16px; color: #3e3e3e; line-height: 1.6; word-spacing: 2px; letter-spacing: 2px; font-family: Helvetica, Tahoma, \u5fae\u8f6f\u96c5\u9ed1, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">\n<section style=\"font-size: 16px; color: #3e3e3e; line-height: 1.6; word-spacing: 2px; letter-spacing: 2px; font-family: Helvetica, Tahoma, \u5fae\u8f6f\u96c5\u9ed1, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">\n<section style=\"font-size: 16px; color: #3e3e3e; line-height: 1.6; word-spacing: 2px; letter-spacing: 2px; font-family: Helvetica, Tahoma, \u5fae\u8f6f\u96c5\u9ed1, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">\n<section style=\"font-size: 16px; color: #3e3e3e; line-height: 1.6; word-spacing: 2px; letter-spacing: 2px; font-family: Helvetica, Tahoma, \u5fae\u8f6f\u96c5\u9ed1, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">\n<section style=\"font-size: 16px; color: #3e3e3e; line-height: 1.6; word-spacing: 2px; letter-spacing: 2px; font-family: Helvetica, Tahoma, \u5fae\u8f6f\u96c5\u9ed1, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\n<section style=\"font-size: 16px; color: #3e3e3e; line-height: 1.6; word-spacing: 2px; letter-spacing: 2px; font-family: Helvetica, Tahoma, \u5fae\u8f6f\u96c5\u9ed1, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u968f\u7740Web\u5e94\u7528\u7a0b\u5e8f\u5bf9\u5b9e\u65f6\u6027\u9700\u6c42\u7684\u589e\u52a0\uff0c\u670d\u52a1\u5668\u7aef\u63a8\u9001\u6280\u672f\u6210\u4e3a\u4e86\u4e00\u79cd\u91cd\u8981\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5728Spring Boot\u6846\u67b6\u4e2d\uff0cSSE\uff08Server-Sent Events\uff09\u548cWebSocket\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u670d\u52a1\u5668\u7aef\u63a8\u9001\u6280\u672f\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e24\u79cd\u6280\u672f\u7684\u539f\u7406\u3001\u5e94\u7528\u573a\u666f\u4ee5\u53ca\u5728Spring Boot\u4e2d\u7684\u5b9e\u8df5\u65b9\u6cd5\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"rich_pages wxw-img lazyload\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145846621.jpeg\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-galleryid=\"\" data-imgfileid=\"100001510\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145846621.jpeg\" data-galleryid=\"\" data-imgfileid=\"100001510\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><\/noscript><\/p>\n<h3 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: center; font-size: 20px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u670d\u52a1\u5668\u7aef\u63a8\u9001\u6280\u672f\u6982\u8ff0<\/span><\/h3>\n<h4 style=\"line-height: inherit; font-weight: bolder; font-size: 18px; text-align: left; border-left: 5px solid #3d5afe; padding-left: 9px; margin-top: 18px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">1.1 SSE\uff08Server-Sent Events\uff09<\/span><\/h4>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">SSE\u662f\u4e00\u79cd\u57fa\u4e8eHTTP\u534f\u8bae\u7684\u670d\u52a1\u5668\u7aef\u63a8\u9001\u6280\u672f\uff0c\u5141\u8bb8\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u5355\u5411\u53d1\u9001\u4e8b\u4ef6\u6d41\u3002\u5b83\u901a\u8fc7\u6807\u51c6\u7684HTTP\u8fde\u63a5\u5b9e\u73b0\uff0c\u5229\u7528\u4e86HTTP\u7684\u957f\u8fde\u63a5\u673a\u5236\u3002\u5ba2\u6237\u7aef\u901a\u8fc7EventSource\u5bf9\u8c61\u76d1\u542c\u670d\u52a1\u5668\u7aef\u7684\u4e8b\u4ef6\u6d41\uff0c\u4ece\u800c\u5b9e\u73b0\u5b9e\u65f6\u66f4\u65b0\u3002<\/p>\n<h4 style=\"line-height: inherit; font-weight: bolder; font-size: 18px; text-align: left; border-left: 5px solid #3d5afe; padding-left: 9px; margin-top: 18px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">1.2 WebSocket<\/span><\/h4>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">WebSocket\u662f\u4e00\u79cd\u5168\u53cc\u5de5\u901a\u4fe1\u534f\u8bae\uff0c\u5141\u8bb8\u5728\u5355\u4e2aTCP\u8fde\u63a5\u4e0a\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\u3002\u4e0e\u4f20\u7edf\u7684HTTP\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\u4e0d\u540c\uff0cWebSocket\u5efa\u7acb\u4e00\u6b21\u8fde\u63a5\u540e\uff0c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u53ef\u4ee5\u4fdd\u6301\u957f\u8fde\u63a5\uff0c\u5e76\u53ef\u4ee5\u4e92\u76f8\u53d1\u9001\u6570\u636e\uff0c\u5b9e\u73b0\u5b9e\u65f6\u901a\u4fe1\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"rich_pages wxw-img lazyload\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145848139.jpeg\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-galleryid=\"\" data-imgfileid=\"100001550\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145848139.jpeg\" data-galleryid=\"\" data-imgfileid=\"100001550\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><\/noscript><\/p>\n<h3 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: center; font-size: 20px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">SSE\u4e0eWebSocket\u7684\u6bd4\u8f83<\/span><\/h3>\n<h4 style=\"line-height: inherit; font-weight: bolder; font-size: 18px; text-align: left; border-left: 5px solid #3d5afe; padding-left: 9px; margin-top: 18px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">2.1 \u6027\u80fd\u6bd4\u8f83<\/span><\/h4>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">SSE\u4f7f\u7528HTTP\u957f\u8fde\u63a5\uff0c\u5728\u4e00\u4e9b\u573a\u666f\u4e0b\u53ef\u80fd\u4f1a\u51fa\u73b0\u8f83\u9ad8\u7684\u5ef6\u8fdf\uff0c\u4f46\u76f8\u5bf9\u4e8eWebSocket\u800c\u8a00\uff0c\u5b83\u7684\u5b9e\u73b0\u66f4\u7b80\u5355\uff0c\u5bf9\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u66f4\u4f4e\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">WebSocket\u5219\u901a\u8fc7\u5355\u4e2aTCP\u8fde\u63a5\u5b9e\u73b0\u53cc\u5411\u901a\u4fe1\uff0c\u6027\u80fd\u8f83\u597d\uff0c\u9002\u7528\u4e8e\u5b9e\u65f6\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002<\/p>\n<h4 style=\"line-height: inherit; font-weight: bolder; font-size: 18px; text-align: left; border-left: 5px solid #3d5afe; padding-left: 9px; margin-top: 18px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">2.2 \u517c\u5bb9\u6027<\/span><\/h4>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">SSE\u5728\u73b0\u4ee3\u6d4f\u89c8\u5668\u4e2d\u6709\u8f83\u597d\u7684\u517c\u5bb9\u6027\uff0c\u4f46\u5728\u4e00\u4e9b\u65e7\u7248\u672c\u6d4f\u89c8\u5668\u4e2d\u5b58\u5728\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">WebSocket\u9700\u8981\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u7aef\u540c\u65f6\u652f\u6301\uff0c\u517c\u5bb9\u6027\u76f8\u5bf9\u8f83\u597d\uff0c\u4f46\u4e0d\u652f\u6301\u7684\u6d4f\u89c8\u5668\u9700\u8981\u63d0\u4f9b\u5907\u7528\u65b9\u6848\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"rich_pages wxw-img lazyload\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145849224.jpeg\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-galleryid=\"\" data-imgfileid=\"100001554\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145849224.jpeg\" data-galleryid=\"\" data-imgfileid=\"100001554\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><\/noscript><\/p>\n<h3 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: center; font-size: 20px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u5728Spring Boot\u4e2d\u4f7f\u7528SSE\u548cWebSocket<\/span><\/h3>\n<h4 style=\"line-height: inherit; font-weight: bolder; font-size: 18px; text-align: left; border-left: 5px solid #3d5afe; padding-left: 9px; margin-top: 18px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">3.1 \u5b9e\u73b0SSE\u670d\u52a1\u7aef<\/span><\/h4>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u5728Spring Boot\u4e2d\uff0c\u4f7f\u7528<code style=\"font-size: inherit; line-height: inherit; padding: 3px 6px; border-radius: 4px; margin-right: 9px; margin-left: 9px; color: #7c4dff; background: #f8f8f8; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">ResponseBodyEmitter<\/code>\u6216<code style=\"font-size: inherit; line-height: inherit; padding: 3px 6px; border-radius: 4px; margin-right: 9px; margin-left: 9px; color: #7c4dff; background: #f8f8f8; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">SseEmitter<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0SSE\u670d\u52a1\u7aef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\"><\/code><\/pre>\n<div>\n<pre>import org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport org.springframework.web.servlet.mvc.method.annotation.SseEmitter;\n\n@RestController\npublic class SSEController {\n    @GetMapping(\"\/sse\")\n    public SseEmitter sse() {\n        SseEmitter emitter = new SseEmitter();\n        \/\/\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u53d1\u9001\u4e8b\u4ef6\u6d41\n        new Thread(() -&gt; {\n            try {\n                for (int i = 0; i &lt; 10; i++) {\n                    emitter.send(SseEmitter.event().data(\"Event\" + i));\n                    Thread.sleep(1000);\n                }\n                emitter.complete();\n            } catch (Exception e) {\n                emitter.completeWithError(e);\n            }\n        }).start();\n        return emitter;\n    }\n}<\/pre>\n<\/div>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\">\u00a0<\/code><\/pre>\n<h4 style=\"line-height: inherit; font-weight: bolder; font-size: 18px; text-align: left; border-left: 5px solid #3d5afe; padding-left: 9px; margin-top: 18px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">3.2 \u5b9e\u73b0WebSocket\u670d\u52a1\u7aef<\/span><\/h4>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">Spring Boot\u63d0\u4f9b\u4e86WebSocket\u652f\u6301\uff0c\u53ef\u4ee5\u4f7f\u7528<code style=\"font-size: inherit; line-height: inherit; padding: 3px 6px; border-radius: 4px; margin-right: 9px; margin-left: 9px; color: #7c4dff; background: #f8f8f8; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">@ServerEndpoint<\/code>\u6ce8\u89e3\u6216<code style=\"font-size: inherit; line-height: inherit; padding: 3px 6px; border-radius: 4px; margin-right: 9px; margin-left: 9px; color: #7c4dff; background: #f8f8f8; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53;\">WebSocketHandler<\/code>\u63a5\u53e3\u5b9e\u73b0WebSocket\u670d\u52a1\u7aef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\"><\/code><\/pre>\n<div>\n<pre>@ServerEndpoint(\"\/websocket\")\npublic class WebSocketServer {\n    @OnOpen\n    public void onOpen(Session session) {\n        \/\/\u65b0\u7684WebSocket\u8fde\u63a5\u5efa\u7acb\u65f6\u89e6\u53d1\n    }\n\n    @OnMessage\n    public void onMessage(String message, Session session) {\n        \/\/\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u6d88\u606f\u65f6\u89e6\u53d1\n    }\n\n    @OnClose\n    public void onClose(Session session) {\n        \/\/WebSocket\u8fde\u63a5\u5173\u95ed\u65f6\u89e6\u53d1\n    }\n}\n         \n    \n<\/pre>\n<\/div>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\">\u00a0<\/code><\/pre>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"rich_pages wxw-img lazyload\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145850357.jpeg\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-galleryid=\"\" data-imgfileid=\"100001553\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145850357.jpeg\" data-galleryid=\"\" data-imgfileid=\"100001553\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><\/noscript><\/p>\n<h3 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: center; font-size: 20px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u5e94\u7528\u573a\u666f<\/span><\/h3>\n<h4 style=\"line-height: inherit; font-weight: bolder; font-size: 18px; text-align: left; border-left: 5px solid #3d5afe; padding-left: 9px; margin-top: 18px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">4.1 SSE\u7684\u5e94\u7528\u573a\u666f<\/span><\/h4>\n<h5 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: left; margin-top: 9px; margin-bottom: 9px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u5b9e\u65f6\u80a1\u7968\u884c\u60c5<\/span><\/h5>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">SSE\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5b9e\u65f6\u80a1\u7968\u884c\u60c5\u7684\u63a8\u9001\u3002\u91d1\u878d\u884c\u4e1a\u5bf9\u5b9e\u65f6\u6570\u636e\u7684\u9700\u6c42\u975e\u5e38\u9ad8\uff0c\u6295\u8d44\u8005\u9700\u8981\u53ca\u65f6\u83b7\u53d6\u80a1\u7968\u4ef7\u683c\u7684\u53d8\u52a8\u60c5\u51b5\u3002\u901a\u8fc7SSE\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u5b9e\u65f6\u5c06\u80a1\u7968\u4ef7\u683c\u53d8\u52a8\u7684\u6d88\u606f\u63a8\u9001\u7ed9\u5ba2\u6237\u7aef\uff0c\u4f7f\u7528\u6237\u53ef\u4ee5\u5b9e\u65f6\u4e86\u89e3\u80a1\u7968\u7684\u884c\u60c5\u53d8\u5316\uff0c\u63d0\u9ad8\u6295\u8d44\u51b3\u7b56\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\"><\/code><\/pre>\n<div>\n<pre>@GetMapping(\"\/stock\")\npublic SseEmitter stockPrice() {\n    SseEmitter emitter = new SseEmitter();\n    \/\/\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5b9e\u65f6\u83b7\u53d6\u80a1\u7968\u4ef7\u683c\uff0c\u5e76\u5c06\u53d8\u52a8\u63a8\u9001\u7ed9\u5ba2\u6237\u7aef\n    new Thread(() -&gt; {\n        try {\n            while (true) {\n                StockPrice price = stockService.getStockPrice();\n                emitter.send(SseEmitter.event().data(price));\n                Thread.sleep(1000);\n            }\n        } catch (Exception e) {\n            emitter.completeWithError(e);\n        }\n    }).start();\n    return emitter;\n}<\/pre>\n<\/div>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\">\u00a0<\/code><\/pre>\n<h5 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: left; margin-top: 9px; margin-bottom: 9px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u804a\u5929\u5ba4\u6d88\u606f\u63a8\u9001<\/span><\/h5>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u662f\u5b9e\u65f6\u804a\u5929\u5ba4\u3002\u5728\u5728\u7ebf\u804a\u5929\u5e94\u7528\u4e2d\uff0c\u7528\u6237\u9700\u8981\u53ca\u65f6\u6536\u5230\u5176\u4ed6\u7528\u6237\u53d1\u9001\u7684\u6d88\u606f\uff0c\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u4ea4\u6d41\u3002\u4f7f\u7528SSE\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u5b9e\u65f6\u5c06\u65b0\u6d88\u606f\u63a8\u9001\u7ed9\u5728\u7ebf\u7528\u6237\uff0c\u4ece\u800c\u5b9e\u73b0\u804a\u5929\u5ba4\u7684\u5b9e\u65f6\u6d88\u606f\u63a8\u9001\u529f\u80fd\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\"><\/code><\/pre>\n<div>\n<pre>@GetMapping(\"\/chat\")\npublic SseEmitter chatRoom() {\n    SseEmitter emitter = new SseEmitter();\n    \/\/\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5b9e\u65f6\u83b7\u53d6\u65b0\u6d88\u606f\uff0c\u5e76\u5c06\u6d88\u606f\u63a8\u9001\u7ed9\u5ba2\u6237\u7aef \n    new Thread(() -&gt; {\n        try {\n            while (true) {\n                ChatMessage message = chatService.getNewMessage();\n                emitter.send(SseEmitter.event().data(message));\n                Thread.sleep(1000);\n            }\n        } catch (Exception e) {\n            emitter.completeWithError(e);\n        }\n    }).start();\n    return emitter;\n}<\/pre>\n<\/div>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\">\u00a0<\/code><\/pre>\n<h5 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: left; margin-top: 9px; margin-bottom: 9px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u5b9e\u65f6\u4e8b\u4ef6\u901a\u77e5<\/span><\/h5>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">SSE\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u65f6\u4e8b\u4ef6\u901a\u77e5\uff0c\u4f8b\u5982\u7cfb\u7edf\u544a\u8b66\u3001\u65b0\u95fb\u63a8\u9001\u7b49\u3002\u901a\u8fc7SSE\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u5b9e\u65f6\u4e8b\u4ef6\u901a\u77e5\uff0c\u4f7f\u7528\u6237\u53ca\u65f6\u4e86\u89e3\u91cd\u8981\u4fe1\u606f\uff0c\u63d0\u9ad8\u7cfb\u7edf\u7684\u54cd\u5e94\u80fd\u529b\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\"><\/code><\/pre>\n<div>\n<pre>@GetMapping(\"\/event\")\npublic SseEmitter eventNotification() {\n    SseEmitter emitter = new SseEmitter();\n    \/\/\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5b9e\u65f6\u83b7\u53d6\u65b0\u4e8b\u4ef6\uff0c\u5e76\u5c06\u4e8b\u4ef6\u63a8\u9001\u7ed9\u5ba2\u6237\u7aef\n    new Thread(() -&gt; {\n        try {\n            while (true) {\n                EventNotification event = eventService.getNewEvent();\n                emitter.send(SseEmitter.event().data(event));\n                Thread.sleep(1000);\n            }\n        } catch (Exception e) {\n            emitter.completeWithError(e);\n        }\n    }).start();\n    return emitter;\n}<\/pre>\n<\/div>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\">\u00a0<\/code><\/pre>\n<h4 style=\"line-height: inherit; font-weight: bolder; font-size: 18px; text-align: left; border-left: 5px solid #3d5afe; padding-left: 9px; margin-top: 18px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">4.2 WebSocket\u7684\u5e94\u7528\u573a\u666f<\/span><\/h4>\n<h5 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: left; margin-top: 9px; margin-bottom: 9px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u5728\u7ebf\u6e38\u620f\u5b9e\u65f6\u901a\u4fe1<\/span><\/h5>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">WebSocket\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5728\u7ebf\u6e38\u620f\u4e2d\u7684\u5b9e\u65f6\u901a\u4fe1\u3002\u5728\u591a\u4eba\u5728\u7ebf\u6e38\u620f\u4e2d\uff0c\u73a9\u5bb6\u9700\u8981\u5b9e\u65f6\u83b7\u53d6\u5176\u4ed6\u73a9\u5bb6\u7684\u52a8\u6001\u3001\u6e38\u620f\u573a\u666f\u7684\u53d8\u5316\u7b49\u4fe1\u606f\uff0c\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u7684\u6e38\u620f\u4ea4\u4e92\u548c\u6c9f\u901a\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\"><\/code><\/pre>\n<div>\n<pre>@ServerEndpoint(\"\/game\")\npublic class GameWebSocket {\n    @OnMessage\n    public voidon Message(String message, Session session) {\n        \/\/\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6e38\u620f\u6d88\u606f\n    }\n\n    @OnClose\n    public void onClose(Session session) {\/\/WebSocket\u8fde\u63a5\u5173\u95ed\u65f6\u89e6\u53d1\n    }\n}\n<\/pre>\n<\/div>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\">\u00a0<\/code><\/pre>\n<h5 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: left; margin-top: 9px; margin-bottom: 9px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u5b9e\u65f6\u5728\u7ebf\u534f\u4f5c\u7f16\u8f91<\/span><\/h5>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u662f\u5b9e\u65f6\u5728\u7ebf\u534f\u4f5c\u7f16\u8f91\u3002\u5728\u534f\u4f5c\u7f16\u8f91\u5e94\u7528\u4e2d\uff0c\u591a\u4e2a\u7528\u6237\u9700\u8981\u540c\u65f6\u7f16\u8f91\u540c\u4e00\u4e2a\u6587\u6863\uff0c\u800c\u4e14\u9700\u8981\u5b9e\u65f6\u540c\u6b65\u5176\u4ed6\u7528\u6237\u7684\u7f16\u8f91\u5185\u5bb9\uff0c\u4ee5\u5b9e\u73b0\u534f\u4f5c\u7f16\u8f91\u7684\u6548\u679c\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\"><\/code><\/pre>\n<div>\n<pre>@ServerEndpoint(\"\/document\")\npublic class DocumentWebSocket {\n    @OnMessage\n    public void onMessage(String message, Session session) {\n        \/\/\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6587\u6863\u7f16\u8f91\u5185\u5bb9\n    }\n\n    @OnClose\n    public void onClose(Session session) {\/\/WebSocket\u8fde\u63a5\u5173\u95ed\u65f6\u89e6\u53d1 \n    }\n}\n<\/pre>\n<\/div>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\">\u00a0<\/code><\/pre>\n<h5 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: left; margin-top: 9px; margin-bottom: 9px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u5b9e\u65f6\u76d1\u63a7\u548c\u63a7\u5236\u7cfb\u7edf<\/span><\/h5>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">WebSocket\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u65f6\u76d1\u63a7\u548c\u63a7\u5236\u7cfb\u7edf\u3002\u5728<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u5de5\u4e1a\u63a7\u5236\u7cfb\u7edf\u3001\u667a\u80fd\u5bb6\u5c45\u7cfb\u7edf\u7b49\u9886\u57df\uff0c\u9700\u8981\u5b9e\u65f6\u76d1\u63a7\u548c\u63a7\u5236\u8bbe\u5907\u72b6\u6001\u3001\u73af\u5883\u53c2\u6570\u7b49\u4fe1\u606f\uff0c\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u7684\u76d1\u63a7\u548c\u8fdc\u7a0b\u63a7\u5236\u3002<\/p>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\"><\/code><\/pre>\n<div>\n<pre>@ServerEndpoint(\"\/monitor\")\npublic class MonitorWebSocket {\n    @OnMessage\n    public void onMessage(String message, Session session) {\/\/\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u76d1\u63a7\u6307\u4ee4\n    }\n\n    @OnClose\n    public void onClose(Session session) {\/\/WebSocket\u8fde\u63a5\u5173\u95ed\u65f6\u89e6\u53d1\n    }\n}<\/pre>\n<\/div>\n<pre style=\"font-size: inherit; color: inherit; line-height: inherit; overflow: hidden;\"><code style=\"line-height: 18px; word-spacing: 0px; letter-spacing: 0px; border-radius: 9px; box-shadow: #3f3f3f 3px 3px 9px; font-family: Helvetica, Tahoma, \u6977\u4f53, \u534e\u6587\u6977\u4f53, \u5b8b\u4f53; background: #282b2e; color: #e0e2e4; margin-top: 16px; margin-bottom: 16px; padding: 18px; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;\">\u00a0<\/code><\/pre>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"rich_pages wxw-img lazyload\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145852432.jpeg\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-galleryid=\"\" data-imgfileid=\"100001552\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145852432.jpeg\" data-galleryid=\"\" data-imgfileid=\"100001552\" data-ratio=\"0.1111111111111111\" data-s=\"300,640\" data-type=\"jpeg\" data-w=\"1080\" \/><\/noscript><\/p>\n<h3 style=\"line-height: inherit; font-weight: bolder; color: #3d5afe; text-align: center; font-size: 20px; margin-bottom: 18px;\"><span style=\"font-size: inherit; color: inherit; line-height: inherit;\">\u603b \u7ed3<\/span><\/h3>\n<p style=\"font-size: inherit; color: inherit; line-height: inherit; text-indent: 2em;\">SSE\u548cWebSocket\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u670d\u52a1\u5668\u7aef\u63a8\u9001\u6280\u672f\uff0c\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u5728Spring Boot\u4e2d\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0SSE\u548cWebSocket\u670d\u52a1\u7aef\uff0c\u5e76\u5e94\u7528\u4e8e\u5404\u79cd\u5b9e\u65f6\u901a\u4fe1\u573a\u666f\u4e2d\u3002\u5f00\u53d1\u4eba\u5458\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\uff0c\u5e76\u5408\u7406\u8bbe\u8ba1\u5b9e\u73b0\uff0c\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u5b9e\u65f6\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/section>\n<\/section>\n<\/section>\n<\/section>\n<\/section>\n<\/section>\n<hr style=\"font-size: inherit; color: inherit; line-height: inherit; height: 1px; margin-top: 1.5rem; margin-bottom: 1.5rem; border-right: none; border-bottom: none; border-left: none; border-top-style: dashed; border-top-color: #a5a5a5;\" \/>\n<p style=\"color: inherit; line-height: inherit; text-indent: 0em; margin-bottom: 0px;\"><strong><span style=\"font-size: 16px;\">\u611f\u8c22\u661f\u6807\u5173\u6ce8<span style=\"font-size: 16px; color: #0052ff;\">\u4e5d\u6781\u5ba2<\/span>\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\uff01<\/span><\/strong><\/p>\n<section style=\"text-align: center; margin-bottom: 0px;\"><img decoding=\"async\" class=\"rich_pages wxw-img lazyload\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145854321.gif\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-galleryid=\"\" data-imgfileid=\"100007419\" data-ratio=\"0.1859375\" data-type=\"gif\" data-w=\"640\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145854321.gif\" data-galleryid=\"\" data-imgfileid=\"100007419\" data-ratio=\"0.1859375\" data-type=\"gif\" data-w=\"640\" \/><\/noscript><\/section>\n<ul class=\"list-paddingleft-1\" style=\"list-style-type: disc;\">\n<li style=\"outline: 0px; font-size: 14px;\">\n<p style=\"outline: 0px; text-align: left;\"><span style=\"outline: 0px;\"><a style=\"outline: 0px; color: var(--weui-link); cursor: pointer;\" href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzI2NTE4MDkxNg==&amp;mid=2247489792&amp;idx=1&amp;sn=bf93b0eea19ac06ba6b054cc680c33f0&amp;chksm=eaa01a9bddd7938d08bbec13239800fbac51fb19511aa16ecd934e29c60805b95a963845fe78&amp;scene=21#wechat_redirect\" target=\"_blank\" rel=\"noopener\" data-itemshowtype=\"0\" data-linktype=\"2\" rel=\"nofollow\" >\u529f\u80fd\u4e0e\u6570\u636e\u6743\u9650\uff0cSpring AOP\u5168\u641e\u5b9a\uff01<\/a><\/span><\/p>\n<\/li>\n<li style=\"outline: 0px; font-size: 14px;\">\n<p style=\"outline: 0px; text-align: left;\"><a style=\"outline: 0px; color: var(--weui-link); cursor: pointer;\" href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzI2NTE4MDkxNg==&amp;mid=2247489806&amp;idx=1&amp;sn=501046cae7dc20475adcfaab424b70fa&amp;chksm=eaa01a95ddd79383caccfb92e4b5dcd6245eb7906ec85153c7f030e2eead3cac03ba0456c7d8&amp;scene=21#wechat_redirect\" target=\"_blank\" rel=\"noopener\" data-itemshowtype=\"0\" data-linktype=\"2\" rel=\"nofollow\" >\u4f7f\u7528Spring Boot\u6784\u5efaGraphQL\u670d\u52a1\u5b9e\u73b0\u591a\u7aef\u6570\u636e\u7edf\u4e00\u8bbf\u95ee<\/a><\/p>\n<\/li>\n<li style=\"outline: 0px; font-size: 14px;\">\n<p style=\"outline: 0px; text-align: left;\"><a style=\"outline: 0px; color: var(--weui-link); cursor: pointer;\" href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzI2NTE4MDkxNg==&amp;mid=2247489103&amp;idx=3&amp;sn=ac3a892469b20f0d404dbc177631c250&amp;chksm=eaa015d4ddd79cc2446bb6120573a0a4c2c75b0dbddeab98940147afdf1913b127cb8ae83019&amp;scene=21#wechat_redirect\" target=\"_blank\" rel=\"noopener\" data-itemshowtype=\"0\" data-linktype=\"2\" rel=\"nofollow\" >\u9ad8\u6548\u5546\u54c1\u4fe1\u606f\u7f13\u5b58\uff1aSpring Cache\u4e0eRedis\u7684\u9ec4\u91d1\u7ec4\u5408<\/a><\/p>\n<\/li>\n<li style=\"outline: 0px; font-size: 14px;\">\n<p style=\"outline: 0px; text-align: left;\"><a style=\"outline: 0px; color: var(--weui-link); cursor: pointer;\" href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzI2NTE4MDkxNg==&amp;mid=2247489799&amp;idx=1&amp;sn=cd1a2efd9680ee2aeb1966737040afab&amp;chksm=eaa01a9cddd7938ab93c5279e83cd892cd47e0bb3ee21fd3357d4eeff8c993cc83b76116edf5&amp;scene=21#wechat_redirect\" target=\"_blank\" rel=\"noopener\" data-itemshowtype=\"0\" data-linktype=\"2\" rel=\"nofollow\" >\u5728Spring Boot\u4e2d\u4f7f\u7528GraphQL\u5e94\u5bf9\u6570\u636e\u9700\u6c42\u7684\u53d8\u5316<\/a><\/p>\n<\/li>\n<li style=\"outline: 0px; font-size: 14px;\">\n<p style=\"outline: 0px; text-align: left;\"><a style=\"outline: 0px; color: var(--weui-link); cursor: pointer;\" href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzI2NTE4MDkxNg==&amp;mid=2247489819&amp;idx=1&amp;sn=867738018bc78d78db08c9170fc7c81d&amp;chksm=eaa01a80ddd793965504cbba3881e290367662f171053444830c5408826d28a7384827b599a3&amp;scene=21#wechat_redirect\" target=\"_blank\" rel=\"noopener\" data-itemshowtype=\"0\" data-linktype=\"2\" rel=\"nofollow\" >\u7528Spring AOP\u5b9e\u73b0\u6570\u636e\u6743\u9650\u7cbe\u7ec6\u63a7\u5236\uff1a\u8ba9\u4fe1\u606f\u53ea\u6d41\u5411\u8be5\u770b\u7684\u4eba\uff01<\/a><\/p>\n<\/li>\n<\/ul>\n<p style=\"display: none;\">\n<blockquote><p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7: \u4e5d\u6781\u5ba2<\/p><\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e5d\u6781\u5ba2 \u6b22\u8fce\u661f\u6807\u5173\u6ce8\u4e5d\u6781\u5ba2\uff0c\u4e00\u8d77\u63a2\u8ba8\u6280\u672f\u4e0e\u67b6\u6784\uff01 \u5927\u5bb6\u7684\u70b9\u8d5e\u3001\u6536\u85cf\u548c\u8bc4\u8bba\u5f88\u91cd\u8981\uff0c\u5982\u6587\u7ae0\u5bf9\u60a8\u6709\u5e2e\u52a9\u8fd8\u8bf7\u8f6c\u53d1\u652f\u6301\u4e0b\uff0c\u8c22\u8c22\uff01 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4156,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-4166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145746333-150x150.jpeg","content_first_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145752625.gif","post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145746333-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145746333.jpeg","post_frist_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145752625.gif","post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145746333-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145746333.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145746333.jpeg","post_all_images":[{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145752625.gif","id":"image0"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145752625.gif","id":"image1"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145846621.jpeg","id":"image2"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145846621.jpeg","id":"image3"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145848139.jpeg","id":"image4"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145848139.jpeg","id":"image5"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145849224.jpeg","id":"image6"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145849224.jpeg","id":"image7"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145850357.jpeg","id":"image8"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145850357.jpeg","id":"image9"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145852432.jpeg","id":"image10"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145852432.jpeg","id":"image11"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145854321.gif","id":"image12"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145854321.gif","id":"image13"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Spring Boot","post_date":"2024-05-17","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":637,"next_post_id":3901,"next_post_title":"Jenkins + Docker\u81ea\u52a8\u5316\u90e8\u7f72 SpringBoot \u5e94\u7528","previous_post_id":4208,"previous_post_title":"Spring Boot\u5b9e\u73b0\u77ed\u4fe1\u9a8c\u8bc1\u6ce8\u518c\u548c\u767b\u5f55","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4166"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4166\/revisions"}],"predecessor-version":[{"id":6461,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4166\/revisions\/6461"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4156"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}