{"id":315925,"date":"2021-02-02T16:07:12","date_gmt":"2021-02-02T08:07:12","guid":{"rendered":"http:\/\/4563.org\/?p=315925"},"modified":"2021-02-02T16:07:12","modified_gmt":"2021-02-02T08:07:12","slug":"%e5%9f%ba%e4%ba%8e-springboot-websocket-%e7%9a%84%e7%be%a4%e8%81%8a%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=315925","title":{"rendered":"\u57fa\u4e8e springboot websocket \u7684\u7fa4\u804a\u5b9e\u73b0"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u57fa\u4e8e springboot websocket \u7684\u7fa4\u804a\u5b9e\u73b0               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Aidenboss <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>\u57fa\u4e8e springboot websocket \u7684\u7fa4\u804a\u5b9e\u73b0<\/h2>\n<h4>\u529f\u80fd\u5217\u8868<\/h4>\n<ul>\n<li>\u5206\u5e03\u5f0f<\/li>\n<li>\u540c\u4e00\u5e10\u53f7\u591a\u8bbe\u5907\u767b\u5f55<\/li>\n<li>\u7fa4\u804a<\/li>\n<li>\u591a\u8bbe\u5907<\/li>\n<li>\u7b80\u5355\u9274\u6743<\/li>\n<li>\u5fc3\u8df3\u68c0\u67e5<\/li>\n<\/ul>\n<h4>\u4f9d\u8d56<\/h4>\n<ul>\n<li>maven<\/li>\n<li>jdk11<\/li>\n<li>redis<\/li>\n<\/ul>\n<h4>redis \u914d\u7f6e<\/h4>\n<p>redis \u9ed8\u8ba4\u4f7f\u7528 localhost:6379 \u3002\u5982\u679c\u9700\u8981\u4fee\u6539 host:port\uff0c\u53ef\u4ee5\u4fee\u6539 application.yml <br \/> redis \u4ec5\u4ec5\u7528\u4e8e\u5b58\u50a8\u7528\u6237 username \/ password<\/p>\n<h4>\u6e90\u7801\u5206\u6790<\/h4>\n<h6>Auth \u8fc7\u7a0b<\/h6>\n<pre><code>String username = ((ServletServerHttpRequest) request)   .getServletRequest().getParameter(USERNAME); String password = ((ServletServerHttpRequest) request)   .getServletRequest().getParameter(PASSWORD); User user = userService.register(username, password); \/\/ \u5c06 user \u8bbe\u7f6e\u5230 attributes \u4e2d attributes.put(USER, user); return true; <\/code><\/pre>\n<h6>\u8fde\u63a5\u521b\u5efa<\/h6>\n<pre><code>\/\/ 1. \u6dfb\u52a0 session sessionService.add(session); \/\/ 2. \u6309 username + sessionId \u751f\u6210 redis key\uff0c\u5e76\u8fdb\u884c\u8ba2\u9605\uff0c\u8fd9\u6837\u505a\u53ef\u4ee5\u652f\u6301\u591a\u8bbe\u5907\u540c\u4e00\u4e2a\u5e10\u53f7\u767b\u5f55 MessageListener messageListener = (message, pattern) -&gt; {   log.info(\"Redis sub receive: [{}]\", new String(message.getBody()));   try {     session.sendMessage(new TextMessage(message.getBody()));   } catch (IOException e) {     log.error(\"\", e);   } }; redisMessageListenerContainer.addMessageListener(messageListener,   new ChannelTopic(String.format(\"sub:%s:%s\", SessionUtil.getUsernameFromSession(session), session.getId()))); \/\/ \u7531\u4e8e session \u4f1a\u65ad\u5f00\uff0c\u9700\u8981\u4fdd\u5b58\u4e0b\u6765\uff0c\u4ee5\u5f85 removeListener messageListenerMap.put(session, messageListener); <\/code><\/pre>\n<h5>\u76d1\u542c\u6d88\u606f<\/h5>\n<pre><code>\/\/ \u83b7\u53d6\u6240\u6709\u5728\u7ebf\u7684 session\uff0c\u7136\u540e\u901a\u8fc7 redis pub \u529f\u80fd\u8f6c\u53d1\u6d88\u606f sessionService.getSessions()   .forEach(session -&gt; subPubService.pub(session, textMessage)); <\/code><\/pre>\n<h5>\u5fc3\u8df3\u68c0\u67e5<\/h5>\n<pre><code>\/\/ \u6838\u5fc3\u903b\u8f91\u662f\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\uff0c\u901a\u8fc7\u5ef6\u65f6\u961f\u5217 poll \u5b9e\u73b0\u3002 \/\/ \u5176\u4e2d HeartbeatSessionTask \u5c01\u88c5\u4e86 session \u548c\u5bf9\u5e94\u7684\u8fc7\u671f\u65f6\u95f4 this.executorService.submit(() -&gt; {   while (true) {     try {       HeartbeatSessionTask task;       while ((task = queue.poll()) != null) {         task.getSession().close();         log.warn(\"[{}] is dead, so close\", SessionUtil.getUsernameFromSession(task.session));       }     } catch (Exception e) {       log.error(\"\", e);     }     Thread.sleep(TimeUnit.SECONDS.toMillis(1));   } }); <\/code><\/pre>\n<h4>\u4f7f\u7528<\/h4>\n<h6>\u670d\u52a1\u7aef\u542f\u52a8<\/h6>\n<p>\u542f\u52a8\u540e\uff0c\u4f1a\u76d1\u542c localhost:8080 \u7aef\u53e3 <br \/> \u5176\u4e2d\uff0cwebsocket url \u4e3a ws:localhost:8080\/ws <br \/> \u83b7\u53d6\u5728\u7ebf\u7528\u6237\u6570\u63a5\u53e3\u4e3a http:\/\/localhost:8080\/session\/page<\/p>\n<h6>shell \u6d4b\u8bd5<\/h6>\n<p>\u4f7f\u7528 wscat \u6d4b\u8bd5<\/p>\n<pre><code>wscat -c 'ws:\/\/localhost:8080\/ws?username=aiden&amp;password=123' <\/code><\/pre>\n<h6>\u524d\u7aef\u6d4b\u8bd5<\/h6>\n<p>\u7531\u4e8e\u6709\u7b80\u5355\u7684\u5e10\u53f7\u4f53\u7cfb\uff0c\u94fe\u63a5\u65f6\u9700\u8981\u5236\u5b9a username \/ password\uff0c\u82e5 username \u4e0d\u5b58\u5728\uff0c\u5219\u76f4\u63a5\u6ce8\u518c\u6210\u529f\uff1b\u5426\u5219\u4f1a\u5224\u65ad username \/ password \u662f\u5426\u5339\u914d <br \/> \u5982 ws:\/\/localhost:8080\/ws?username=aiden&amp;password=123 \u624d\u80fd\u8fdb\u884c\u8fde\u63a5 \u56fe\u793a\uff1a <img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210205_601cb9855cfb1.png\" alt=\"\u57fa\u4e8e springboot websocket \u7684\u7fa4\u804a\u5b9e\u73b0\" \/><\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5185564\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wuvvu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u652f\u6301\u652f\u6301                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5185565\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : agentFitzzzzz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u9519                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5185566\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : razertory <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76f4\u63a5\u4e00\u4e2a Akka \u5c31\u80fd\u641e\u5b9a https:\/\/doc.akka.io\/docs\/akka\/1.3.1\/scala\/tutorial-chat-server.html.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5185567\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Aidenboss <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @razertory \u611f\u89c9\u5b9a\u4f4d\u4e0d\u592a\u4e00\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u4e8e springboot web&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/315925"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=315925"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/315925\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=315925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=315925"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=315925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}