{"id":78025,"date":"2026-02-25T22:41:37","date_gmt":"2026-02-25T14:41:37","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78025.html"},"modified":"2026-02-25T22:41:37","modified_gmt":"2026-02-25T14:41:37","slug":"springbootvuenettywebsocketwebrtc-%e5%ae%9e%e7%8e%b0%e8%a7%86%e9%a2%91%e8%81%8a%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78025.html","title":{"rendered":"SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929"},"content":{"rendered":"<p>\u5b9e\u65f6\u97f3\u89c6\u9891\u804a\u5929\u662f\u5f53\u4e0b\u793e\u4ea4\u3001\u5728\u7ebf\u534f\u4f5c\u7c7b\u5e94\u7528\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00&#xff0c;WebRTC&#xff08;Web Real-Time Communication&#xff09;\u4f5c\u4e3a\u6d4f\u89c8\u5668\u539f\u751f\u652f\u6301\u7684\u5b9e\u65f6\u901a\u4fe1\u6280\u672f&#xff0c;\u80fd\u8ba9\u524d\u7aef\u65e0\u9700\u63d2\u4ef6\u5373\u53ef\u5b9e\u73b0\u70b9\u5bf9\u70b9\u97f3\u89c6\u9891\u4f20\u8f93&#xff1b;\u800c Netty \u4f5c\u4e3a\u9ad8\u6027\u80fd\u7684 Java NIO \u6846\u67b6&#xff0c;\u53ef\u63d0\u4f9b\u7a33\u5b9a\u7684 WebSocket \u901a\u4fe1\u901a\u9053&#xff0c;\u914d\u5408 SpringBoot \u7684\u5feb\u901f\u5f00\u53d1\u80fd\u529b\u548c Vue \u7684\u524d\u7aef\u5de5\u7a0b\u5316\u80fd\u529b&#xff0c;\u80fd\u5feb\u901f\u642d\u5efa\u4e00\u5957\u5b8c\u6574\u7684\u89c6\u9891\u804a\u5929\u7cfb\u7edf\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u57fa\u4e8e\u8fd9\u4e9b\u6280\u672f\u6808\u5b9e\u73b0\u4e00\u5bf9\u4e00\u89c6\u9891\u804a\u5929\u529f\u80fd\u3002<\/p>\n<h2>\u4e00\u3001SpringBoot&#043;Vue&#043;Netty&#043;WebSocket&#043;WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929<\/h2>\n<table>\n<tr>\u6280\u672f\u6808\u6838\u5fc3\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>SpringBoot<\/td>\n<td>\u540e\u7aef\u5feb\u901f\u5f00\u53d1\u6846\u67b6&#xff0c;\u6574\u5408 Netty\u3001\u914d\u7f6e WebSocket&#xff0c;\u63d0\u4f9b\u63a5\u53e3\u652f\u6491<\/td>\n<\/tr>\n<tr>\n<td>Vue<\/td>\n<td>\u524d\u7aef\u5de5\u7a0b\u5316\u6846\u67b6&#xff0c;\u8d1f\u8d23\u97f3\u89c6\u9891\u754c\u9762\u6e32\u67d3\u3001WebRTC API \u8c03\u7528<\/td>\n<\/tr>\n<tr>\n<td>Netty<\/td>\n<td>\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6&#xff0c;\u5b9e\u73b0 WebSocket \u670d\u52a1\u7aef&#xff0c;\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u4fe1\u4ee4\u8f6c\u53d1<\/td>\n<\/tr>\n<tr>\n<td>WebSocket<\/td>\n<td>\u5168\u53cc\u5de5\u901a\u4fe1\u534f\u8bae&#xff0c;\u7528\u4e8e\u524d\u7aef\u548c\u540e\u7aef\u4e4b\u95f4\u7684\u4fe1\u4ee4&#xff08;\u5982\u547c\u53eb\u3001\u5e94\u7b54\u3001ICE \u5019\u9009&#xff09;\u4f20\u8f93<\/td>\n<\/tr>\n<tr>\n<td>WebRTC<\/td>\n<td>\u5b9e\u65f6\u901a\u4fe1\u6807\u51c6&#xff0c;\u63d0\u4f9b\u97f3\u89c6\u9891\u91c7\u96c6\u3001\u7f16\u7801\u3001\u70b9\u5bf9\u70b9\u4f20\u8f93\u80fd\u529b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6574\u4e2a\u89c6\u9891\u804a\u5929\u7cfb\u7edf\u5206\u4e3a\u4e09\u5c42&#xff0c;\u6838\u5fc3\u662f\u4fe1\u4ee4\u8f6c\u53d1\u548c\u70b9\u5bf9\u70b9\u97f3\u89c6\u9891\u4f20\u8f93&#xff1a;<\/p>\n<li>\u524d\u7aef\u5c42&#xff08;Vue&#xff09;&#xff1a;\u91c7\u96c6\u97f3\u89c6\u9891\u6d41\u3001\u521b\u5efa WebRTC \u8fde\u63a5\u3001\u901a\u8fc7 WebSocket \u53d1\u9001 \/ \u63a5\u6536\u4fe1\u4ee4&#xff1b;<\/li>\n<li>\u901a\u4fe1\u5c42&#xff08;Netty&#043;WebSocket&#xff09;&#xff1a;\u7ef4\u62a4\u5ba2\u6237\u7aef\u8fde\u63a5\u3001\u8f6c\u53d1\u4fe1\u4ee4&#xff08;\u5982\u547c\u53eb\u8bf7\u6c42\u3001ICE \u5019\u9009\u4fe1\u606f&#xff09;&#xff1b;<\/li>\n<li>\u540e\u7aef\u5c42&#xff08;SpringBoot&#xff09;&#xff1a;\u6574\u5408 Netty\u3001\u7ba1\u7406\u7528\u6237\u8fde\u63a5\u72b6\u6001\u3001\u63d0\u4f9b\u57fa\u7840\u914d\u7f6e\u652f\u6491\u3002<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"671\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260225144136-699f0a2076761.png\" width=\"1250\" \/><\/p>\n<p>WebRTC \u662f\u4ec0\u4e48:\u662f\u6d4f\u89c8\u5668\u5185\u7f6e\u7684\u5b9e\u65f6\u901a\u4fe1\u6280\u672f&#xff0c;\u80fd\u8ba9\u7f51\u9875\u76f4\u63a5\u5b9e\u73b0\u97f3\u89c6\u9891\u901a\u8bdd\u3001\u6570\u636e\u4f20\u8f93&#xff0c;\u65e0\u9700\u5b89\u88c5\u63d2\u4ef6\u3002<\/p>\n<p>ICE \u662f\u4ec0\u4e48&#xff1a;ICE&#xff08;Interactive Connectivity Establishment&#xff09;\u662f WebRTC \u4e2d\u7528\u4e8e\u89e3\u51b3 NAT \u7a7f\u900f&#xff08;\u7b80\u5355\u8bf4\u5c31\u662f\u8ba9\u4e0d\u540c\u7f51\u7edc\u4e0b\u7684\u8bbe\u5907\u80fd\u627e\u5230\u5f7c\u6b64&#xff09;\u7684\u6846\u67b6&#xff0c;\u800c iceServers \u5c31\u662f\u7ed9 ICE \u63d0\u4f9b \u201c\u8f85\u52a9\u670d\u52a1\u5668\u201d \u7684\u914d\u7f6e\u3002<\/p>\n<p>STUN \u670d\u52a1\u5668&#xff1a;STUN&#xff08;Session Traversal Utilities for NAT&#xff09;&#xff0c;\u76f4\u8bd1\u662f \u201cNAT \u4f1a\u8bdd\u7a7f\u900f\u5de5\u5177\u201d&#xff0c;\u5b83\u662f\u4e00\u79cd<span style=\"color:#1f2329\"><span>\u8f7b\u91cf\u7ea7\u7684\u7f51\u7edc\u670d\u52a1\u5668<\/span><\/span>&#xff0c;\u6838\u5fc3\u4f5c\u7528\u662f&#xff1a;\u5e2e\u52a9\u5904\u4e8e NAT&#xff08;\u7f51\u7edc\u5730\u5740\u8f6c\u6362&#xff09;\u540e\u7684\u8bbe\u5907&#xff08;\u6bd4\u5982\u4f60\u7684\u7535\u8111 \/ \u624b\u673a&#xff09;&#xff0c;\u83b7\u53d6\u81ea\u5df1\u7684<span style=\"color:#1f2329\"><span>\u516c\u7f51 IP &#043; \u7aef\u53e3<\/span><\/span>&#xff0c;\u4ee5\u53ca NAT \u8bbe\u5907\u7684\u7c7b\u578b&#xff0c;\u4ece\u800c\u8ba9\u4e0d\u540c NAT \u540e\u7684\u8bbe\u5907\u80fd\u627e\u5230\u5f7c\u6b64&#xff0c;\u5efa\u7acb\u70b9\u5bf9\u70b9&#xff08;P2P&#xff09;\u8fde\u63a5\u3002<\/p>\n<h5 id=\"GeM1-1771854089358\" style=\"text-align:left\"><span style=\"color:#1f2329\">\u5148\u8865\u4e2a\u524d\u63d0&#xff1a;\u4e3a\u4ec0\u4e48\u9700\u8981 STUN&#xff1f;<\/span><\/h5>\n<p>\u6211\u4eec\u65e5\u5e38\u7528\u7684\u7f51\u7edc&#xff08;\u6bd4\u5982\u5bb6\u91cc\u7684\u5bbd\u5e26\u3001\u516c\u53f8\u7684\u5185\u7f51&#xff09;&#xff0c;\u8bbe\u5907\u62ff\u5230\u7684\u90fd\u662f\u5185\u7f51 IP&#xff08;\u5982 192.168.1.100&#xff09;&#xff0c;\u4e0d\u662f\u516c\u7f51 IP\u3002\u5f53\u4e24\u4e2a\u5185\u7f51\u8bbe\u5907\u8981\u76f4\u63a5\u901a\u4fe1&#xff08;\u6bd4\u5982 WebRTC \u97f3\u89c6\u9891\u901a\u8bdd&#xff09;&#xff0c;\u5b83\u4eec\u4e0d\u77e5\u9053\u5bf9\u65b9\u7684\u516c\u7f51\u5730\u5740&#xff0c;\u5c31\u50cf\u4e24\u4e2a\u4eba\u5728\u4e0d\u540c\u7684\u5c0f\u533a\u91cc&#xff0c;\u53ea\u77e5\u9053\u81ea\u5df1\u7684\u95e8\u724c\u53f7&#xff0c;\u5374\u4e0d\u77e5\u9053\u5c0f\u533a\u7684\u5730\u5740\u548c\u5bf9\u5916\u7684\u51fa\u5165\u53e3 \u2014\u2014STUN \u670d\u52a1\u5668\u5c31\u662f\u5e2e\u5b83\u4eec\u67e5 \u201c\u5c0f\u533a\u5730\u5740 &#043; \u51fa\u5165\u53e3\u201d \u7684\u5de5\u5177\u3002<\/p>\n<p>\u7528\u4e00\u4e2a \u201c\u6253\u7535\u8bdd\u201d \u7684\u4f8b\u5b50\u89e3\u91ca&#xff1a;<\/p>\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>\u8bbe\u5907 A&#xff08;\u5185\u7f51&#xff09;<\/span><\/span> \u5411 STUN \u670d\u52a1\u5668\u53d1\u9001\u4e00\u4e2a\u8bf7\u6c42&#xff1a;\u201c\u8bf7\u544a\u8bc9\u6211&#xff0c;\u4f60\u770b\u5230\u7684\u6211\u7684\u5730\u5740\u548c\u7aef\u53e3\u662f\u4ec0\u4e48&#xff1f;\u201d<\/li>\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>STUN \u670d\u52a1\u5668<\/span><\/span> \u6536\u5230\u8bf7\u6c42\u540e&#xff0c;\u4f1a\u8bb0\u5f55\u4e0b\u8bf7\u6c42\u6765\u6e90\u7684<span style=\"color:#1f2329\"><span>\u516c\u7f51 IP &#043; \u7aef\u53e3<\/span><\/span>&#xff08;\u8fd9\u662f NAT \u8bbe\u5907\u7ed9\u8bbe\u5907 A \u5206\u914d\u7684\u5bf9\u5916\u7aef\u53e3&#xff09;&#xff0c;\u7136\u540e\u628a\u8fd9\u4e2a\u4fe1\u606f\u8fd4\u56de\u7ed9\u8bbe\u5907 A\u3002<\/li>\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>\u8bbe\u5907 A<\/span><\/span> \u62ff\u5230\u81ea\u5df1\u7684\u516c\u7f51\u5730\u5740\u540e&#xff0c;\u901a\u8fc7\u4fe1\u4ee4\u670d\u52a1\u5668&#xff08;\u6bd4\u5982 WebSocket&#xff09;\u628a\u8fd9\u4e2a\u5730\u5740\u544a\u8bc9\u8bbe\u5907 B&#xff1b;\u540c\u7406&#xff0c;\u8bbe\u5907 B \u4e5f\u901a\u8fc7 STUN \u670d\u52a1\u5668\u62ff\u5230\u81ea\u5df1\u7684\u516c\u7f51\u5730\u5740\u5e76\u544a\u8bc9\u8bbe\u5907 A\u3002<\/li>\n<li style=\"text-align:left\">\u6700\u540e&#xff0c;\u8bbe\u5907 A \u548c\u8bbe\u5907 B \u5c31\u53ef\u4ee5\u7528\u5f7c\u6b64\u7684\u516c\u7f51\u5730\u5740&#xff0c;\u76f4\u63a5\u5efa\u7acb P2P \u8fde\u63a5\u3002<\/li>\n<h2 id=\"A5RF-1771854579837\">\u4e8c\u3001\u670d\u52a1\u7aef\u5f00\u53d1<\/h2>\n<h3>2.1 \u9879\u76ee\u642d\u5efa\u4e0e\u4f9d\u8d56\u914d\u7f6e<\/h3>\n<p>&lt;!&#8211; SpringBoot \u6838\u5fc3 &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter&lt;\/artifactId&gt;<br \/>\n&lt;\/dependency&gt;<br \/>\n&lt;!&#8211; Netty WebSocket \u4f9d\u8d56 &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;io.netty&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;netty-all&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;4.1.94.Final&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<br \/>\n&lt;!&#8211; JSON \u89e3\u6790 &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;fastjson&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;2.0.25&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<\/p>\n<p>\u4f9d\u8d56\u4f5c\u7528&#xff1a;Netty \u662f\u6838\u5fc3&#xff0c;\u8d1f\u8d23\u7f51\u7edc\u901a\u4fe1&#xff1b;FastJSON \u7528\u4e8e\u5904\u7406\u524d\u540e\u7aef\u4f20\u8f93\u7684 JSON \u683c\u5f0f\u4fe1\u4ee4<\/p>\n<h3>2.2\u00a0\u6838\u5fc3\u5b9e\u4f53\u7c7b\u4e0e\u6d88\u606f\u5904\u7406\u5668<\/h3>\n<p>1.\u521b\u5efa\u6d88\u606f\u5b9e\u4f53\u7c7b Message.java<\/p>\n<p>\u9700\u6c42&#xff1a;\u4fe1\u4ee4\u6d88\u606f\u9700\u8981\u5305\u542b\u201c\u6d88\u606f\u7c7b\u578b\u201d\u201c\u53d1\u9001\u65b9ID\u201d\u201c\u63a5\u6536\u65b9ID\u201d\u201c\u6d88\u606f\u5185\u5bb9\u201d&#xff0c;\u6240\u4ee5\u5b9a\u4e49\u5bf9\u5e94\u7684\u5c5e\u6027<\/p>\n<p>public class Message {<br \/>\n    \/\/ \u6d88\u606f\u7c7b\u578b&#xff1a;register(\u6ce8\u518c)\u3001call(\u547c\u53eb)\u3001answer(\u5e94\u7b54)\u3001ice(ICE\u5019\u9009)<br \/>\n    private String type;<br \/>\n    \/\/ \u53d1\u9001\u65b9ID<br \/>\n    private String from;<br \/>\n    \/\/ \u63a5\u6536\u65b9ID<br \/>\n    private String to;<br \/>\n    \/\/ \u6d88\u606f\u5185\u5bb9&#xff08;SDP\/ICE \u6570\u636e&#xff09;<br \/>\n    private String data;<br \/>\n}<\/p>\n<p>2.\u521b\u5efa Netty WebSocket \u5904\u7406\u5668<\/p>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u5904\u7406 WebSocket \u8fde\u63a5\u7684\u5efa\u7acb\u3001\u6d88\u606f\u63a5\u6536\u4e0e\u8f6c\u53d1\u3001\u8fde\u63a5\u65ad\u5f00\u7b49\u4e8b\u4ef6&#xff0c;\u662f\u670d\u52a1\u7aef\u7684\u6838\u5fc3\u903b\u8f91<\/p>\n<p>import com.alibaba.fastjson.JSON;<br \/>\nimport com.qcby.springboot.entity.Message;<br \/>\nimport io.netty.channel.Channel;<br \/>\nimport io.netty.channel.ChannelHandler;<br \/>\nimport io.netty.channel.ChannelHandlerContext;<br \/>\nimport io.netty.channel.SimpleChannelInboundHandler;<br \/>\nimport io.netty.handler.codec.http.websocketx.TextWebSocketFrame;<br \/>\nimport io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;<br \/>\nimport org.springframework.context.annotation.Configuration;<br \/>\nimport java.util.Map;<br \/>\nimport java.util.concurrent.ConcurrentHashMap;<\/p>\n<p>\/**<br \/>\n * \u63cf\u8ff0:<br \/>\n *\/<br \/>\n&#064;Configuration<br \/>\n&#064;ChannelHandler.Sharable<br \/>\npublic class WebSocketHandler extends SimpleChannelInboundHandler&lt;TextWebSocketFrame&gt; {<br \/>\n    \/\/ \u5b58\u50a8\u7528\u6237ID\u4e0eChannel\u7684\u6620\u5c04&#xff08;\u7ebf\u7a0b\u5b89\u5168&#xff09;<br \/>\n    public static final ConcurrentHashMap&lt;String, Channel&gt; USER_CHANNEL_MAP &#061; new ConcurrentHashMap&lt;&gt;();<\/p>\n<p>    &#064;Override<br \/>\n    public void channelActive(ChannelHandlerContext ctx) throws Exception {<br \/>\n        System.out.println(&#034;\u4e0e\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5&#xff0c;\u901a\u9053\u5f00\u542f&#xff01;&#034;);<br \/>\n    }<br \/>\n    \/**<br \/>\n     * \u5904\u7406\u63a5\u6536\u5230\u7684\u6587\u672c\u6d88\u606f<br \/>\n     *\/<br \/>\n    &#064;Override<br \/>\n    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {<br \/>\n        \/\/ \u89e3\u6790JSON\u6d88\u606f<br \/>\n        String text &#061; msg.text();<br \/>\n        Message message &#061; JSON.parseObject(text, Message.class);<br \/>\n        System.out.println(&#034;\u6536\u5230\u6d88\u606f&#xff1a;&#034; &#043; text);<\/p>\n<p>        switch (message.getType()) {<br \/>\n            case &#034;register&#034;:<br \/>\n                \/\/ \u6ce8\u518c\u7528\u6237ID\u4e0eChannel\u7684\u6620\u5c04<br \/>\n                USER_CHANNEL_MAP.put(message.getFrom(), ctx.channel());<br \/>\n                System.out.println(&#034;\u7528\u6237 &#034; &#043; message.getFrom() &#043; &#034; \u6ce8\u518c\u6210\u529f&#034;);<br \/>\n                break;<br \/>\n            case &#034;call&#034;:<br \/>\n            case &#034;answer&#034;:<br \/>\n            case &#034;ice&#034;:<br \/>\n                \/\/ \u8f6c\u53d1\u6d88\u606f\u5230\u63a5\u6536\u65b9<br \/>\n                Channel targetChannel &#061; USER_CHANNEL_MAP.get(message.getTo());<br \/>\n                if (targetChannel !&#061; null &amp;&amp; targetChannel.isActive()) {<br \/>\n                    targetChannel.writeAndFlush(new TextWebSocketFrame(text));<br \/>\n                    System.out.println(&#034;\u8f6c\u53d1\u6d88\u606f\u5230\u7528\u6237 &#034; &#043; message.getTo());<br \/>\n                } else {<br \/>\n                    System.out.println(&#034;\u7528\u6237 &#034; &#043; message.getTo() &#043; &#034; \u4e0d\u5728\u7ebf&#034;);<br \/>\n                }<br \/>\n                break;<br \/>\n            default:<br \/>\n                System.out.println(&#034;\u672a\u77e5\u6d88\u606f\u7c7b\u578b&#xff1a;&#034; &#043; message.getType());<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u5904\u7406\u8fde\u63a5\u65ad\u5f00\u4e8b\u4ef6<br \/>\n     *\/<br \/>\n    &#064;Override<br \/>\n    public void channelInactive(ChannelHandlerContext ctx) throws Exception {<br \/>\n        System.out.println(&#034;\u4e0e\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5&#xff0c;\u901a\u9053\u5173\u95ed&#xff01;&#034;);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u5904\u7406\u5f02\u5e38<br \/>\n     *\/<br \/>\n    &#064;Override<br \/>\n    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {<br \/>\n        System.out.println(&#034;\u8fde\u63a5\u5f02\u5e38&#xff1a;&#034; &#043; cause.getMessage());<br \/>\n        USER_CHANNEL_MAP.entrySet().removeIf(entry -&gt; entry.getValue() &#061;&#061; ctx.channel());<br \/>\n        ctx.close();<br \/>\n    }<br \/>\n}<\/p>\n<h2>\u4e09\u3001Netty \u670d\u52a1\u542f\u52a8\u7c7b\u4e0e SpringBoot \u542f\u52a8\u7c7b<\/h2>\n<p>\u9700\u6c42&#xff1a;\u9700\u8981\u5728 SpringBoot \u9879\u76ee\u542f\u52a8\u65f6&#xff0c;\u81ea\u52a8\u542f\u52a8 Netty \u7684 WebSocket \u670d\u52a1&#xff0c;\u76d1\u542c\u6307\u5b9a\u7aef\u53e3&#xff08;8081&#xff09;<\/p>\n<p>import io.netty.bootstrap.ServerBootstrap;<br \/>\nimport io.netty.channel.ChannelFuture;<br \/>\nimport io.netty.channel.ChannelInitializer;<br \/>\nimport io.netty.channel.ChannelOption;<br \/>\nimport io.netty.channel.EventLoopGroup;<br \/>\nimport io.netty.channel.nio.NioEventLoopGroup;<br \/>\nimport io.netty.channel.socket.SocketChannel;<br \/>\nimport io.netty.channel.socket.nio.NioServerSocketChannel;<br \/>\nimport io.netty.handler.codec.http.HttpObjectAggregator;<br \/>\nimport io.netty.handler.codec.http.HttpServerCodec;<br \/>\nimport io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;<br \/>\nimport io.netty.handler.stream.ChunkedWriteHandler;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.context.annotation.Configuration;<br \/>\n\/**<br \/>\nNetty WebSocket \u670d\u52a1\u7aef*\/<br \/>\n&#064;Configurationpublic<br \/>\nclass NettyWebSocketServer {<br \/>\n    &#064;Autowired<br \/>\n    private WebSocketHandler coordinationSocketHandler;<br \/>\n    public void start() throws Exception {<br \/>\n            EventLoopGroup bossGroup &#061; new NioEventLoopGroup();<br \/>\n            EventLoopGroup group &#061; new NioEventLoopGroup();<br \/>\n            try {<br \/>\n                ServerBootstrap sb &#061; new ServerBootstrap();<br \/>\n                sb.option(ChannelOption.SO_BACKLOG, 1024);<br \/>\n                sb.group(group, bossGroup) \/\/ \u7ed1\u5b9a\u7ebf\u7a0b\u6c60<br \/>\n                    .channel(NioServerSocketChannel.class) \/\/ \u6307\u5b9a\u4f7f\u7528\u7684channel<br \/>\n                    .localAddress(8004)\/\/ \u7ed1\u5b9a\u76d1\u542c\u7aef\u53e3<br \/>\n                    .childHandler(new ChannelInitializer&lt;SocketChannel&gt;() { \/\/ \u7ed1\u5b9a\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\u5019\u89e6\u53d1\u64cd\u4f5c<br \/>\n                        &#064;Override<br \/>\n                        protected void initChannel(SocketChannel ch) throws Exception {<br \/>\n                        \/\/websocket\u534f\u8bae\u672c\u8eab\u662f\u57fa\u4e8ehttp\u534f\u8bae\u7684&#xff0c;\u6240\u4ee5\u8fd9\u8fb9\u4e5f\u8981\u4f7f\u7528http\u89e3\u7f16\u7801\u5668<br \/>\n                        ch.pipeline().addLast(new HttpServerCodec());<br \/>\n                        \/\/\u4ee5\u5757\u7684\u65b9\u5f0f\u6765\u5199\u7684\u5904\u7406\u5668<br \/>\n                        ch.pipeline().addLast(new ChunkedWriteHandler());<br \/>\n                        ch.pipeline().addLast(new HttpObjectAggregator(8192));<br \/>\n                        ch.pipeline().addLast(new WebSocketServerProtocolHandler(&#034;\/ws&#034;, &#034;WebSocket&#034;, true, 65536 * 10));<br \/>\n                        ch.pipeline().addLast(coordinationSocketHandler);\/\/\u81ea\u5b9a\u4e49\u6d88\u606f\u5904\u7406\u7c7b<br \/>\n                }<br \/>\n            });<br \/>\n                ChannelFuture cf &#061; sb.bind().sync(); \/\/ \u670d\u52a1\u5668\u5f02\u6b65\u521b\u5efa\u7ed1\u5b9a<br \/>\n                cf.channel().closeFuture().sync(); \/\/ \u5173\u95ed\u670d\u52a1\u5668\u901a\u9053<br \/>\n    } finally {<br \/>\n            group.shutdownGracefully().sync(); \/\/ \u91ca\u653e\u7ebf\u7a0b\u6c60\u8d44\u6e90<br \/>\n            bossGroup.shutdownGracefully().sync();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">&#064;PostConstruct \u548c &#064;PreDestroy&#xff1a;Spring \u7684\u6ce8\u89e3&#xff0c;\u5206\u522b\u5728\u9879\u76ee\u542f\u52a8\u540e\u548c\u9500\u6bc1\u524d\u6267\u884c&#xff0c;\u5b9e\u73b0 Netty \u670d\u52a1\u7684\u81ea\u52a8\u542f\u505c<\/li>\n<li style=\"text-align:left\">EventLoopGroup \u7ebf\u7a0b\u7ec4&#xff1a;bossGroup \u8d1f\u8d23\u63a5\u6536\u8fde\u63a5&#xff0c;workerGroup \u8d1f\u8d23\u5904\u7406\u8bfb\u5199&#xff0c;\u662f Netty \u9ad8\u6027\u80fd\u7684\u6838\u5fc3<\/li>\n<li style=\"text-align:left\">\u901a\u9053\u5904\u7406\u5668\u94fe&#xff1a;\u6309\u987a\u5e8f\u6dfb\u52a0\u5904\u7406\u5668&#xff0c;WebSocket \u57fa\u4e8e HTTP \u63e1\u624b&#xff0c;\u6240\u4ee5\u5148\u6dfb\u52a0 HTTP \u76f8\u5173\u5904\u7406\u5668&#xff0c;\u6700\u540e\u6dfb\u52a0\u81ea\u5b9a\u4e49\u5904\u7406\u5668<\/li>\n<\/ul>\n<p>import com.qcby.springboot.commun.NettyWebSocketServer;<br \/>\nimport org.mybatis.spring.annotation.MapperScan;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.boot.CommandLineRunner;<br \/>\nimport org.springframework.boot.SpringApplication;<br \/>\nimport org.springframework.boot.autoconfigure.SpringBootApplication;<br \/>\n&#064;SpringBootApplication<br \/>\n&#064;MapperScan(&#034;com.qcby.springboot.dao&#034;)<br \/>\npublic class Application implements CommandLineRunner {<br \/>\npublic static void main(String[] args) {<br \/>\n    SpringApplication.run(Application.class, args);<br \/>\n}<\/p>\n<p>    &#064;Autowired<br \/>\n    private NettyWebSocketServer nettyServer;<br \/>\n    &#064;Override<br \/>\n    public void run(String&#8230; args) throws Exception {<br \/>\n        nettyServer.start();<br \/>\n    }<\/p>\n<p>}<\/p>\n<h2 style=\"background-color:transparent\">\u56db\u3001Vue\u524d\u7aef\u5f00\u53d1<\/h2>\n<h3 id=\"Zh9K-1771854826621\">4.1 \u7f16\u8f91\u6a21\u7248template<\/h3>\n<p>&lt;template&gt;<br \/>\n  &lt;div class&#061;&#034;container&#034;&gt;<br \/>\n    &lt;h2&gt;WebRTC \u89c6\u9891\u804a\u5929&lt;\/h2&gt;<\/p>\n<p>    &lt;!&#8211; 1. \u7528\u6237ID\u8f93\u5165\u4e0e\u8fde\u63a5\u670d\u52a1\u5668\u533a\u57df &#8211;&gt;<br \/>\n    &lt;div class&#061;&#034;input-group&#034;&gt;<br \/>\n      &lt;input<br \/>\n        v-model&#061;&#034;userId&#034;<br \/>\n        placeholder&#061;&#034;\u8f93\u5165\u4f60\u7684\u7528\u6237ID&#xff08;\u5982user1&#xff09;&#034;<br \/>\n        type&#061;&#034;text&#034;<br \/>\n      \/&gt;<br \/>\n      &lt;button &#064;click&#061;&#034;connect&#034;&gt;\u8fde\u63a5\u670d\u52a1\u5668&lt;\/button&gt;<br \/>\n    &lt;\/div&gt;<\/p>\n<p>    &lt;!&#8211; 2. \u547c\u53eb\u529f\u80fd\u533a\u57df&#xff08;\u4ec5\u8fde\u63a5\u6210\u529f\u540e\u663e\u793a&#xff09; &#8211;&gt;<br \/>\n    &lt;div class&#061;&#034;input-group&#034; v-if&#061;&#034;socketConnected&#034;&gt;<br \/>\n      &lt;input<br \/>\n        v-model&#061;&#034;targetUserId&#034;<br \/>\n        placeholder&#061;&#034;\u8f93\u5165\u5bf9\u65b9\u7528\u6237ID&#xff08;\u5982user2&#xff09;&#034;<br \/>\n        type&#061;&#034;text&#034;<br \/>\n      \/&gt;<br \/>\n      &lt;button &#064;click&#061;&#034;call&#034;&gt;\u53d1\u8d77\u89c6\u9891\u547c\u53eb&lt;\/button&gt;<br \/>\n    &lt;\/div&gt;<\/p>\n<p>    &lt;!&#8211; 3. \u89c6\u9891\u5c55\u793a\u533a\u57df &#8211;&gt;<br \/>\n    &lt;div class&#061;&#034;video-container&#034;&gt;<br \/>\n      &lt;div class&#061;&#034;video-item&#034;&gt;<br \/>\n        &lt;p&gt;\u672c\u5730\u89c6\u9891&lt;\/p&gt;<br \/>\n        &lt;!&#8211; muted&#xff1a;\u672c\u5730\u89c6\u9891\u9759\u97f3&#xff0c;\u907f\u514d\u56de\u58f0&#xff1b;autoplay&#xff1a;\u81ea\u52a8\u64ad\u653e &#8211;&gt;<br \/>\n        &lt;video ref&#061;&#034;localVideo&#034; autoplay muted&gt;&lt;\/video&gt;<br \/>\n      &lt;\/div&gt;<br \/>\n      &lt;div class&#061;&#034;video-item&#034;&gt;<br \/>\n        &lt;p&gt;\u8fdc\u7a0b\u89c6\u9891&lt;\/p&gt;<br \/>\n        &lt;video ref&#061;&#034;remoteVideo&#034; autoplay&gt;&lt;\/video&gt;<br \/>\n      &lt;\/div&gt;<br \/>\n    &lt;\/div&gt;<br \/>\n  &lt;\/div&gt;<\/p>\n<p>&lt;\/template&gt;<br \/>\n&lt;style scoped&gt;<br \/>\n\/* \u5168\u5c40\u5bb9\u5668&#xff1a;\u5c45\u4e2d&#043;\u56fa\u5b9a\u5bbd\u5ea6&#xff0c;\u907f\u514d\u9875\u9762\u592a\u5bbd\/\u592a\u7a84 *\/<br \/>\n.container {<br \/>\n  width: 900px;<br \/>\n  margin: 20px auto;<br \/>\n  text-align: center;<br \/>\n  font-family: &#034;Microsoft YaHei&#034;, sans-serif;<br \/>\n}<\/p>\n<p>\/* \u8f93\u5165\u6846&#043;\u6309\u94ae\u7ec4\u6837\u5f0f&#xff1a;\u7edf\u4e00\u95f4\u8ddd\u548c\u6837\u5f0f *\/<br \/>\n.input-group {<br \/>\n  margin: 25px 0;<br \/>\n}<br \/>\ninput {<br \/>\n  padding: 10px 15px;<br \/>\n  width: 220px;<br \/>\n  margin-right: 10px;<br \/>\n  border: 1px solid #ddd;<br \/>\n  border-radius: 4px;<br \/>\n  font-size: 14px;<br \/>\n}<br \/>\nbutton {<br \/>\n  padding: 10px 20px;<br \/>\n  background: #42b983;<br \/>\n  color: white;<br \/>\n  border: none;<br \/>\n  border-radius: 4px;<br \/>\n  cursor: pointer;<br \/>\n  font-size: 14px;<br \/>\n}<br \/>\nbutton:hover {<br \/>\n  background: #359469; \/* \u9f20\u6807\u60ac\u6d6e\u53d8\u8272&#xff0c;\u63d0\u5347\u4ea4\u4e92\u4f53\u9a8c *\/<br \/>\n}<\/p>\n<p>\/* \u89c6\u9891\u5bb9\u5668&#xff1a;\u5e76\u6392\u5c55\u793a\u4e24\u4e2a\u89c6\u9891 *\/<br \/>\n.video-container {<br \/>\n  display: flex;<br \/>\n  justify-content: center;<br \/>\n  gap: 30px;<br \/>\n  margin-top: 30px;<br \/>\n}<br \/>\n.video-item {<br \/>\n  text-align: center;<br \/>\n}<br \/>\n.video-item p {<br \/>\n  margin-bottom: 8px;<br \/>\n  font-size: 16px;<br \/>\n  color: #333;<br \/>\n}<br \/>\n\/* \u89c6\u9891\u6807\u7b7e\u6837\u5f0f&#xff1a;\u56fa\u5b9a\u5c3a\u5bf8&#xff0c;\u52a0\u8fb9\u6846&#xff0c;\u63d0\u5347\u7f8e\u89c2\u5ea6 *\/<br \/>\nvideo {<br \/>\n  width: 400px;<br \/>\n  height: 300px;<br \/>\n  border: 1px solid #ccc;<br \/>\n  border-radius: 8px;<br \/>\n  background-color: #f5f5f5; \/* \u672a\u52a0\u8f7d\u6d41\u65f6\u663e\u793a\u6d45\u7070\u8272\u80cc\u666f *\/<br \/>\n}<br \/>\n&lt;\/style&gt;<\/p>\n<h3 id=\"hhHs-1771854856742\">4.2 \u7f16\u5199\u6838\u5fc3\u811a\u672c&#xff08;script setup&#xff09;&#xff0c;\u5b9e\u73b0\u4ea4\u4e92\u903b\u8f91<\/h3>\n<p>\u6b65\u9aa4 1&#xff1a;\u5bfc\u5165\u4f9d\u8d56 &#043; \u5b9a\u4e49\u57fa\u7840\u53d8\u91cf<\/p>\n<p>&lt;script setup&gt;<br \/>\n\/\/ 1. \u5bfc\u5165 Vue \u5185\u7f6e\u7684\u54cd\u5e94\u5f0f\u53d8\u91cf\u548c\u751f\u547d\u5468\u671f\u94a9\u5b50<br \/>\nimport { ref, onUnmounted } from &#039;vue&#039;;<\/p>\n<p>\/\/ 2. \u5b9a\u4e49\u54cd\u5e94\u5f0f\u53d8\u91cf&#xff08;\u9875\u9762\u4e0a\u7528\u5230\u7684\u52a8\u6001\u6570\u636e&#xff09;<br \/>\nconst userId &#061; ref(&#039;&#039;); \/\/ \u672c\u5730\u7528\u6237ID<br \/>\nconst targetUserId &#061; ref(&#039;&#039;); \/\/ \u5bf9\u65b9\u7528\u6237ID<br \/>\nconst socketConnected &#061; ref(false); \/\/ WebSocket\u8fde\u63a5\u72b6\u6001&#xff08;\u63a7\u5236\u547c\u53eb\u533a\u57df\u663e\u793a&#xff09;<\/p>\n<p>\/\/ 3. \u5b9a\u4e49\u89c6\u9891DOM\u5f15\u7528&#xff08;\u7528\u4e8e\u7ed1\u5b9a\u97f3\u89c6\u9891\u6d41&#xff09;<br \/>\nconst localVideo &#061; ref(null); \/\/ \u672c\u5730\u89c6\u9891DOM<br \/>\nconst remoteVideo &#061; ref(null); \/\/ \u8fdc\u7a0b\u89c6\u9891DOM<\/p>\n<p>\/\/ 4. \u5b9a\u4e49\u975e\u54cd\u5e94\u5f0f\u5168\u5c40\u53d8\u91cf&#xff08;\u4ec5\u811a\u672c\u5185\u4f7f\u7528&#xff0c;\u65e0\u9700\u9875\u9762\u54cd\u5e94&#xff09;<br \/>\nlet socket &#061; null; \/\/ WebSocket\u5b9e\u4f8b<br \/>\nlet peerConnection &#061; null; \/\/ WebRTC\u6838\u5fc3\u5b9e\u4f8b<br \/>\nlet localStream &#061; null; \/\/ \u672c\u5730\u97f3\u89c6\u9891\u6d41&#xff08;\u7528\u4e8e\u540e\u7eed\u505c\u6b62\u6d41&#xff09;<br \/>\n&lt;\/script&gt;<\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>ref<\/span><\/span>&#xff1a;\u521b\u5efa\u54cd\u5e94\u5f0f\u53d8\u91cf&#xff0c;\u53d8\u91cf\u503c\u53d8\u5316\u65f6&#xff0c;\u9875\u9762\u4f1a\u81ea\u52a8\u66f4\u65b0&#xff08;\u6bd4\u5982 <span style=\"color:#1f2329\"><span>socketConnected<\/span><\/span> \u53d8\u4e3a true&#xff0c;\u547c\u53eb\u533a\u57df\u4f1a\u663e\u793a&#xff09;&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>onUnmounted<\/span><\/span>&#xff1a;\u9875\u9762\u9500\u6bc1\u65f6\u6267\u884c\u7684\u94a9\u5b50&#xff0c;\u7528\u4e8e\u6e05\u7406\u8d44\u6e90&#xff08;\u907f\u514d\u5185\u5b58\u6cc4\u6f0f&#xff09;&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>localStream<\/span><\/span>&#xff1a;\u989d\u5916\u5b9a\u4e49\u672c\u5730\u6d41\u53d8\u91cf&#xff0c;\u65b9\u4fbf\u540e\u7eed \u201c\u6302\u65ad\u201d \u65f6\u505c\u6b62\u6444\u50cf\u5934 \/ \u9ea6\u514b\u98ce\u3002<\/li>\n<\/ul>\n<p>\u6b65\u9aa4 2&#xff1a;\u914d\u7f6e STUN \u670d\u52a1\u5668 &#043; \u7f16\u5199 WebSocket \u8fde\u63a5\u51fd\u6570<\/p>\n<p>&lt;script setup&gt;<br \/>\n\/\/ &#xff08;\u63a5\u4e0a\u4e00\u6b65\u4ee3\u7801&#xff09;<\/p>\n<p>\/\/ 5. \u914d\u7f6e STUN \u670d\u52a1\u5668&#xff08;WebRTC \u5fc5\u9700&#xff0c;\u7528\u4e8e\u83b7\u53d6\u516c\u7f51ICE\u5019\u9009&#xff09;<br \/>\nconst iceServers &#061; {<br \/>\n  iceServers: [<br \/>\n    { urls: &#039;stun:stun.l.google.com:19302&#039; }, \/\/ \u8c37\u6b4c\u514d\u8d39STUN&#xff08;\u9700\u5916\u7f51&#xff09;<br \/>\n    { urls: &#039;stun:stun.qq.com:3478&#039; }, \/\/ \u817e\u8baf\u514d\u8d39STUN&#xff08;\u56fd\u5185\u66f4\u7a33\u5b9a&#xff09;<br \/>\n    { urls: &#039;stun:stun.aliyun.com:3478&#039; } \/\/ \u963f\u91cc\u4e91\u514d\u8d39STUN&#xff08;\u5907\u7528&#xff09;<br \/>\n  ]<br \/>\n};<\/p>\n<p>\/\/ 6. \u8fde\u63a5 WebSocket \u670d\u52a1\u5668\u51fd\u6570&#xff08;\u70b9\u51fb\u201c\u8fde\u63a5\u670d\u52a1\u5668\u201d\u6309\u94ae\u89e6\u53d1&#xff09;<br \/>\nconst connect &#061; () &#061;&gt; {<br \/>\n  \/\/ \u6821\u9a8c&#xff1a;\u7528\u6237ID\u4e0d\u80fd\u4e3a\u7a7a<br \/>\n  if (!userId.value.trim()) {<br \/>\n    alert(&#039;\u8bf7\u8f93\u5165\u4f60\u7684\u7528\u6237ID&#xff01;&#xff08;\u4e0d\u80fd\u4e3a\u7a7a\/\u4ec5\u7a7a\u683c&#xff09;&#039;);<br \/>\n    return;<br \/>\n  }<\/p>\n<p>  \/\/ \u521b\u5efa WebSocket \u8fde\u63a5&#xff08;Netty\u670d\u52a1\u7aef\u5730\u5740&#xff1a;ws:\/\/localhost:8081\/ws&#xff09;<br \/>\n  \/\/ \u6ce8\u610f&#xff1a;\u5982\u679c\u670d\u52a1\u7aef\u90e8\u7f72\u5728\u5176\u4ed6\u673a\u5668&#xff0c;\u9700\u66ff\u6362\u4e3a\u5bf9\u5e94IP&#xff08;\u5982 ws:\/\/192.168.1.100:8081\/ws&#xff09;<br \/>\n  socket &#061; new WebSocket(&#096;ws:\/\/localhost:8081\/ws&#096;);<\/p>\n<p>  \/\/ 6.1 \u8fde\u63a5\u6210\u529f\u56de\u8c03<br \/>\n  socket.onopen &#061; () &#061;&gt; {<br \/>\n    console.log(&#039;\u2705 WebSocket\u8fde\u63a5\u6210\u529f&#039;);<br \/>\n    socketConnected.value &#061; true; \/\/ \u66f4\u65b0\u8fde\u63a5\u72b6\u6001&#xff0c;\u663e\u793a\u547c\u53eb\u533a\u57df<br \/>\n    \/\/ \u53d1\u9001\u6ce8\u518c\u6d88\u606f&#xff1a;\u544a\u8bc9\u670d\u52a1\u7aef\u201c\u6211\u4e0a\u7ebf\u4e86\u201d<br \/>\n    sendMessage({<br \/>\n      type: &#039;register&#039;,<br \/>\n      from: userId.value,<br \/>\n      to: &#039;&#039;,<br \/>\n      data: &#039;&#039;<br \/>\n    });<br \/>\n  };<\/p>\n<p>  \/\/ 6.2 \u63a5\u6536\u670d\u52a1\u7aef\u6d88\u606f\u56de\u8c03&#xff08;\u6838\u5fc3&#xff1a;\u5904\u7406\u8f6c\u53d1\u7684\u4fe1\u4ee4&#xff09;<br \/>\n  socket.onmessage &#061; (e) &#061;&gt; {<br \/>\n    try {<br \/>\n      const message &#061; JSON.parse(e.data); \/\/ \u89e3\u6790JSON\u6d88\u606f<br \/>\n      console.log(&#039;&#x1f4e5; \u6536\u5230\u670d\u52a1\u7aef\u6d88\u606f&#xff1a;&#039;, message);<br \/>\n      handleMessage(message); \/\/ \u4e13\u95e8\u5904\u7406\u6d88\u606f\u7684\u51fd\u6570&#xff08;\u540e\u7eed\u5b9a\u4e49&#xff09;<br \/>\n    } catch (err) {<br \/>\n      console.error(&#039;\u274c \u6d88\u606f\u89e3\u6790\u5931\u8d25&#xff1a;&#039;, err);<br \/>\n    }<br \/>\n  };<\/p>\n<p>  \/\/ 6.3 \u8fde\u63a5\u5173\u95ed\u56de\u8c03<br \/>\n  socket.onclose &#061; () &#061;&gt; {<br \/>\n    console.log(&#039;\u274c WebSocket\u8fde\u63a5\u5173\u95ed&#039;);<br \/>\n    socketConnected.value &#061; false; \/\/ \u66f4\u65b0\u8fde\u63a5\u72b6\u6001&#xff0c;\u9690\u85cf\u547c\u53eb\u533a\u57df<br \/>\n  };<\/p>\n<p>  \/\/ 6.4 \u8fde\u63a5\u9519\u8bef\u56de\u8c03<br \/>\n  socket.onerror &#061; (err) &#061;&gt; {<br \/>\n    console.error(&#039;\u274c WebSocket\u8fde\u63a5\u9519\u8bef&#xff1a;&#039;, err);<br \/>\n    socketConnected.value &#061; false;<br \/>\n    alert(&#039;\u8fde\u63a5\u670d\u52a1\u5668\u5931\u8d25&#xff01;\u8bf7\u68c0\u67e5\u670d\u52a1\u7aef\u662f\u5426\u542f\u52a8&#xff0c;\u7aef\u53e3\u662f\u5426\u6b63\u786e\u3002&#039;);<br \/>\n  };<br \/>\n};<br \/>\n&lt;\/script&gt;<\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">STUN \u670d\u52a1\u5668&#xff1a;\u5fc5\u987b\u914d\u7f6e&#xff0c;\u5426\u5219\u5185\u7f51\u8bbe\u5907\u65e0\u6cd5\u627e\u5230\u5f7c\u6b64\u7684\u7f51\u7edc\u5730\u5740&#xff0c;\u5bfc\u81f4\u89c6\u9891\u65e0\u6cd5\u8fde\u63a5&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>trim()<\/span><\/span>&#xff1a;\u53bb\u9664\u7528\u6237 ID \u524d\u540e\u7a7a\u683c&#xff0c;\u907f\u514d\u7528\u6237\u8f93\u5165\u7a7a\u5b57\u7b26 \/ \u4ec5\u7a7a\u683c&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>try-catch<\/span><\/span>&#xff1a;\u9632\u6b62\u670d\u52a1\u7aef\u8fd4\u56de\u975e JSON \u683c\u5f0f\u6d88\u606f&#xff0c;\u5bfc\u81f4\u811a\u672c\u5d29\u6e83&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">WebSocket \u5730\u5740&#xff1a;<span style=\"color:#1f2329\"><span>ws:\/\/<\/span><\/span> \u5bf9\u5e94 HTTP&#xff0c;<span style=\"color:#1f2329\"><span>wss:\/\/<\/span><\/span> \u5bf9\u5e94 HTTPS&#xff0c;\u672c\u5730\u6d4b\u8bd5\u7528 <span style=\"color:#1f2329\"><span>ws:\/\/<\/span><\/span> \u5373\u53ef\u3002<\/li>\n<\/ul>\n<p>\u6b65\u9aa43&#xff1a;\u7f16\u5199\u901a\u7528\u6d88\u606f\u53d1\u9001\u51fd\u6570<\/p>\n<p>&lt;script setup&gt;<br \/>\n\/\/ &#xff08;\u63a5\u4e0a\u4e00\u6b65\u4ee3\u7801&#xff09;<\/p>\n<p>\/\/ 7. \u901a\u7528\u53d1\u9001WebSocket\u6d88\u606f\u51fd\u6570&#xff08;\u590d\u7528&#xff0c;\u907f\u514d\u91cd\u590d\u4ee3\u7801&#xff09;<br \/>\nconst sendMessage &#061; (message) &#061;&gt; {<br \/>\n  \/\/ \u6821\u9a8c&#xff1a;WebSocket\u5fc5\u987b\u5904\u4e8e\u6253\u5f00\u72b6\u6001<br \/>\n  if (socket &amp;&amp; socket.readyState &#061;&#061;&#061; WebSocket.OPEN) {<br \/>\n    socket.send(JSON.stringify(message)); \/\/ \u8f6c\u4e3aJSON\u5b57\u7b26\u4e32\u53d1\u9001<br \/>\n    console.log(&#039;&#x1f4e4; \u53d1\u9001\u6d88\u606f&#xff1a;&#039;, message);<br \/>\n  } else {<br \/>\n    console.error(&#039;\u274c WebSocket\u672a\u8fde\u63a5&#xff0c;\u65e0\u6cd5\u53d1\u9001\u6d88\u606f&#039;);<br \/>\n    alert(&#039;\u672a\u8fde\u63a5\u670d\u52a1\u5668&#xff0c;\u8bf7\u5148\u70b9\u51fb\u201c\u8fde\u63a5\u670d\u52a1\u5668\u201d&#xff01;&#039;);<br \/>\n  }<br \/>\n};<br \/>\n&lt;\/script&gt;<\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>WebSocket.OPEN<\/span><\/span>&#xff1a;\u72b6\u6001\u7801\u4e3a 1&#xff0c;\u8868\u793a\u8fde\u63a5\u5df2\u6253\u5f00&#xff1b;\u5176\u4ed6\u72b6\u6001&#xff08;0 &#061; \u8fde\u63a5\u4e2d&#xff0c;2 &#061; \u5173\u95ed\u4e2d&#xff0c;3 &#061; \u5df2\u5173\u95ed&#xff09;\u90fd\u65e0\u6cd5\u53d1\u9001\u6d88\u606f&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">\u5c01\u88c5\u6210\u901a\u7528\u51fd\u6570&#xff1a;\u540e\u7eed\u53d1\u9001 call\/answer\/ice \u6d88\u606f\u65f6&#xff0c;\u76f4\u63a5\u8c03\u7528\u5373\u53ef&#xff0c;\u51cf\u5c11\u91cd\u590d\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p style=\"text-align:left\">\u6b65\u9aa4 4&#xff1a;\u7f16\u5199\u6d88\u606f\u5904\u7406\u51fd\u6570<\/p>\n<p>&lt;script setup&gt;<br \/>\n\/\/ &#xff08;\u63a5\u4e0a\u4e00\u6b65\u4ee3\u7801&#xff09;<\/p>\n<p>\/\/ 8. \u5904\u7406\u6536\u5230\u7684\u4fe1\u4ee4\u6d88\u606f<br \/>\nconst handleMessage &#061; async (message) &#061;&gt; {<br \/>\n  switch (message.type) {<br \/>\n    case &#039;call&#039;:<br \/>\n      \/\/ \u6536\u5230\u547c\u53eb\u8bf7\u6c42&#xff1a;\u81ea\u52a8\u5e94\u7b54&#xff08;\u5b9e\u9645\u9879\u76ee\u53ef\u52a0\u201c\u662f\u5426\u63a5\u542c\u201d\u5f39\u7a97&#xff09;<br \/>\n      await answerCall(message);<br \/>\n      break;<br \/>\n    case &#039;answer&#039;:<br \/>\n      \/\/ \u6536\u5230\u5e94\u7b54\u6d88\u606f&#xff1a;\u8bbe\u7f6e\u8fdc\u7a0bSDP<br \/>\n      await setRemoteSDP(message.data);<br \/>\n      break;<br \/>\n    case &#039;ice&#039;:<br \/>\n      \/\/ \u6536\u5230ICE\u5019\u9009&#xff1a;\u6dfb\u52a0\u5230PeerConnection<br \/>\n      await addIceCandidate(message.data);<br \/>\n      break;<br \/>\n    default:<br \/>\n      console.log(&#039;&#x1f4cc; \u672a\u77e5\u6d88\u606f\u7c7b\u578b&#xff1a;&#039;, message.type);<br \/>\n  }<br \/>\n};<br \/>\n&lt;\/script&gt;<\/p>\n<p><span style=\"color:#1f2329\"><span>\u8981\u70b9<\/span><\/span>&#xff1a;<\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">\u6309\u6d88\u606f\u7c7b\u578b\u5206\u652f\u5904\u7406&#xff1a;\u5bf9\u5e94\u670d\u52a1\u7aef\u7684 <span style=\"color:#1f2329\"><span>register\/call\/answer\/ice<\/span><\/span> \u56db\u79cd\u7c7b\u578b&#xff1b;<\/li>\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>async\/await<\/span><\/span>&#xff1a;\u540e\u7eed\u64cd\u4f5c&#xff08;\u5982\u8bbe\u7f6e SDP&#xff09;\u662f\u5f02\u6b65\u7684&#xff0c;\u9700\u7b49\u5f85\u5b8c\u6210&#xff0c;\u907f\u514d\u62a5\u9519\u3002<\/li>\n<\/ul>\n<p>\u6b65\u9aa4 5&#xff1a;\u7f16\u5199 WebRTC \u6838\u5fc3\u51fd\u6570<\/p>\n<p>&lt;script setup&gt;<br \/>\n\/\/ &#xff08;\u63a5\u4e0a\u4e00\u6b65\u4ee3\u7801&#xff09;<\/p>\n<p>\/\/ 9. \u521d\u59cb\u5316 PeerConnection&#xff08;WebRTC\u6838\u5fc3&#xff0c;\u590d\u7528\u51fd\u6570&#xff09;<br \/>\nconst initPeerConnection &#061; async () &#061;&gt; {<br \/>\n  \/\/ \u5982\u679c\u5df2\u6709PeerConnection&#xff0c;\u5148\u5173\u95ed&#xff08;\u907f\u514d\u91cd\u590d\u521b\u5efa&#xff09;<br \/>\n  if (peerConnection) {<br \/>\n    peerConnection.close();<br \/>\n  }<br \/>\n  \/\/ \u521b\u5efaPeerConnection\u5b9e\u4f8b&#xff08;\u4f20\u5165STUN\u670d\u52a1\u5668\u914d\u7f6e&#xff09;<br \/>\n  peerConnection &#061; new RTCPeerConnection(iceServers);<\/p>\n<p>  \/\/ 9.1 \u76d1\u542cICE\u5019\u9009\u751f\u6210\u4e8b\u4ef6&#xff08;\u672c\u5730\u7f51\u7edc\u5730\u5740&#xff09;<br \/>\n  peerConnection.onicecandidate &#061; (e) &#061;&gt; {<br \/>\n    if (e.candidate) {<br \/>\n      \/\/ \u53d1\u9001ICE\u5019\u9009\u7ed9\u5bf9\u65b9<br \/>\n      sendMessage({<br \/>\n        type: &#039;ice&#039;,<br \/>\n        from: userId.value,<br \/>\n        to: targetUserId.value,<br \/>\n        data: JSON.stringify(e.candidate)<br \/>\n      });<br \/>\n    }<br \/>\n  };<\/p>\n<p>  \/\/ 9.2 \u76d1\u542c\u8fdc\u7a0b\u97f3\u89c6\u9891\u6d41\u5230\u8fbe\u4e8b\u4ef6&#xff08;\u5173\u952e&#xff1a;\u663e\u793a\u5bf9\u65b9\u89c6\u9891&#xff09;<br \/>\n  peerConnection.ontrack &#061; (e) &#061;&gt; {<br \/>\n    \/\/ \u5c06\u8fdc\u7a0b\u6d41\u7ed1\u5b9a\u5230\u8fdc\u7a0b\u89c6\u9891DOM<br \/>\n    remoteVideo.value.srcObject &#061; e.streams[0];<br \/>\n    console.log(&#039;&#x1f3a5; \u6536\u5230\u8fdc\u7a0b\u97f3\u89c6\u9891\u6d41&#039;);<br \/>\n  };<br \/>\n};<\/p>\n<p>\/\/ 10. \u53d1\u8d77\u89c6\u9891\u547c\u53eb\u51fd\u6570&#xff08;\u70b9\u51fb\u201c\u53d1\u8d77\u89c6\u9891\u547c\u53eb\u201d\u6309\u94ae\u89e6\u53d1&#xff09;<br \/>\nconst call &#061; async () &#061;&gt; {<br \/>\n  \/\/ \u6821\u9a8c&#xff1a;\u5bf9\u65b9ID\u4e0d\u80fd\u4e3a\u7a7a<br \/>\n  if (!targetUserId.value.trim()) {<br \/>\n    alert(&#039;\u8bf7\u8f93\u5165\u5bf9\u65b9\u7528\u6237ID&#xff01;&#039;);<br \/>\n    return;<br \/>\n  }<\/p>\n<p>  try {<br \/>\n    \/\/ \u521d\u59cb\u5316PeerConnection<br \/>\n    await initPeerConnection();<br \/>\n    \/\/ \u83b7\u53d6\u672c\u5730\u97f3\u89c6\u9891\u6d41&#xff08;\u8bf7\u6c42\u6444\u50cf\u5934\/\u9ea6\u514b\u98ce\u6743\u9650&#xff09;<br \/>\n    localStream &#061; await navigator.mediaDevices.getUserMedia({<br \/>\n      video: true, \/\/ \u5f00\u542f\u89c6\u9891<br \/>\n      audio: true  \/\/ \u5f00\u542f\u97f3\u9891<br \/>\n    });<br \/>\n    \/\/ \u5c06\u672c\u5730\u6d41\u7ed1\u5b9a\u5230\u672c\u5730\u89c6\u9891DOM<br \/>\n    localVideo.value.srcObject &#061; localStream;<br \/>\n    \/\/ \u5c06\u97f3\u89c6\u9891\u8f68\u9053\u6dfb\u52a0\u5230PeerConnection&#xff08;\u4f20\u7ed9\u5bf9\u65b9&#xff09;<br \/>\n    localStream.getTracks().forEach(track &#061;&gt; {<br \/>\n      peerConnection.addTrack(track, localStream);<br \/>\n    });<\/p>\n<p>    \/\/ \u521b\u5efaSDP\u63d0\u8bae&#xff08;offer&#xff09;&#xff1a;\u5305\u542b\u672c\u5730\u97f3\u89c6\u9891\u914d\u7f6e<br \/>\n    const offer &#061; await peerConnection.createOffer();<br \/>\n    \/\/ \u8bbe\u7f6e\u672c\u5730SDP<br \/>\n    await peerConnection.setLocalDescription(offer);<br \/>\n    \/\/ \u53d1\u9001\u547c\u53eb\u4fe1\u4ee4&#xff08;\u542bSDP offer&#xff09;\u7ed9\u5bf9\u65b9<br \/>\n    sendMessage({<br \/>\n      type: &#039;call&#039;,<br \/>\n      from: userId.value,<br \/>\n      to: targetUserId.value,<br \/>\n      data: JSON.stringify(offer)<br \/>\n    });<br \/>\n    console.log(&#039;&#x1f4de; \u53d1\u8d77\u89c6\u9891\u547c\u53eb&#xff1a;&#039;, targetUserId.value);<br \/>\n  } catch (err) {<br \/>\n    console.error(&#039;\u274c \u53d1\u8d77\u547c\u53eb\u5931\u8d25&#xff1a;&#039;, err);<br \/>\n    alert(&#039;\u53d1\u8d77\u547c\u53eb\u5931\u8d25&#xff01;\u8bf7\u68c0\u67e5\u6444\u50cf\u5934\/\u9ea6\u514b\u98ce\u6743\u9650&#xff0c;\u6216\u662f\u5426\u5df2\u8fde\u63a5\u670d\u52a1\u5668\u3002&#039;);<br \/>\n  }<br \/>\n};<\/p>\n<p>\/\/ 11. \u5e94\u7b54\u547c\u53eb\u8bf7\u6c42\u51fd\u6570<br \/>\nconst answerCall &#061; async (message) &#061;&gt; {<br \/>\n  \/\/ \u8bb0\u5f55\u547c\u53eb\u65b9ID&#xff08;\u540e\u7eed\u53d1\u9001\u5e94\u7b54\/ICE\u6d88\u606f\u9700\u8981&#xff09;<br \/>\n  targetUserId.value &#061; message.from;<br \/>\n  try {<br \/>\n    \/\/ \u521d\u59cb\u5316PeerConnection<br \/>\n    await initPeerConnection();<br \/>\n    \/\/ \u83b7\u53d6\u672c\u5730\u97f3\u89c6\u9891\u6d41<br \/>\n    localStream &#061; await navigator.mediaDevices.getUserMedia({<br \/>\n      video: true,<br \/>\n      audio: true<br \/>\n    });<br \/>\n    localVideo.value.srcObject &#061; localStream;<br \/>\n    localStream.getTracks().forEach(track &#061;&gt; {<br \/>\n      peerConnection.addTrack(track, localStream);<br \/>\n    });<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u8fdc\u7a0bSDP&#xff08;\u547c\u53eb\u65b9\u7684offer&#xff09;<br \/>\n    await peerConnection.setRemoteDescription(JSON.parse(message.data));<br \/>\n    \/\/ \u521b\u5efaSDP\u5e94\u7b54&#xff08;answer&#xff09;<br \/>\n    const answer &#061; await peerConnection.createAnswer();<br \/>\n    \/\/ \u8bbe\u7f6e\u672c\u5730SDP<br \/>\n    await peerConnection.setLocalDescription(answer);<br \/>\n    \/\/ \u53d1\u9001\u5e94\u7b54\u4fe1\u4ee4\u7ed9\u547c\u53eb\u65b9<br \/>\n    sendMessage({<br \/>\n      type: &#039;answer&#039;,<br \/>\n      from: userId.value,<br \/>\n      to: targetUserId.value,<br \/>\n      data: JSON.stringify(answer)<br \/>\n    });<br \/>\n    console.log(&#039;&#x1f4de; \u5e94\u7b54\u89c6\u9891\u547c\u53eb&#xff1a;&#039;, targetUserId.value);<br \/>\n  } catch (err) {<br \/>\n    console.error(&#039;\u274c \u5e94\u7b54\u547c\u53eb\u5931\u8d25&#xff1a;&#039;, err);<br \/>\n    alert(&#039;\u5e94\u7b54\u547c\u53eb\u5931\u8d25&#xff01;&#039;);<br \/>\n  }<br \/>\n};<\/p>\n<p>\/\/ 12. \u8bbe\u7f6e\u8fdc\u7a0bSDP\u51fd\u6570<br \/>\nconst setRemoteSDP &#061; async (sdpStr) &#061;&gt; {<br \/>\n  try {<br \/>\n    const sdp &#061; JSON.parse(sdpStr);<br \/>\n    await peerConnection.setRemoteDescription(new RTCSessionDescription(sdp));<br \/>\n    console.log(&#039;\u2705 \u8bbe\u7f6e\u8fdc\u7a0bSDP\u6210\u529f&#039;);<br \/>\n  } catch (err) {<br \/>\n    console.error(&#039;\u274c \u8bbe\u7f6e\u8fdc\u7a0bSDP\u5931\u8d25&#xff1a;&#039;, err);<br \/>\n  }<br \/>\n};<\/p>\n<p>\/\/ 13. \u6dfb\u52a0ICE\u5019\u9009\u51fd\u6570<br \/>\nconst addIceCandidate &#061; async (iceStr) &#061;&gt; {<br \/>\n  try {<br \/>\n    const ice &#061; JSON.parse(iceStr);<br \/>\n    await peerConnection.addIceCandidate(new RTCIceCandidate(ice));<br \/>\n    console.log(&#039;\u2705 \u6dfb\u52a0ICE\u5019\u9009\u6210\u529f&#039;);<br \/>\n  } catch (err) {<br \/>\n    console.error(&#039;\u274c \u6dfb\u52a0ICE\u5019\u9009\u5931\u8d25&#xff1a;&#039;, err);<br \/>\n  }<br \/>\n};<br \/>\n&lt;\/script&gt;<\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>getUserMedia<\/span><\/span>&#xff1a;\u6d4f\u89c8\u5668\u539f\u751f API&#xff0c;\u8bf7\u6c42\u97f3\u89c6\u9891\u6743\u9650&#xff0c;\u8fd4\u56de\u672c\u5730\u6d41&#xff1b;\u5982\u679c\u7528\u6237\u62d2\u7edd\u6743\u9650&#xff0c;\u4f1a\u629b\u51fa\u9519\u8bef&#xff0c;\u6240\u4ee5\u7528 <span style=\"color:#1f2329\"><span>try-catch<\/span><\/span> \u5305\u88f9&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>PeerConnection<\/span><\/span>&#xff1a;WebRTC \u7684\u6838\u5fc3\u5bf9\u8c61&#xff0c;\u8d1f\u8d23\u534f\u5546\u8fde\u63a5\u3001\u4f20\u8f93\u97f3\u89c6\u9891\u6d41&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>ontrack<\/span><\/span> \u4e8b\u4ef6&#xff1a;\u5bf9\u65b9\u7684\u97f3\u89c6\u9891\u6d41\u5230\u8fbe\u65f6\u89e6\u53d1&#xff0c;\u5c06\u6d41\u7ed1\u5b9a\u5230 <span style=\"color:#1f2329\"><span>remoteVideo<\/span><\/span> \u5373\u53ef\u663e\u793a\u5bf9\u65b9\u753b\u9762&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>onicecandidate<\/span><\/span> \u4e8b\u4ef6&#xff1a;\u672c\u5730\u751f\u6210\u7f51\u7edc\u5730\u5740&#xff08;ICE \u5019\u9009&#xff09;\u65f6\u89e6\u53d1&#xff0c;\u53d1\u9001\u7ed9\u5bf9\u65b9&#xff0c;\u53cc\u65b9\u624d\u80fd\u5efa\u7acb P2P \u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p>\u6b65\u9aa46&#xff1a;\u7f16\u5199\u8d44\u6e90\u6e05\u7406\u51fd\u6570<\/p>\n<p>&lt;script setup&gt;<br \/>\n\/\/ &#xff08;\u63a5\u4e0a\u4e00\u6b65\u4ee3\u7801&#xff09;<\/p>\n<p>\/\/ 14. \u9875\u9762\u9500\u6bc1\u65f6\u6e05\u7406\u8d44\u6e90&#xff08;\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\/\u8bbe\u5907\u5360\u7528&#xff09;<br \/>\nonUnmounted(() &#061;&gt; {<br \/>\n  \/\/ \u5173\u95edWebSocket\u8fde\u63a5<br \/>\n  if (socket) {<br \/>\n    socket.close();<br \/>\n    console.log(&#039;&#x1f50c; \u5173\u95edWebSocket\u8fde\u63a5&#039;);<br \/>\n  }<br \/>\n  \/\/ \u5173\u95edPeerConnection<br \/>\n  if (peerConnection) {<br \/>\n    peerConnection.close();<br \/>\n    console.log(&#039;&#x1f50c; \u5173\u95edPeerConnection&#039;);<br \/>\n  }<br \/>\n  \/\/ \u505c\u6b62\u672c\u5730\u97f3\u89c6\u9891\u6d41&#xff08;\u91ca\u653e\u6444\u50cf\u5934\/\u9ea6\u514b\u98ce&#xff09;<br \/>\n  if (localStream) {<br \/>\n    localStream.getTracks().forEach(track &#061;&gt; {<br \/>\n      track.stop();<br \/>\n      console.log(&#039;&#x1f507; \u505c\u6b62\u672c\u5730\u97f3\u89c6\u9891\u6d41&#039;);<br \/>\n    });<br \/>\n  }<br \/>\n});<br \/>\n&lt;\/script&gt;<\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">\u5fc5\u987b\u505c\u6b62\u672c\u5730\u6d41&#xff1a;\u5426\u5219\u9875\u9762\u5173\u95ed\u540e&#xff0c;\u6444\u50cf\u5934 \/ \u9ea6\u514b\u98ce\u4ecd\u4f1a\u88ab\u5360\u7528&#xff08;\u6d4f\u89c8\u5668\u6807\u7b7e\u680f\u4f1a\u663e\u793a\u6444\u50cf\u5934\u56fe\u6807&#xff09;&#xff1b;<\/li>\n<\/ul>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#1f2329\"><span>onUnmounted<\/span><\/span>&#xff1a;Vue3 \u7684\u751f\u547d\u5468\u671f\u94a9\u5b50&#xff0c;\u9875\u9762\u9500\u6bc1\u65f6\u81ea\u52a8\u6267\u884c&#xff0c;\u786e\u4fdd\u8d44\u6e90\u5168\u90e8\u6e05\u7406\u3002<\/li>\n<\/ul>\n<h3 id=\"sUxH-1771854992616\" style=\"text-align:left\">4.3\u00a0\u8865\u5145 \u201c\u6302\u65ad\u201d \u529f\u80fd<\/h3>\n<p>\u5728 <span style=\"color:#1f2329\"><span>video-container<\/span><\/span> \u4e0a\u65b9\u6dfb\u52a0&#xff1a;<\/p>\n<p>&lt;!&#8211; \u6302\u65ad\u6309\u94ae&#xff08;\u4ec5\u8fde\u63a5\u6210\u529f\u4e14\u6709\u8fdc\u7a0b\u6d41\u65f6\u663e\u793a&#xff09; &#8211;&gt;<br \/>\n&lt;div class&#061;&#034;input-group&#034; v-if&#061;&#034;socketConnected &amp;&amp; remoteVideo.value?.srcObject&#034;&gt;<br \/>\n    &lt;button &#064;click&#061;&#034;hangUp&#034; style&#061;&#034;background: #e53935;&#034;&gt;\u6302\u65ad\u901a\u8bdd&lt;\/button&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>\u811a\u672c\u6dfb\u52a0\u6302\u65ad\u51fd\u6570<\/p>\n<p>&lt;script setup&gt;<br \/>\n\/\/ &#xff08;\u6dfb\u52a0\u5728 initPeerConnection \u4e4b\u540e&#xff09;<\/p>\n<p>\/\/ 15. \u6302\u65ad\u901a\u8bdd\u51fd\u6570<br \/>\nconst hangUp &#061; () &#061;&gt; {<br \/>\n  \/\/ \u505c\u6b62\u672c\u5730\u6d41<br \/>\n  if (localStream) {<br \/>\n    localStream.getTracks().forEach(track &#061;&gt; track.stop());<br \/>\n    localVideo.value.srcObject &#061; null; \/\/ \u6e05\u7a7a\u672c\u5730\u89c6\u9891<br \/>\n  }<br \/>\n  \/\/ \u6e05\u7a7a\u8fdc\u7a0b\u89c6\u9891<br \/>\n  if (remoteVideo.value) {<br \/>\n    remoteVideo.value.srcObject &#061; null;<br \/>\n  }<br \/>\n  \/\/ \u5173\u95edPeerConnection<br \/>\n  if (peerConnection) {<br \/>\n    peerConnection.close();<br \/>\n    peerConnection &#061; null;<br \/>\n  }<br \/>\n  \/\/ \u91cd\u7f6e\u76ee\u6807\u7528\u6237ID<br \/>\n  targetUserId.value &#061; &#039;&#039;;<br \/>\n  console.log(&#039;&#x1f4de; \u6302\u65ad\u901a\u8bdd&#039;);<br \/>\n  alert(&#039;\u5df2\u6302\u65ad\u901a\u8bdd&#xff01;&#039;);<br \/>\n};<br \/>\n&lt;\/script&gt;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u65f6\u97f3\u89c6\u9891\u804a\u5929\u662f\u5f53\u4e0b\u793e\u4ea4\u3001\u5728\u7ebf\u534f\u4f5c\u7c7b\u5e94\u7528\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00&#xff0c;WebRTC&#xff08;Web Real-Time Communication&#xff09;\u4f5c\u4e3a\u6d4f\u89c8\u5668\u539f\u751f\u652f\u6301\u7684\u5b9e\u65f6\u901a\u4fe1\u6280\u672f&#xff0c;\u80fd\u8ba9\u524d\u7aef\u65e0\u9700\u63d2\u4ef6\u5373\u53ef\u5b9e\u73b0\u70b9\u5bf9\u70b9\u97f3\u89c6\u9891\u4f20\u8f93&#xff1b;\u800c Netty \u4f5c\u4e3a\u9ad8\u6027\u80fd\u7684 Java NIO \u6846\u67b6&#xff0c;\u53ef\u63d0\u4f9b\u7a33\u5b9a\u7684 WebSocket \u901a\u4fe1\u901a\u9053&#xff0c;\u914d\u5408 SpringBoot \u7684\u5feb\u901f\u5f00\u53d1\u80fd\u529b\u548c Vue \u7684\u524d\u7aef\u5de5\u7a0b\u5316\u80fd\u529b&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":78024,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[98,625,947],"topic":[],"class_list":["post-78025","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-spring-boot","tag-vue-js","tag-websocket"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/78025.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5b9e\u65f6\u97f3\u89c6\u9891\u804a\u5929\u662f\u5f53\u4e0b\u793e\u4ea4\u3001\u5728\u7ebf\u534f\u4f5c\u7c7b\u5e94\u7528\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00&#xff0c;WebRTC&#xff08;Web Real-Time Communication&#xff09;\u4f5c\u4e3a\u6d4f\u89c8\u5668\u539f\u751f\u652f\u6301\u7684\u5b9e\u65f6\u901a\u4fe1\u6280\u672f&#xff0c;\u80fd\u8ba9\u524d\u7aef\u65e0\u9700\u63d2\u4ef6\u5373\u53ef\u5b9e\u73b0\u70b9\u5bf9\u70b9\u97f3\u89c6\u9891\u4f20\u8f93&#xff1b;\u800c Netty \u4f5c\u4e3a\u9ad8\u6027\u80fd\u7684 Java NIO \u6846\u67b6&#xff0c;\u53ef\u63d0\u4f9b\u7a33\u5b9a\u7684 WebSocket \u901a\u4fe1\u901a\u9053&#xff0c;\u914d\u5408 SpringBoot \u7684\u5feb\u901f\u5f00\u53d1\u80fd\u529b\u548c Vue \u7684\u524d\u7aef\u5de5\u7a0b\u5316\u80fd\u529b&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78025.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-25T14:41:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260225144136-699f0a2076761.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78025.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78025.html\",\"name\":\"SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-25T14:41:37+00:00\",\"dateModified\":\"2026-02-25T14:41:37+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78025.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78025.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78025.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/78025.html","og_locale":"zh_CN","og_type":"article","og_title":"SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5b9e\u65f6\u97f3\u89c6\u9891\u804a\u5929\u662f\u5f53\u4e0b\u793e\u4ea4\u3001\u5728\u7ebf\u534f\u4f5c\u7c7b\u5e94\u7528\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00&#xff0c;WebRTC&#xff08;Web Real-Time Communication&#xff09;\u4f5c\u4e3a\u6d4f\u89c8\u5668\u539f\u751f\u652f\u6301\u7684\u5b9e\u65f6\u901a\u4fe1\u6280\u672f&#xff0c;\u80fd\u8ba9\u524d\u7aef\u65e0\u9700\u63d2\u4ef6\u5373\u53ef\u5b9e\u73b0\u70b9\u5bf9\u70b9\u97f3\u89c6\u9891\u4f20\u8f93&#xff1b;\u800c Netty \u4f5c\u4e3a\u9ad8\u6027\u80fd\u7684 Java NIO \u6846\u67b6&#xff0c;\u53ef\u63d0\u4f9b\u7a33\u5b9a\u7684 WebSocket \u901a\u4fe1\u901a\u9053&#xff0c;\u914d\u5408 SpringBoot \u7684\u5feb\u901f\u5f00\u53d1\u80fd\u529b\u548c Vue \u7684\u524d\u7aef\u5de5\u7a0b\u5316\u80fd\u529b&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/78025.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-25T14:41:37+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260225144136-699f0a2076761.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"11 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78025.html","url":"https:\/\/www.wsisp.com\/helps\/78025.html","name":"SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-25T14:41:37+00:00","dateModified":"2026-02-25T14:41:37+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78025.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78025.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78025.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"SpringBoot+Vue+Netty+WebSocket+WebRTC \u5b9e\u73b0\u89c6\u9891\u804a\u5929"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=78025"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78025\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/78024"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78025"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}