{"id":6659,"date":"2025-09-29T12:35:27","date_gmt":"2025-09-29T04:35:27","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=6659"},"modified":"2025-09-29T12:38:46","modified_gmt":"2025-09-29T04:38:46","slug":"springbootwebsocket%e6%89%93%e9%80%a0%e8%b6%85%e7%a8%b3%e5%ae%9a%e5%ae%9e%e6%97%b6%e7%9b%91%e6%8e%a7%ef%bc%81","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/spring-boot\/springbootwebsocket%e6%89%93%e9%80%a0%e8%b6%85%e7%a8%b3%e5%ae%9a%e5%ae%9e%e6%97%b6%e7%9b%91%e6%8e%a7%ef%bc%81\/","title":{"rendered":"SpringBoot+WebSocket\u6253\u9020\u8d85\u7a33\u5b9a\u5b9e\u65f6\u76d1\u63a7\uff01"},"content":{"rendered":"\n<p><strong>\u544a\u522b\u6389\u7ebf\uff01SpringBoot+WebSocket\u6253\u9020\u8d85\u7a33\u5b9a\u5b9e\u65f6\u76d1\u63a7\uff01<\/strong><\/p>\n\n\n\n<p>\u5728\u73b0\u4ee3\u5316\u7684\u7cfb\u7edf\u67b6\u6784\u4e2d\uff0c\u5b9e\u65f6\u76d1\u63a7\u65e0\u7591\u662f\u63d0\u5347\u6548\u7387\u548c\u786e\u4fdd\u4e1a\u52a1\u987a\u7545\u8fd0\u884c\u7684\u5173\u952e\u4e4b\u4e00\u3002\u5c24\u5176\u662f\u5728\u8bbe\u5907\u5de1\u68c0\u7b49\u573a\u666f\u4e2d\uff0c\u5b9e\u65f6\u76d1\u63a7\u7cfb\u7edf\u53ef\u4ee5\u786e\u4fdd\u53ca\u65f6\u53d1\u73b0\u95ee\u9898\u5e76\u5feb\u901f\u54cd\u5e94\u3002\u672c\u6587\u5c06\u7ed3\u5408&nbsp;<strong>SpringBoot + WebSocket<\/strong>&nbsp;\u6280\u672f\uff0c\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u6784\u5efa\u4e00\u4e2a\u8d85\u7a33\u5b9a\u7684\u5b9e\u65f6\u76d1\u63a7\u7cfb\u7edf\uff0c\u5b9e\u65f6\u63a5\u6536\u8bbe\u5907\u5de1\u68c0\u7684\u5f02\u5e38\u4fe1\u606f\uff0c\u5e76\u5728\u76d1\u63a7\u754c\u9762\u4e0a\u53ca\u65f6\u66f4\u65b0\u8bbe\u5907\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u901a\u8fc7 WebSocket \u5b9e\u73b0\u670d\u52a1\u5668\u4e3b\u52a8\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u6d88\u606f\uff0c\u786e\u4fdd\u76d1\u63a7\u7cfb\u7edf\u80fd\u591f\u5b9e\u65f6\u53cd\u6620\u8bbe\u5907\u72b6\u6001\u7684\u53d8\u5316\u3002\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u6d88\u9632\u8bbe\u5907\u5de1\u68c0\u7cfb\u7edf\uff0c\u540e\u53f0\u670d\u52a1\u53ef\u5b9e\u65f6\u5411\u524d\u7aef\u9875\u9762\u53d1\u9001\u8bbe\u5907\u5f02\u5e38\u4fe1\u606f\uff0c\u5e76\u901a\u8fc7 WebSocket \u5c06\u8fd9\u4e9b\u4fe1\u606f\u63a8\u9001\u5230\u76d1\u63a7\u9875\u9762\uff0c\u5b9e\u65f6\u66f4\u65b0\u8bbe\u5907\u72b6\u6001\u3002<\/p>\n\n\n\n<p><strong>\u6574\u4f53\u67b6\u6784<\/strong><\/p>\n\n\n\n<p>\u5728\u672c\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u91c7\u7528&nbsp;<strong>Spring Boot<\/strong>&nbsp;\u4f5c\u4e3a\u540e\u7aef\u5f00\u53d1\u6846\u67b6\uff0c\u7ed3\u5408&nbsp;<strong>WebSocket<\/strong>&nbsp;\u5b9e\u73b0\u5b9e\u65f6\u6d88\u606f\u63a8\u9001\u3002\u524d\u7aef\u5219\u4f7f\u7528&nbsp;<strong>Vue<\/strong>&nbsp;\u548c&nbsp;<strong>Element<\/strong>\uff0c\u5e76\u901a\u8fc7&nbsp;<strong>WebSocket<\/strong>&nbsp;\u4e0e\u540e\u7aef\u5efa\u7acb\u5b9e\u65f6\u901a\u4fe1\u8fde\u63a5\u3002\u5f53\u8bbe\u5907\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u524d\u7aef\u4f1a\u6839\u636e WebSocket \u63a8\u9001\u7684\u6570\u636e\u52a8\u6001\u66f4\u65b0\u8bbe\u5907\u72b6\u6001\u3002<\/p>\n\n\n\n<p><strong>\u9879\u76ee\u7ed3\u6784<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u524d\u7aef\uff1a<\/strong>\u00a0\u57fa\u4e8e Vue \u548c Element \u5b9e\u73b0\u7684\u8bbe\u5907\u5de1\u68c0\u9875\u9762\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u9875\u9762\u67e5\u770b\u5404\u8bbe\u5907\u7684\u72b6\u6001\u5e76\u63a5\u6536\u5f02\u5e38\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u540e\u7aef\uff1a<\/strong>\u00a0\u4f7f\u7528 Spring Boot \u6784\u5efa WebSocket \u670d\u52a1\u7aef\uff0c\u5e76\u901a\u8fc7\u00a0<code>@ServerEndpoint<\/code>\u00a0\u6ce8\u89e3\u521b\u5efa WebSocket \u8fde\u63a5\uff0c\u5b9e\u73b0\u4e0e\u5ba2\u6237\u7aef\u7684\u5b9e\u65f6\u6d88\u606f\u63a8\u9001\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u5b9e\u73b0\u6b65\u9aa4<\/strong><\/p>\n\n\n\n<p><strong>\u914d\u7f6e Spring Boot WebSocket \u670d\u52a1\u7aef<\/strong><\/p>\n\n\n\n<p>\u914d\u7f6e&nbsp;<code>application.yml<\/code>&nbsp;\u914d\u7f6e\u6587\u4ef6<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code># application.yml \u914d\u7f6e<\/code><code>server:<\/code><code>&nbsp; port: 18801<\/code><code><br><\/code><code>mySocket:<\/code><code>&nbsp; myPwd: jae_123 &nbsp;# WebSocket \u5bc6\u7801\u6821\u9a8c<\/code><\/pre>\n\n\n\n<p>WebSocket \u914d\u7f6e\u7c7b<\/p>\n\n\n\n<p>\u5728 Spring Boot \u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u7c7b\u6765\u542f\u7528 WebSocket\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>package&nbsp;com.icoderoad.websocket;<\/code><code><br><\/code><code>import&nbsp;org.springframework.context.annotation.Bean;<\/code><code>import&nbsp;org.springframework.context.annotation.Configuration;<\/code><code>import&nbsp;org.springframework.web.socket.server.standard.ServerEndpointExporter;<\/code><code><br><\/code><code>@Configuration<\/code><code>public&nbsp;class&nbsp;WebSocketConfig&nbsp;{<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;\/**<\/code><code>&nbsp; &nbsp; &nbsp;* \u6ce8\u5165\u4e00\u4e2a ServerEndpointExporter, \u8be5 Bean \u4f1a\u81ea\u52a8\u6ce8\u518c\u4f7f\u7528&nbsp;@ServerEndpoint&nbsp;\u6ce8\u89e3\u58f0\u660e\u7684 WebSocket endpoint<\/code><code>&nbsp; &nbsp; &nbsp;*\/<\/code><code>&nbsp; &nbsp;&nbsp;@Bean<\/code><code>&nbsp; &nbsp;&nbsp;public&nbsp;ServerEndpointExporter serverEndpointExporter(){<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;new ServerEndpointExporter();<\/code><code>&nbsp; &nbsp; }<\/code><code>}<\/code><\/pre>\n\n\n\n<p>&nbsp;WebSocket \u670d\u52a1\u7aef\u7c7b<\/p>\n\n\n\n<p>WebSocket \u670d\u52a1\u7aef\u7c7b\u8d1f\u8d23\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5\u3001\u6d88\u606f\u53d1\u9001\u548c\u5173\u95ed\u8fde\u63a5\u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>package&nbsp;com.icoderoad.websocket;<\/code><code><br><\/code><code>import&nbsp;org.springframework.stereotype.Component;<\/code><code>import&nbsp;javax.websocket.*;<\/code><code>import&nbsp;javax.websocket.server.ServerEndpoint;<\/code><code>import&nbsp;java.io.IOException;<\/code><code>import&nbsp;java.util.concurrent.CopyOnWriteArraySet;<\/code><code>import&nbsp;java.util.concurrent.atomic.AtomicInteger;<\/code><code><br><\/code><code>@ServerEndpoint(\"\/webSocket\/{uid}\")<\/code><code>@Component<\/code><code>public&nbsp;class&nbsp;WebSocketServer&nbsp;{<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;AtomicInteger&nbsp;onlineNum&nbsp;=&nbsp;new&nbsp;AtomicInteger(0);<\/code><code>&nbsp; &nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;CopyOnWriteArraySet&lt;Session&gt; sessionPools =&nbsp;new&nbsp;CopyOnWriteArraySet&lt;&gt;();<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;@OnOpen<\/code><code>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;onOpen(Session session,&nbsp;@PathParam(\"uid\")&nbsp;String uid)&nbsp;{<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; sessionPools.add(session);<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; onlineNum.incrementAndGet();<\/code><code>&nbsp; &nbsp; }<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;@OnClose<\/code><code>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;onClose(Session session)&nbsp;{<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; sessionPools.remove(session);<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; onlineNum.decrementAndGet();<\/code><code>&nbsp; &nbsp; }<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;sendMessage(Session session, String message)&nbsp;throws&nbsp;IOException {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(session !=&nbsp;null) {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.getBasicRemote().sendText(message);<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; }<\/code><code>&nbsp; &nbsp; }<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;broadCastInfo(String message)&nbsp;throws&nbsp;IOException {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;(Session session : sessionPools) {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(session.isOpen()) {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendMessage(session, message);<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; }<\/code><code>&nbsp; &nbsp; }<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;@OnError<\/code><code>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;onError(Session session, Throwable throwable)&nbsp;{<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; throwable.printStackTrace();<\/code><code>&nbsp; &nbsp; }<\/code><code>}<\/code><\/pre>\n\n\n\n<p><strong>WebSocket Controller \u5904\u7406\u524d\u7aef\u8bf7\u6c42<\/strong><\/p>\n\n\n\n<p>\u63a7\u5236\u5668\u7c7b\u8d1f\u8d23\u63a5\u6536\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7 WebSocket \u5411\u6240\u6709\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u5e7f\u64ad\u6d88\u606f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>package&nbsp;com.icoderoad.websocket;<\/code><code><br><\/code><code>import&nbsp;org.springframework.beans.factory.annotation.Value;<\/code><code>import&nbsp;org.springframework.beans.factory.annotation.Autowired;<\/code><code>import&nbsp;org.springframework.web.bind.annotation.*;<\/code><code><br><\/code><code>import&nbsp;java.io.IOException;<\/code><code><br><\/code><code>@RestController<\/code><code>@RequestMapping(\"\/open\/socket\")<\/code><code>public&nbsp;class&nbsp;WebSocketController&nbsp;{<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;@Value(\"${mySocket.myPwd}\")<\/code><code>&nbsp; &nbsp;&nbsp;private&nbsp;String myPwd;<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;@Autowired<\/code><code>&nbsp; &nbsp;&nbsp;private&nbsp;WebSocketServer webSocketServer;<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;@PostMapping(\"\/onReceive\")<\/code><code>&nbsp; &nbsp;&nbsp;public&nbsp;void onReceive(String id, String pwd) throws IOException {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(pwd.equals(myPwd)) {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; webSocketServer.broadCastInfo(id); &nbsp;\/\/ \u5e7f\u64ad\u5f02\u5e38\u8bbe\u5907ID<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; }<\/code><code>&nbsp; &nbsp; }<\/code><code>}<\/code><\/pre>\n\n\n\n<p><strong>\u524d\u7aef\u5b9e\u73b0\uff1aVue + WebSocket<\/strong><\/p>\n\n\n\n<p>\u524d\u7aef\u9875\u9762\u91c7\u7528 Vue \u548c WebSocket \u8fdb\u884c\u5b9e\u65f6\u901a\u4fe1\u3002<\/p>\n\n\n\n<p>\u524d\u7aef HTML \u548c Vue \u4ee3\u7801https:\/\/wxa.wxs.qq.com\/tmpl\/oc\/base_tmpl.html<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;!DOCTYPE&nbsp;html&gt;<\/code><code>&lt;html&gt;<\/code><code>&lt;head&gt;<\/code><code>&nbsp; &nbsp;&nbsp;&lt;meta&nbsp;charset=\"utf-8\"\/&gt;<\/code><code>&nbsp; &nbsp;&nbsp;&lt;title&gt;\u5b9e\u65f6\u76d1\u63a7&lt;\/title&gt;<\/code><code>&lt;\/head&gt;<\/code><code>&lt;style&gt;<\/code><code>&nbsp; &nbsp;&nbsp;.item&nbsp;{<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;display: flex;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;border-bottom:&nbsp;1px&nbsp;solid&nbsp;#000000;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;justify-content: space-between;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;width:&nbsp;30%;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;line-height:&nbsp;50px;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;height:&nbsp;50px;<\/code><code>&nbsp; &nbsp; }<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;.item&nbsp;span:nth-child(2){<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;margin-right:&nbsp;10px;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;margin-top:&nbsp;15px;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;width:&nbsp;20px;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;height:&nbsp;20px;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;border-radius:&nbsp;50%;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;background:&nbsp;#55ff00;<\/code><code>&nbsp; &nbsp; }<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;.nowI&nbsp;{<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;background:&nbsp;#ff0000&nbsp;!important;<\/code><code>&nbsp; &nbsp; }<\/code><code>&lt;\/style&gt;<\/code><code>&lt;body&gt;<\/code><code>&nbsp; &nbsp;&nbsp;&lt;div&nbsp;id=\"app\"&gt;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;div&nbsp;v-for=\"item in list\"&nbsp;class=\"item\"&gt;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;span&gt;{{item.id}}.{{item.name}}&lt;\/span&gt;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;span&nbsp;:class=\"item.state == -1 ? 'nowI' : ''\"&gt;&lt;\/span&gt;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;\/div&gt;<\/code><code>&nbsp; &nbsp;&nbsp;&lt;\/div&gt;<\/code><code>&lt;\/body&gt;<\/code><code>&lt;script&nbsp;src=\".\/js\/vue.min.js\"&gt;&lt;\/script&gt;<\/code><code>&lt;script&nbsp;type=\"text\/javascript\"&gt;<\/code><code>&nbsp; &nbsp;&nbsp;var&nbsp;vm =&nbsp;new&nbsp;Vue({<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;el:&nbsp;\"#app\",<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;data: {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;list: [<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {id:&nbsp;1,&nbsp;name:&nbsp;'\u5f20\u4e09',&nbsp;state:&nbsp;1},<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {id:&nbsp;2,&nbsp;name:&nbsp;'\u674e\u56db',&nbsp;state:&nbsp;1},<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {id:&nbsp;3,&nbsp;name:&nbsp;'\u738b\u4e94',&nbsp;state:&nbsp;1},<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {id:&nbsp;4,&nbsp;name:&nbsp;'\u97e9\u6885\u6885',&nbsp;state:&nbsp;1},<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {id:&nbsp;5,&nbsp;name:&nbsp;'\u674e\u78ca',&nbsp;state:&nbsp;1},<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; }<\/code><code>&nbsp; &nbsp; });<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;var&nbsp;webSocket =&nbsp;new&nbsp;WebSocket(\"ws:\/\/localhost:18801\/webSocket\/\"&nbsp;+&nbsp;getUUID());<\/code><code><br><\/code><code>&nbsp; &nbsp; webSocket.onopen&nbsp;=&nbsp;function&nbsp;() {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;console.log(\"\u5df2\u8fde\u63a5\");<\/code><code>&nbsp; &nbsp; };<\/code><code><br><\/code><code>&nbsp; &nbsp; webSocket.onmessage&nbsp;=&nbsp;function&nbsp;(msg) {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;serverMsg = msg.data;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;t_id =&nbsp;parseInt(serverMsg);&nbsp;\/\/ \u670d\u52a1\u7aef\u53d1\u8fc7\u6765\u7684\u6d88\u606f\uff0cID\uff0cstring\u9700\u8f6c\u5316\u4e3aint\u7c7b\u578b\u624d\u80fd\u6bd4\u8f83<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;(var&nbsp;i =&nbsp;0; i &lt; vm.list.length; i++) {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;item = vm.list[i];<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(item.id&nbsp;== t_id) {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.state&nbsp;= -1;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vm.list.splice(i,&nbsp;1, item);<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;break;<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; }<\/code><code>&nbsp; &nbsp; };<\/code><code><br><\/code><code>&nbsp; &nbsp; webSocket.onclose&nbsp;=&nbsp;function&nbsp;() {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;console.log(\"websocket\u5df2\u5173\u95ed\");<\/code><code>&nbsp; &nbsp; };<\/code><code><br><\/code><code>&nbsp; &nbsp; webSocket.onerror&nbsp;=&nbsp;function&nbsp;() {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;console.log(\"websocket\u53d1\u751f\u4e86\u9519\u8bef\");<\/code><code>&nbsp; &nbsp; };<\/code><code><br><\/code><code>&nbsp; &nbsp;&nbsp;function&nbsp;getUUID() {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(\/[xy]\/g,&nbsp;function&nbsp;(c) {<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;r =&nbsp;Math.random() *&nbsp;16&nbsp;|&nbsp;0,<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = c ==&nbsp;'x'&nbsp;? r : (r &amp;&nbsp;0x3&nbsp;|&nbsp;0x8);<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;v.toString(16);<\/code><code>&nbsp; &nbsp; &nbsp; &nbsp; });<\/code><code>&nbsp; &nbsp; }<\/code><code>&lt;\/script&gt;<\/code><code>&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p><strong>\u6d4b\u8bd5\u4e0e\u7ed3\u679c<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6253\u5f00\u524d\u7aef\u9875\u9762\uff1a<\/strong>\u00a0\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u76d1\u63a7\u9875\u9762\uff0cWebSocket \u4f1a\u81ea\u52a8\u8fde\u63a5\u670d\u52a1\u5668\u5e76\u8f93\u51fa\u8fde\u63a5\u6210\u529f\u7684\u65e5\u5fd7\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u4ea4\u5f02\u5e38\u6570\u636e\uff1a<\/strong>\u00a0\u4f7f\u7528 Postman \u7b49\u63a5\u53e3\u6d4b\u8bd5\u5de5\u5177\u5411\u540e\u7aef\u53d1\u9001\u8bbe\u5907\u5f02\u5e38\u6570\u636e\uff0c\u8bbe\u5907 ID \u4e3a 3 \u7684\u8bbe\u5907\u72b6\u6001\u4f1a\u53d8\u4e3a\u7ea2\u8272\uff08\u8868\u793a\u5f02\u5e38\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7&nbsp;<strong>Spring Boot<\/strong>&nbsp;\u548c&nbsp;<strong>WebSocket<\/strong>&nbsp;\u7684\u7ed3\u5408\uff0c\u6210\u529f\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5b9e\u65f6\u76d1\u63a7\u7cfb\u7edf\u3002\u5728\u8fd9\u4e2a\u7cfb\u7edf\u4e2d\uff0c\u670d\u52a1\u7aef\u80fd\u591f\u4e3b\u52a8\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u6d88\u606f\uff0c\u786e\u4fdd\u76d1\u63a7\u6570\u636e\u5b9e\u65f6\u66f4\u65b0\u3002\u5229\u7528 WebSocket \u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u7a33\u5b9a\u3001\u4f4e\u5ef6\u8fdf\u7684\u5b9e\u65f6\u901a\u4fe1\uff0c\u4f7f\u5f97\u8bbe\u5907\u5f02\u5e38\u80fd\u591f\u5feb\u901f\u4f20\u9012\u5230\u76d1\u63a7\u5e73\u53f0\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u5b89\u5168\u6027\u3002\u8fd9\u79cd\u57fa\u4e8e WebSocket \u7684\u5b9e\u65f6\u76d1\u63a7\u65b9\u6848\uff0c\u4e0d\u4ec5\u9002\u7528\u4e8e\u8bbe\u5907\u5de1\u68c0\u573a\u666f\uff0c\u8fd8\u53ef\u4ee5\u6269\u5c55\u5e94\u7528\u5230\u5176\u4ed6\u9700\u8981\u5b9e\u65f6\u53cd\u9988\u7684\u7cfb\u7edf\u4e2d\u3002<\/p>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u544a\u522b\u6389\u7ebf\uff01SpringBoot+WebSocket\u6253\u9020\u8d85\u7a33\u5b9a\u5b9e\u65f6\u76d1\u63a7\uff01 \u5728\u73b0\u4ee3\u5316\u7684\u7cfb\u7edf\u67b6\u6784\u4e2d\uff0c\u5b9e\u65f6\u76d1\u63a7\u65e0\u7591\u662f\u63d0\u5347\u6548\u7387\u548c\u786e\u4fdd\u4e1a\u52a1\u987a\u7545 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[7,37],"tags":[],"class_list":["post-6659","post","type-post","status-publish","format-standard","hentry","category-spring-boot","category-websocket"],"post_thumbnail_image":"","content_first_image":null,"post_medium_image_300":"","post_thumbnail_image_624":"","post_frist_image":null,"post_medium_image":"","post_large_image":"","post_full_image":"","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Spring Boot","post_date":"2025-09-29","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":163,"next_post_id":6669,"next_post_title":"\u4ece 0 \u5230 1\uff0c\u5b9e\u6218 SpringBoot \u6253\u9020\u8f7b\u91cf\u89c6\u9891\u63a8\u62c9\u6d41\u7cfb\u7edf","previous_post_id":6655,"previous_post_title":"\u5206\u5e03\u5f0f\u7cfb\u7edf Token \u7eed\u671f\u8e29\u5751\u5b9e\u5f55\uff1a\u81ea\u52a8\u7eed\u671f + \u8bbe\u5907\u7ed1\u5b9a\u8fd9\u6837\u505a\u624d\u5b89\u5168","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6659","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=6659"}],"version-history":[{"count":2,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6659\/revisions"}],"predecessor-version":[{"id":6662,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6659\/revisions\/6662"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}