{"id":8194,"date":"2023-11-28T20:18:14","date_gmt":"2023-11-28T12:18:14","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8194"},"modified":"2023-12-06T10:07:30","modified_gmt":"2023-12-06T02:07:30","slug":"springboot-zhong-shen-me-shi-websockets-2","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-shen-me-shi-websockets-2\/","title":{"rendered":"SpringBoot \u4e2d\u4ec0\u4e48\u662f WebSockets?"},"content":{"rendered":"<p>WebSockets \u662f\u4e00\u79cd\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u53cc\u5411\u901a\u4fe1\u7684\u534f\u8bae\u3002\u5b83\u5141\u8bb8\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5efa\u7acb\u6301\u4e45\u6027\u7684\u8fde\u63a5\uff0c\u4f7f\u5f97\u53cc\u65b9\u80fd\u591f\u901a\u8fc7\u8fd9\u4e2a\u8fde\u63a5\u8fdb\u884c\u5b9e\u65f6\u7684\u3001\u4f4e\u5ef6\u8fdf\u7684\u53cc\u5411\u901a\u4fe1\u3002\u5728 Spring Boot \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Spring \u6846\u67b6\u63d0\u4f9b\u7684 <code>WebSocket<\/code> \u6a21\u5757\u6765\u8f7b\u677e\u5b9e\u73b0 WebSockets\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5728 Spring Boot \u4e2d\u4f7f\u7528 WebSockets \u7684\u4e00\u4e9b\u5173\u952e\u6982\u5ff5\u548c\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<strong>\u4f9d\u8d56\u5f15\u5165\uff1a<\/strong> \u9996\u5148\uff0c\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u5f15\u5165 Spring WebSockets \u7684\u4f9d\u8d56\u3002\u5982\u679c\u4f7f\u7528 Maven\uff0c\u53ef\u4ee5\u5728 <code>pom.xml<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-websocket&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n<p>\u5982\u679c\u4f7f\u7528 Gradle\uff1a<\/p>\n<pre><code class=\"language-groovy\">implementation 'org.springframework.boot:spring-boot-starter-websocket'\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>WebSocket \u914d\u7f6e\uff1a<\/strong> \u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u914d\u7f6e WebSocket \u76f8\u5173\u7684 Bean\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u914d\u7f6e\u7c7b\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.messaging.simp.config.MessageBrokerRegistry;\nimport org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;\nimport org.springframework.web.socket.config.annotation.StompEndpointRegistry;\nimport org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;\n\n@Configuration\n@EnableWebSocketMessageBroker\npublic class WebSocketConfig implements WebSocketMessageBrokerConfigurer {\n\n    @Override\n    public void configureMessageBroker(MessageBrokerRegistry config) {\n        config.enableSimpleBroker(&quot;\/topic&quot;);\n        config.setApplicationDestinationPrefixes(&quot;\/app&quot;);\n    }\n\n    @Override\n    public void registerStompEndpoints(StompEndpointRegistry registry) {\n        registry.addEndpoint(&quot;\/websocket-example&quot;).withSockJS();\n    }\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u914d\u7f6e\u7c7b\u542f\u7528\u4e86 WebSocket \u6d88\u606f\u4ee3\u7406\uff0c\u5e76\u5b9a\u4e49\u4e86\u6d88\u606f\u7684\u524d\u7f00\u3002\u5b83\u8fd8\u6ce8\u518c\u4e86\u4e00\u4e2a Stomp \u534f\u8bae\u7684 WebSocket \u7aef\u70b9\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u901a\u8fc7\u8fd9\u4e2a\u7aef\u70b9\u8fde\u63a5\u5230 WebSocket \u670d\u52a1\u5668\u3002\n<\/li>\n<li>\n<strong>\u7f16\u5199 WebSocket \u63a7\u5236\u5668\uff1a<\/strong> \u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u5904\u7406 WebSocket \u7684\u6d88\u606f\u548c\u4e8b\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.messaging.handler.annotation.MessageMapping;\nimport org.springframework.messaging.handler.annotation.SendTo;\nimport org.springframework.stereotype.Controller;\n\n@Controller\npublic class WebSocketController {\n\n    @MessageMapping(&quot;\/hello&quot;)\n    @SendTo(&quot;\/topic\/greetings&quot;)\n    public Greeting greeting(HelloMessage message) {\n        return new Greeting(&quot;Hello, &quot; + message.getName() + &quot;!&quot;);\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u63a7\u5236\u5668\u5904\u7406 <code>\/hello<\/code> \u8def\u5f84\u7684\u6d88\u606f\uff0c\u5e76\u5c06\u54cd\u5e94\u53d1\u9001\u5230 <code>\/topic\/greetings<\/code> \u8def\u5f84\u3002\n<\/li>\n<li>\n<strong>\u7f16\u5199\u524d\u7aef\u4ee3\u7801\uff1a<\/strong> \u5728\u524d\u7aef\u9875\u9762\u4e2d\u4f7f\u7528 JavaScript \u6216\u5176\u4ed6\u524d\u7aef\u6846\u67b6\uff0c\u901a\u8fc7 WebSocket \u8fde\u63a5\u5230\u540e\u7aef\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528 JavaScript \u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>SockJS<\/code> \u548c <code>Stomp<\/code> \u8fd9\u4e24\u4e2a\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-javascript\">var socket = new SockJS('\/websocket-example');\nvar stompClient = Stomp.over(socket);\n\nstompClient.connect({}, function (frame) {\n    console.log('Connected: ' + frame);\n    stompClient.subscribe('\/topic\/greetings', function (greeting) {\n        console.log('Received: ' + JSON.parse(greeting.body).content);\n    });\n});\n\nfunction sendHello() {\n    stompClient.send(&quot;\/app\/hello&quot;, {}, JSON.stringify({'name': 'John'}));\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u5efa\u7acb\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 Spring Boot WebSockets \u5e94\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u5b9e\u65f6\u53cc\u5411\u901a\u4fe1\u3002\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u5230 <code>\/app\/hello<\/code> \u8def\u5f84\u65f6\uff0c\u670d\u52a1\u5668\u4f1a\u5904\u7406\u8be5\u6d88\u606f\u5e76\u5c06\u54cd\u5e94\u53d1\u9001\u5230\u6240\u6709\u8ba2\u9605\u4e86 <code>\/topic\/greetings<\/code> \u8def\u5f84\u7684\u5ba2\u6237\u7aef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WebSockets \u662f\u4e00\u79cd\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u53cc\u5411\u901a\u4fe1\u7684\u534f\u8bae\u3002\u5b83\u5141\u8bb8\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5efa\u7acb\u6301\u4e45\u6027\u7684\u8fde [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-8194","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8194"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8194\/revisions"}],"predecessor-version":[{"id":16584,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8194\/revisions\/16584"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8194"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}