{"id":1,"date":"2024-05-27T22:37:41","date_gmt":"2024-05-27T14:37:41","guid":{"rendered":"http:\/\/www.yixi.ltd\/?p=1"},"modified":"2024-05-28T19:37:47","modified_gmt":"2024-05-28T11:37:47","slug":"hello-world","status":"publish","type":"post","link":"https:\/\/www.bengbu.host\/?p=1","title":{"rendered":"Websocket"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728springboot\u9879\u76ee\u4e2d\u5f15\u5165websocket\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;dependency&gt;\n\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t&lt;artifactId&gt;spring-boot-starter-websocket&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u4e00\u4e2awebsocket\u670d\u52a1\u70b9\u5bfc\u51fa\u5668<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>import<\/strong> <strong>org.springframework.context.annotation.Bean<\/strong>;\n<strong>import<\/strong> <strong>org.springframework.context.annotation.Configuration<\/strong>;\n<strong>import<\/strong> <strong>org.springframework.stereotype.Component<\/strong>;\n<strong>import<\/strong> <strong>org.springframework.web.socket.server.standard.ServerEndpointExporter<\/strong>;\n \n@Configuration\n<strong>public<\/strong> <strong>class<\/strong> <strong>WebsocketConfig<\/strong> {\n    @Bean\n    <strong>public<\/strong> <strong>ServerEndpointExporter<\/strong> <strong>getServerEndpointExporter<\/strong>(){\n        <strong>return<\/strong> <strong>new<\/strong> <strong>ServerEndpointExporter<\/strong>();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u4e00\u4e2awebsocket\u670d\u52a1\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>import<\/strong> <strong>org.springframework.stereotype.Component<\/strong>;\n \n<strong>import<\/strong> <strong>javax.websocket.OnClose<\/strong>;\n<strong>import<\/strong> <strong>javax.websocket.OnMessage<\/strong>;\n<strong>import<\/strong> <strong>javax.websocket.OnOpen<\/strong>;\n<strong>import<\/strong> <strong>javax.websocket.Session<\/strong>;\n<strong>import<\/strong> <strong>javax.websocket.server.ServerEndpoint<\/strong>;\n<strong>import<\/strong> <strong>java.io.IOException<\/strong>;\n<strong>import<\/strong> <strong>java.util.concurrent.CopyOnWriteArraySet<\/strong>;\n \n@Component\n@ServerEndpoint(\"\/websocketurl\")\/\/\u8bbe\u7f6ewebsocket\u8fde\u63a5url\uff0c\u5c31\u662f\u524d\u7aef\u521b\u5efawebsocket\u9700\u8981\u63d0\u4f9b\u7684url\n<strong>public<\/strong> <strong>class<\/strong> <strong>MyWebSocketService<\/strong> {\n    <strong>private<\/strong> <strong>Session<\/strong> session;\n    \n    \/\/\u5b58\u653e\u6240\u6709\u7684websocket\u8fde\u63a5\n    <strong>private<\/strong> <strong>static<\/strong> <strong>CopyOnWriteArraySet<\/strong>&lt;<strong>MyWebSocketService<\/strong>&gt; myWebSocketServicesSet = <strong>new<\/strong> <strong>CopyOnWriteArraySet<\/strong>&lt;&gt;();\n    \n    \/\/\u5efa\u7acbwebsocket\u8fde\u63a5\u65f6\u81ea\u52a8\u8c03\u7528\n    @OnOpen\n    <strong>public<\/strong> <strong>void<\/strong> <strong>onOpen<\/strong>(<strong>Session<\/strong> session){\n        <strong>this<\/strong>.session = session;\n        myWebSocketServicesSet.add(<strong>this<\/strong>);\n        <strong>System<\/strong>.out.println(\"\u6709\u65b0\u7684websocket\u8fde\u63a5\u8fdb\u5165\uff0c\u5f53\u524d\u8fde\u63a5\u603b\u6570\u4e3a\" + myWebSocketServicesSet.size());\n    }\n \n    \/\/\u5173\u95edwebsocket\u8fde\u63a5\u65f6\u81ea\u52a8\u8c03\u7528\n    @OnClose\n    <strong>public<\/strong> <strong>void<\/strong> <strong>onClose<\/strong>(){\n        myWebSocketServicesSet.remove(<strong>this<\/strong>);\n        <strong>System<\/strong>.out.println(\"\u8fde\u63a5\u65ad\u5f00\uff0c\u5f53\u524d\u8fde\u63a5\u603b\u6570\u4e3a\" + myWebSocketServicesSet.size());\n    }\n \n    \/\/websocket\u63a5\u6536\u5230\u6d88\u606f\u65f6\u81ea\u52a8\u8c03\u7528\n    @OnMessage\n    <strong>public<\/strong> <strong>void<\/strong> <strong>onMessage<\/strong>(<strong>String<\/strong> message){\n        <strong>System<\/strong>.out.println(\"this\uff1a\" + message);\n    }\n \n    \/\/\u901a\u8fc7websocket\u53d1\u9001\u6d88\u606f\n    <strong>public<\/strong> <strong>void<\/strong> <strong>sendMessage<\/strong>(<strong>String<\/strong> message){\n        <strong>for<\/strong> (<strong>MyWebSocketService<\/strong> webSocketService : myWebSocketServicesSet){\n            <strong>try<\/strong> {\n                webSocketService.session.getBasicRemote().sendText(message);\n            } <strong>catch<\/strong> (<strong>IOException<\/strong> e) {\n                <strong>System<\/strong>.err.println(<strong>this<\/strong> + \"\u53d1\u9001\u6d88\u606f\u9519\u8bef:\" + e.getClass() + \":\" + e.getMessage());\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u4e00\u4e2a\u63a5\u6536\u521b\u5efa\u8ba2\u5355\u4fe1\u606f\u7684\u63a7\u5236\u5668<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>import<\/strong> <strong>org.springframework.stereotype.Controller<\/strong>;\n<strong>import<\/strong> <strong>org.springframework.web.bind.annotation.RequestMapping<\/strong>;\n<strong>import<\/strong> <strong>org.springframework.web.bind.annotation.ResponseBody<\/strong>;\n<strong>import<\/strong> <strong>xyz.syyrjx.websockettest.websocketService.MyWebSocketService<\/strong>;\n \n<strong>import<\/strong> <strong>javax.annotation.Resource<\/strong>;\n \n@Controller\n<strong>public<\/strong> <strong>class<\/strong> <strong>MyController<\/strong> {\n \n    @Resource\n    <strong>MyWebSocketService<\/strong> webSocketService;\n \n    @RequestMapping(\"\/sendMessage\")\n    @ResponseBody\n    <strong>public<\/strong> <strong>Object<\/strong> <strong>sendMessage<\/strong>(<strong>String<\/strong> msg){\n        webSocketService.sendMessage(msg);\n        <strong>return<\/strong> <strong>null<\/strong>;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728springboot\u9879\u76ee\u4e2d\u5f15&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1"}],"version-history":[{"count":2,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":13,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions\/13"}],"wp:attachment":[{"href":"https:\/\/www.bengbu.host\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}