{"id":930,"date":"2022-11-24T20:34:49","date_gmt":"2022-11-24T12:34:49","guid":{"rendered":"http:\/\/www.aqwu.net\/wp\/?p=930"},"modified":"2022-11-24T20:34:49","modified_gmt":"2022-11-24T12:34:49","slug":"%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e5%ae%89%e5%85%a8%e3%80%81%e6%97%a0%e6%9c%8d%e5%8a%a1%e5%99%a8%e3%80%81%e6%8c%81%e4%b9%85%e4%b8%94%e5%ae%9e%e6%97%b6%e7%9a%84%e5%8d%95%e9%a1%b5%e8%81%8a%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=930","title":{"rendered":"\u5982\u4f55\u6784\u5efa\u5b89\u5168\u3001\u65e0\u670d\u52a1\u5668\u3001\u6301\u4e45\u4e14\u5b9e\u65f6\u7684\u5355\u9875\u804a\u5929"},"content":{"rendered":"\n<p>\u4f7f\u7528 WebRTC\u3001PeerJS\u3001Vue \u548c javascript vanilla cookies \u4e0d\u5230 3 \u5c0f\u65f6<\/p>\n\n\n\n<p id=\"7898\">\u4eca\u5929\u7684\u4e92\u8054\u7f51\u57fa\u7840\u8bbe\u65bd\u4f9d\u8d56\u4e8e\u6570\u636e\u4e2d\u5fc3\uff0c\u8fd9\u4f1a\u4ea7\u751f<strong>\u6210\u672c<\/strong>\u548c<strong>\u73af\u5883\u5f71\u54cd<\/strong>\u3002\u66f4\u5177\u4f53\u5730\u8bf4\uff0c<a href=\"https:\/\/www.climatechangenews.com\/2017\/12\/11\/tsunami-data-consume-one-fifth-global-electricity-2025\/\" rel=\"noreferrer noopener\" target=\"_blank\">\u6570\u636e\u4e2d\u5fc3\u8d1f\u8d23\u5927\u7ea6\u3002\u5360\u4e16\u754c\u80fd\u6e90\u6d88\u8017\u76844%<\/a>\uff0c\u800c\u4e14\u8fd8\u5728\u4e0d\u65ad\u589e\u957f\u3002\u4f46\u662f\uff0c\u5982\u679c\u6ca1\u6709\u6bcf\u5929<strong>\u6309\u9700<\/strong>\u72c2\u6b22\uff0c\u60a8\u5c06\u5982\u4f55\u5ea6\u8fc7\u4e00\u5929\uff1f<\/p>\n\n\n\n<p>\u539f\u6587\u8fde\u63a5\uff1ahttps:\/\/levelup.gitconnected.com\/multichat-a-secure-serverless-persistent-and-real-time-chat-proof-of-concept-22d3dddc0345<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"d326\">\u5982\u679c\u6211\u4eec\u53ef\u4ee5<strong>\u5728\u4e0d\u9700\u8981\u670d\u52a1\u5668\u7684\u60c5\u51b5\u4e0b<\/strong>\u901a\u4fe1\/\u4ea4\u6362\u6570\u636e\u600e\u4e48\u529e\uff1f\u5982\u679c\u4e2a\u4eba\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5728\u672c\u5730\uff0c\u8fdc\u79bb\u5927\u516c\u53f8\u7684\u6570\u636e\u4e2d\u5fc3\uff0c\u4f1a\u600e\u6837\uff1f<\/p>\n<\/blockquote>\n\n\n\n<p id=\"41c0\">\u5e78\u8fd0\u7684\u662f\uff0c\u70b9\u5bf9\u70b9\u8fde\u63a5\u662f\u4e92\u8054\u7f51\u7684\u57fa\u7840\uff0c\u6709\u4e9b\u4eba\u6bd4\u6211\u66f4\u65e9\u60f3\u5230\u5b83\uff1b\u8fd9\u5bfc\u81f4\u4e86\uff08\u9664\u5176\u4ed6\u5916\uff09<a href=\"https:\/\/webrtc.org\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong><em>WebRTC<\/em><\/strong><\/a>\u7684\u53d1\u5c55\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u4ecb\u7ecd WebRTC \u7684\u4f18\u52bf\uff0c\u5e76\u901a\u8fc7\u7b80\u5355\u3001\u6613\u7528\u3001\u5b89\u5168\u3001\u51e0\u4e4e\u65e0\u670d\u52a1\u5668\u3001\u6301\u4e45\u7684\u5b9e\u65f6\u804a\u5929\u6982\u5ff5\u9a8c\u8bc1\u6765\u5c55\u793a\u5176\u5f3a\u5927\u529f\u80fd\u3002\u7136\u800c\uff0cWebRTC \u4e5f\u6709\u5176\u81ea\u8eab\u7684\u5c40\u9650\u6027\uff1b\u4f46\u8fd9\u662f\u53e6\u4e00\u4e2a\u540e\u6765\u7684\u6545\u4e8b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"b154\">\u6211\u80fd\u770b\u61c2\u8fd9\u7bc7\u6587\u7ae0\u5417\uff1f<\/p>\n<\/blockquote>\n\n\n\n<p id=\"dcf6\">\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u6db5\u76d6\u4e86\u5927\u578b\u5168\u6808\u5f00\u53d1\u4e3b\u9898\uff0c\u8fd9\u4e9b\u4e3b\u9898\u9700\u8981\u4e00\u4e9b\u4e92\u8054\u7f51\u901a\u4fe1\u3001javascript \u6570\u636e\u548c\u72b6\u6001\u7ba1\u7406\u4ee5\u53ca\u663e\u7136\u662f HTML\/CSS \u7684\u77e5\u8bc6\u3002\u5982\u679c\u60a8\u6ca1\u6709\u5168\u90e8\uff0c\u60a8\u5c06\u5b66\u5230\u4e00\u4e9b\u6709\u7528\u7684\u63d0\u793a\u548c\u6280\u5de7\uff01<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"5cd3\">\u6311\u6218<\/h1>\n\n\n\n<p id=\"67fb\">\u8be5\u9879\u76ee\u7684\u76ee\u6807\u662f\u7b80\u5316\u548c\u5c55\u793a\u6d4f\u89c8\u5668\u4e2d\u65e0\u670d\u52a1\u5668\u6570\u636e\u4ea4\u6362\u7684<em>\u5f3a\u5927\u529f\u80fd\u3001\u5b89\u5168\u6027\/\u673a\u5bc6\u6027<\/em>\u548c<em>\u6301\u4e45\u6027\u3002<\/em>\u4ee5\u4e0b\u662f\u6211\u9009\u62e9\u7684\u6807\u51c6\u7684\u5b8c\u6574\u5217\u8868\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e26\u7528\u6237\u540d\u7684\u57fa\u672c\u804a\u5929\u5e94\u7528\u7a0b\u5e8f<\/li>\n\n\n\n<li>\u4efb\u4f55\u7528\u6237\u90fd\u5fc5\u987b\u80fd\u591f\u52a0\u5165\u4efb\u4f55\u5176\u4ed6\u7528\u6237\uff08\u5728\u804a\u5929\u5ba4\u4e2d\uff09<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u6570\u636e\u4ea4\u6362\u5fc5\u987b\u52a0\u5bc6<\/li>\n\n\n\n<li><strong>\u6301\u4e45\u6027\uff1a<\/strong>\u6570\u636e\u5fc5\u987b\u5728\u6574\u4e2a\u4f1a\u8bdd\u4e2d\u4fdd\u5b58\uff08\u7528\u6237\u540d\u3001\u804a\u5929\uff09<\/li>\n\n\n\n<li><strong>\u65e0\u670d\u52a1\u5668\uff1a<\/strong>\u6570\u636e\u5fc5\u987b\u5728\u4e0d\u9700\u8981\u670d\u52a1\u5668\u7684\u60c5\u51b5\u4e0b\u5728\u5bf9\u7b49\u70b9\u4e4b\u95f4\u4ea4\u6362<\/li>\n\n\n\n<li><strong>\u7b80\u5355\u6027\uff1a<\/strong>\u4efb\u4f55\u4eba\u90fd\u5fc5\u987b\u80fd\u591f\u5728\u4e0d\u4e86\u89e3\u4efb\u4f55\u4e1c\u897f\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u8fd9\u4e2a\u9879\u76ee<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"4a77\">\u89e3\u51b3\u65b9\u6848<\/h1>\n\n\n\n<p id=\"3241\">\u6211\u8bbe\u8ba1\u4e86\u6700\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u6765\u6ee1\u8db3\u4ee5\u524d\u7684\u8981\u6c42\uff0c\u4f7f\u7528\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/webrtc.org\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>WebRTC<\/strong><\/a>\uff0c\u7528\u4e8e\u5b89\u5168\u3001\u53ef\u9760\u548c\u65e0\u670d\u52a1\u5668\u7684\u6570\u636e\u4ea4\u6362<\/li>\n\n\n\n<li><a href=\"https:\/\/vuejs.org\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>Vue.js<\/strong><\/a>\uff0c\u7528\u4e8e\u5feb\u901f\u548c\u8f7b\u91cf\u7ea7\u7684\u52a8\u6001\u6e32\u67d3\u548c\u72b6\u6001\u7ba1\u7406<\/li>\n\n\n\n<li><a href=\"https:\/\/peerjs.com\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>PeerJS<\/strong><\/a>\uff0c\u7528\u4e8e\u7b80\u5316 WebRTC \u7684\u5f00\u53d1<\/li>\n\n\n\n<li><a href=\"https:\/\/materializecss.com\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>MaterializeCSS<\/strong><\/a>\uff0c\u7528\u4e8e\u5feb\u901f\u548c\u73b0\u4ee3\u7684\u8bbe\u8ba1<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/localStorage\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>JavaScript localStorage<\/strong><\/a>\uff0c\u7528\u4e8e\u6570\u636e\u6301\u4e45\u5316<\/li>\n<\/ul>\n\n\n\n<p id=\"97c2\">WebRTC \u662f Google \u7684\u5f00\u6e90\u57fa\u7840\uff0c\u7528\u4e8e\u6d4f\u89c8\u5668\u4e2d\u7684\u5bf9\u7b49\u901a\u4fe1\uff0c\u4f7f\u7528<a href=\"https:\/\/en.wikipedia.org\/wiki\/QUIC\" rel=\"noreferrer noopener\" target=\"_blank\">QUIC \u534f\u8bae<\/a>\uff08\u6700\u65b0\u3001\u5feb\u901f\u4e14\u53ef\u9760\u7684<a href=\"https:\/\/en.wikipedia.org\/wiki\/Transport_layer\" rel=\"noreferrer noopener\" target=\"_blank\">\u4f20\u8f93\u5c42<\/a>&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Network_protocol\" rel=\"noreferrer noopener\" target=\"_blank\">\u7f51\u7edc\u534f\u8bae<\/a>\uff09\u3002\u51e0\u4e4e\u6240\u6709\u50cf\u6837\u7684\u6d4f\u89c8\u5668\u90fd\u652f\u6301\u5b83\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728 2 \u4e2a\u6d4f\u89c8\u5668\uff08\u5bf9\u7b49\uff09\u4e4b\u95f4\u5efa\u7acb\u6570\u636e\u901a\u9053\u7684\u65b9\u6cd5\uff0c\u652f\u6301\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u5b9e\u65f6\u97f3\u9891\u548c\u89c6\u9891\u3002<\/p>\n\n\n\n<p id=\"59ac\">PeerJS \u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u5ba2\u6237\u7aef\u8fde\u63a5\u5230\u670d\u52a1\u5668\u7aef\u70b9\u7684\u65b9\u6cd5\uff0c\u7b49\u5f85\u6765\u81ea\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u5bf9\u7b49\u8fde\u63a5\u3002\u5efa\u7acb\u8fde\u63a5\u540e\uff0c\u4e24\u4e2a\u5bf9\u7b49\u65b9\u5c06\u901a\u8fc7 WebRTC \u6570\u636e\u901a\u9053\u8fdb\u884c\u901a\u4fe1\uff0c\u53ea\u6709\u4ed6\u4eec\u53ef\u4ee5\u4ece\u4e2d\u8bfb\u53d6\/\u5199\u5165\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"44dd\">\u7f16\u7801<\/h1>\n\n\n\n<p id=\"64e5\">\u8ba9\u6211\u4eec\u4ece\u521b\u5efa\u7f51\u9875\u7684\u9aa8\u67b6\u5f00\u59cb\uff1a\u56e0\u4e3a\u6211\u4eec\u60f3\u8981\u4e00\u4e2a\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u5c06\u628a\u6240\u6709\u7684 CSS \u4ee3\u7801<strong>\u548c<\/strong>JS \u4ee3\u7801\u5199\u5728\u4e00\u4e2a index.html \u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u8fd8\u9700\u8981\u5728\u6211\u4eec\u7684 html \u4e2d\u5bfc\u5165\u6bcf\u4e2a\u4f9d\u8d56\u9879\u3002\u6211\u4eec\u7684\u8eab\u4f53\u5c06\u5305\u542b\u4e00\u4e2a\u4e3b\u8981\u7684<em>#app<\/em>&nbsp;div\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\n&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n    &lt;head>\n        &lt;title>Serverless, secure &amp; persistent multichat&lt;\/title>\n\n        &lt;!-- To efficiently implement sockets -->\n        &lt;script src=\"https:\/\/unpkg.com\/peerjs@1.2.0\/dist\/peerjs.min.js\">&lt;\/script>\n        &lt;!-- To easily manage dynamic rendering -->\n        &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\">&lt;\/script>\n\n        &lt;!-- Integrate font styles -->\n        &lt;link href=\"https:\/\/fonts.googleapis.com\/css2?family=Red+Hat+Display&amp;display=swap\" rel=\"stylesheet\">\n\n        &lt;!-- Material Design stylesheets -->\n        &lt;link href=\"https:\/\/fonts.googleapis.com\/icon?family=Material+Icons\" rel=\"stylesheet\">\n        &lt;link rel=\"stylesheet\" href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/materialize\/1.0.0\/css\/materialize.min.css\">\n        &lt;script async src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/materialize\/1.0.0\/js\/materialize.min.js\">&lt;\/script>\n\n        &lt;style>\n        &lt;\/style>\n    &lt;\/head>\n\n    &lt;body>\n        &lt;div id=\"app\">\n        &lt;\/div>\n    &lt;\/body>\n  \n    &lt;script>\n    &lt;\/script>\n&lt;\/html>\nview rawindex.html hosted with \u2764 by GitHub<\/code><\/pre>\n\n\n\n<p id=\"3c39\">\u7136\u540e\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u7528\u6237\u7ba1\u7406 2 \u4e2a\u5c4f\u5e55\uff1a\u767b\u5f55\u5c4f\u5e55\u548c\u804a\u5929\u5c4f\u5e55\uff08\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u5728\u767b\u5f55\u540e\u66f4\u6539\u5176\u7528\u6237\u540d\uff09\u3002\u4e3a\u6b64\uff0c\u6211\u9009\u62e9\u5728\u6211\u7684\u5e94\u7528\u7a0b\u5e8f\u72b6\u6001\u4e2d\u5b58\u50a8\u4e00\u4e2a\u5c4f\u5e55\u53d8\u91cf\uff0c\u8be5\u503c\u6307\u793a\u8981\u663e\u793a\u7684\u5c4f\u5e55\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"57ef\">\u4f60\u4e0d\u5e94\u8be5\u8fd9\u6837\u505a\uff01\u901a\u5e38\uff0c\u6211\u4eec\u4e3a 2 \u4e2a\u4e0d\u540c\u7684\u5c4f\u5e55\u4e2d\u7684\u6bcf\u4e00\u4e2a\u521b\u5efa 2 \u4e2a\u4e0d\u540c\u7684\u7ec4\u4ef6\uff0c\u5e76\u4f7f\u7528 Vue \u7684\u8def\u7531\u5668\u5728\u5b83\u4eec\u4e4b\u95f4\u5207\u6362\u3002\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u9009\u62e9\u7b80\u5355\u5730\u521b\u5efa\u4e00\u4e2a\u7ec4\u4ef6\u6765\u5141\u8bb8\u6211\u76f4\u63a5\u8bbf\u95ee\u6211\u5e94\u7528\u7a0b\u5e8f\u7684\u6240\u6709\u72b6\u6001\uff01<\/p>\n<\/blockquote>\n\n\n\n<p id=\"4bb1\">\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u6211\u4eec\u7684 2 \u4e2a\u5c4f\u5e55\uff0c\u6211\u4eec\u5c06\u5b83\u4eec\u653e\u5728<em>#app \u4e2d<\/em>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\n&lt;div id=\"login\" v-if=\"screen === 'login'\">\n    &lt;div class=\"container\">\n        &lt;h1>Enter your username&lt;\/h1>\n\n        &lt;div class=\"row\">\n            &lt;form v-on:submit=\"submitLogin\">\n                &lt;div class=\"input-field col s8\">\n                    &lt;input :disabled=\"loading\" id=\"username\" type=\"text\" v-model=\"usernameInput\" \/>\n                    &lt;label for=\"username\">My username&lt;\/label>\n                &lt;\/div>\n                &lt;button v-bind:class=\"{ disabled: loading }\" class=\"btn-large waves-effect waves-light red col s4\" type=\"submit\">\n                    Login&lt;i class=\"material-icons right\">login&lt;\/i>\n                &lt;\/button>\n            &lt;\/form>\n        &lt;\/div>\n\n        &lt;p v-if=\"peerError\" style=\"color: red\">{{ peerError }}&lt;\/p>\n    &lt;\/div>\n&lt;\/div>\n&lt;div id=\"chat\" v-if=\"screen === 'chat'\">\n    &lt;div class=\"container\">\n        &lt;h1>Multichat&lt;\/h1>\n        &lt;p>Your username: {{ usernameInput }}&lt;\/p>\n    &lt;\/div>\n\n    &lt;div class=\"row\">\n        &lt;div id=\"users\" class=\"col s12 m5\">\n            &lt;div class=\"row\">\n                &lt;form v-on:submit=\"submitConnection\">\n                    &lt;div class=\"input-field col s8\">\n                        &lt;input :disabled=\"loading\" id=\"target_id\" type=\"text\" v-model=\"targetIdInput\" \/>\n                        &lt;label for=\"target_id\">Target username&lt;\/label>\n                    &lt;\/div>\n                    &lt;button v-bind:class=\"{ disabled: loading }\" class=\"btn-large waves-effect waves-light col s4\" type=\"submit\">\n                        Connect&lt;i class=\"material-icons right\">login&lt;\/i>\n                    &lt;\/button>\n                &lt;\/form>\n\n                &lt;div class=\"col s12\">\n                    &lt;em v-if=\"peerError\" style=\"color: red\">{{ peerError }}&lt;\/em>\n                &lt;\/div>\n\n                &lt;div class=\"col s12\">\n                    &lt;h4>Connected users&lt;\/h4>\n                    &lt;p>&lt;i class=\"material-icons\">portrait&lt;\/i>{{ usernameInput }}&lt;\/p>\n                    &lt;p v-for=\"peerId in peerIds\">\n                        &lt;i class=\"material-icons\">portrait&lt;\/i>{{ getUsername(peerId) }}\n                    &lt;\/p>\n                &lt;\/div>\n            &lt;\/div>\n        &lt;\/div>\n\n        &lt;div class=\"col s12 m7\">\n            &lt;h4>Chatbox&lt;\/h4>\n            &lt;div id=\"chatbox\">\n                &lt;p v-for=\"chat in chats\">{{ chat.sender }}: {{ chat.message }}&lt;\/p>\n            &lt;\/div>\n            &lt;div class=\"row\">\n                &lt;form v-on:submit=\"submitChat\">\n                    &lt;div class=\"input-field col s10\">\n                        &lt;input id=\"chat_message\" type=\"text\" v-model=\"chatMessageInput\" \/>\n                        &lt;label for=\"chat_message\">Your message&lt;\/label>\n                    &lt;\/div>\n                    &lt;button class=\"btn-floating btn-large waves-effect waves-light red\" type=\"submit\">\n                        &lt;i class=\"material-icons\">send&lt;\/i>\n                    &lt;\/button>\n                &lt;\/form>\n            &lt;\/div>\n        &lt;\/div>\n    &lt;\/div>\n&lt;\/div>\n<\/code><\/pre>\n\n\n\n<p id=\"6bee\">\u5982\u679c\u5ffd\u7565\u6240\u6709\u6837\u5f0f\u7c7b\u548c\u56fe\u6807\uff0c\u60a8\u4f1a\u53d1\u73b0\u6211\u4eec\u7684\u5c4f\u5e55\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e9b Vue \u53d8\u91cf\u548c\u51fd\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>screen<\/em>\uff1a\u5305\u542b\u663e\u793a\u5c4f\u5e55\u540d\u79f0\u7684\u53d8\u91cf<\/li>\n\n\n\n<li><em>usernameInput<\/em>&nbsp;: \u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\uff08\u767b\u5f55\u540e\u5fc5\u987b\u4fdd\u6301\u4e0d\u53d8\uff09<\/li>\n\n\n\n<li><em>peerError<\/em>\uff1a\u53d1\u751f\u9519\u8bef\u65f6\u663e\u793a\u7684\u6d88\u606f<\/li>\n\n\n\n<li><em>loading<\/em>\uff1a\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u6b63\u5728\u52a0\u8f7d<\/li>\n\n\n\n<li><em>targetIdInput<\/em>\uff1a\u8981\u8fde\u63a5\u5230\u7684\u76ee\u6807\u7528\u6237\u540d\u8f93\u5165<\/li>\n\n\n\n<li><em>peerIds<\/em>\uff1a\u6211\u4eec\u8fde\u63a5\u5230\u7684\u5bf9\u7b49 ID \u5217\u8868<\/li>\n\n\n\n<li><em>chats<\/em>\uff1a\u804a\u5929\u5bf9\u8c61\u5217\u8868\uff08\u53d1\u4ef6\u4eba\uff0c\u6d88\u606f\uff09<\/li>\n\n\n\n<li><em>chatMessageInput<\/em>&nbsp;: \u7528\u6237\u5f53\u524d\u8f93\u5165\u7684\u804a\u5929\u6d88\u606f<\/li>\n\n\n\n<li><em>submitLogin<\/em>\uff1a\u5728\u767b\u5f55\u8868\u5355\u63d0\u4ea4\u65f6\u8c03\u7528\uff0c\u5e76\u89e6\u53d1\u9002\u5f53\u7684 javascript \u4e8b\u4ef6<\/li>\n\n\n\n<li><em>submitConnection<\/em>\uff1a\u5728\u63d0\u4ea4\u8fde\u63a5\u8868\u5355\u65f6\u8c03\u7528\uff0c\u4ee5\u8fde\u63a5\u5230\u76ee\u6807\u5ba2\u6237\u7aef (&nbsp;<em>targetIdInput<\/em>&nbsp;)<\/li>\n\n\n\n<li><em>submitChat<\/em>\uff1a\u53d1\u9001\u804a\u5929\u6d88\u606f\u65f6\u8c03\u7528\uff08\u901a\u8fc7\u804a\u5929\u8868\u5355\u63d0\u4ea4\uff09<\/li>\n<\/ul>\n\n\n\n<p id=\"271e\">\u6709\u4e86\u6240\u6709\u8fd9\u4e9b\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fd0\u884c\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e86\u3002\u8ba9\u6211\u4eec\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u7684\u5b9e\u4f8b\u5316\u548c\u6e32\u67d3\u903b\u8f91\uff01\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u4e2a\u7528\u6237\u90fd\u6709\u4e00\u4e2a\u7528\u6237\u540d\uff0c\u6bcf\u4e2a\u5bf9\u7b49\u70b9\uff08\u6d4f\u89c8\u5668\uff09\u90fd\u6709\u4e00\u4e2a ID\uff0c\u7528\u4e8e\u5728 PeerJS \u4f7f\u7528\u7684\u4fe1\u4ee4\u670d\u52a1\u5668\u4e0a\u552f\u4e00\u6807\u8bc6\u5b83\u3002\u7528\u6237\u540d\u548c\u5bf9\u7b49 ID \u4f1a\u5f88\u65b9\u4fbf\u3002\u4f46\u662f\u4e3a\u4e86\u786e\u4fdd\u5927\u89c4\u6a21\u7684\u552f\u4e00\u6027\u548c\u53ef\u7528\u6027\uff08\u4fe1\u4ee4\u670d\u52a1\u5668\u4e0d\u4ec5\u7531\u6211\u4eec\u4f7f\u7528\uff09\uff0c\u6211\u4eec\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u7528\u6237\u540d\u6dfb\u52a0\u4e00\u4e2a\u552f\u4e00\u7684\u524d\u7f00\uff0c\u4ee5\u4fbf\u6211\u4eec\u83b7\u5f97\u51e0\u4e4e\u552f\u4e00\u7684\u5bf9\u7b49 ID\u3002<br>\u4e3a\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u7f16\u5199 2 \u4e2a\u5b9e\u7528\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5c06\u5bf9\u7b49 ID \u8f6c\u6362\u4e3a\u7528\u6237\u540d\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u6211\u4eec\u5c06\u5b83\u4eec\u7f16\u5199\u4e3a Vue \u7ec4\u4ef6\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u80fd\u591f\u5728\u6e32\u67d3\u65f6\u4f7f\u7528\u5b83\u4eec\u3002<\/li>\n\n\n\n<li>\u6211\u4eec\u60f3\u8981\u6301\u4e45\u7684\u804a\u5929\uff0c\u6211\u4eec\u9009\u62e9\u5c06\u6570\u636e\u4fdd\u5b58\u4e3a cookie\u3002\u4e0d\u5e78\u7684\u662f\uff0ccookie \u53ea\u5141\u8bb8\u4fdd\u5b58\u5b57\u7b26\u4e32\u3002<br>\u591a\u4e48\u5de7\u5408\uff0cJavaScript \u672c\u8eab\u5c31\u5141\u8bb8\u6211\u4eec\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316 JSON \u5bf9\u8c61\uff01\u8ba9\u6211\u4eec\u4ece\u201c\u804a\u5929\u201dcookie \u4e2d\u8bfb\u53d6\u4e4b\u524d\u5b58\u50a8\u7684\u804a\u5929\u8bb0\u5f55\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 cookie \u6765\u5b58\u50a8\u6211\u4eec\u4e4b\u524d\u4f7f\u7528\u7684\u7528\u6237\u540d\uff0c\u4ee5\u4fbf\u5728\u6211\u4eec\u7684\u4e0b\u4e00\u4e2a\u4f1a\u8bdd\u4e2d\u542f\u52a8\u5b83\uff08\u8fd9\u5c31\u662f\u4e0b\u9762\u6240\u505a\u7684\uff01\uff09<\/li>\n\n\n\n<li>\u6211\u4eec\u5e0c\u671b\u6211\u4eec\u7684\u804a\u5929\u6846\u5728\u6536\u5230\u65b0\u804a\u5929\u6d88\u606f\u65f6\u81ea\u52a8\u5411\u4e0b\u6eda\u52a8\uff1a\u6bcf\u5f53<em>\u804a\u5929<\/em>\u53d8\u91cf\u66f4\u65b0\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u804a\u5929\u6846\u6eda\u52a8\u5230\u6700\u5927\u4f4d\u7f6e\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528 Vue \u7684\u89c2\u5bdf\u8005\uff0c\u5c06\u89c2\u5bdf\u8005\u547d\u540d\u4e3a<em>chats()<\/em><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\nconst appPrefix = \"secure-p2p-multichat-\"; \/\/ the prefix we will preprend to usernames\n\nconst oldChats = localStorage.getItem(\"chats\");\nconst chats = oldChats ? JSON.parse(oldChats) : &#91;]; \/\/ oldChats may be undefined, which throws error if passed into JSON.parse\n\nconst app = new Vue({\n    el: \"#app\",\n    data: {\n        screen: \"login\", \/\/ initialize at login screen\n        usernameInput: localStorage.getItem(\"username\"), \/\/ to load saved username\n        peerError: \"\",\n        loading: false,\n        peer: {}, \/\/ initialize as empty object instead of undefined\n        targetIdInput: \"\",\n        peerIds: &#91;], \/\/ connected to nobody at first\n        connections: {}, \/\/ maps peerIds to their correspondig PeerJS's DataConnection objects\n        chats,\n        chatMessageInput: \"\"\n    },\n    watch: {\n        chats: function () {\n            const chatbox = document.getElementById(\"chatbox\");\n            if (chatbox) chatbox.scrollTop = 99999999; \/\/ to automatically scroll the chatbox to the most recent chat message\n        }\n    },\n    methods: {\n        \/\/ util functions to convert username to peer ids and vice-versa\n        getPeerId: username => appPrefix + username,\n        getUsername: peerId => peerId ? peerId.slice(appPrefix.length) : \"\",\n        submitLogin: function (event) {\n            if (event) event.preventDefault(); \/\/ to prevent default behavior which is to POST to the same page\n\n            if (this.usernameInput.length > 0 &amp;&amp; !this.loading) {\n                this.loading = true; \/\/ update status\n                this.peerError = \"\"; \/\/ reset error status\n\n                localStorage.setItem(\"username\", this.usernameInput); \/\/ set username cookie to instanciate it at the next session\n\n                this.createPeer();\n            }\n        },\n        submitConnection: function (event) {\n            event.preventDefault(); \/\/ to prevent default behavior which is to POST to the same page\n\n            const peerId = this.getPeerId(this.targetIdInput); \/\/ the peer's id we want to connect to\n            this.initiateConnection(peerId);\n        },\n        receiveChat: function (chat) {\n            this.chats.push(chat);\n            localStorage.setItem(\"chats\", JSON.stringify(this.chats));\n        },\n        submitChat: function (event) {\n            event.preventDefault(); \/\/ to prevent default behavior which is to POST to the same page\n\n            if (this.chatMessageInput.length > 0) {\n                \/\/ the chat object's data\n                const chat = {\n                    sender: this.usernameInput,\n                    message: this.chatMessageInput,\n                    timestamp: new Date().getTime()\n                };\n\n                this.receiveChat(chat); \/\/ simulate receiving a chat\n                \/\/ send chat object to connected users\n                Object.values(this.connections).forEach(conn => {\n                    conn.send({\n                        type: \"chat\",\n                        chat\n                    });\n                });\n\n                this.chatMessageInput = \"\"; \/\/ reset chat message input\n            }\n        }\n    }\n});\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"177a\">\u5feb\u5230\u4e86\uff01<\/h2>\n\n\n\n<p id=\"729d\">\u8ba9\u6211\u4eec\u901a\u8fc7\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u540e\u53f0\u903b\u8f91\u3001\u5b9e\u4f8b\u5316\u548c\u8fde\u63a5\u5bf9\u7b49\u70b9\u6765\u5b8c\u6210\u8fd9\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4f46\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6211\u4eec\u5fc5\u987b\u8ddf\u8e2a\u6211\u4eec\u62e5\u6709\u7684\u6bcf\u4e2a\u6d3b\u52a8\u8fde\u63a5\u5e76\u5c06\u5b83\u4eec\u6620\u5c04\u5230\u76f8\u5e94\u7684\u8fdc\u7a0b\u5bf9\u7b49 ID\u3002<br><em>\u4f46\u662f\u6211\u4eec\u5df2\u7ecf\u5b58\u50a8\u4e86\u6d3b\u52a8\u8fde\u63a5\u7684\u8fdc\u7a0b\u5bf9\u7b49 ID\uff01\u4e0d\u662f\u91cd\u88c5\u5417\uff1f<br><\/em>\u4e0d\u5e78\u7684\u662f\uff0cVue \u6ca1\u6709\u63d0\u4f9b\u4e00\u79cd\u76f4\u63a5\u7684\u65b9\u6cd5\u6765\u8ddf\u8e2a\u8d4b\u4e88 Vue \u6570\u636e\u5bf9\u8c61\u7684\u4efb\u4f55\u65b0\u5c5e\u6027\u3002\u6211\u7684\u9009\u62e9\u662f\u5c06\u8fdc\u7a0b\u5bf9\u7b49 ID \u6620\u5c04\u5230 Vue \u6570\u636e\u5bf9\u8c61 (&nbsp;<em>connections<\/em>&nbsp;) \u4e2d\u7684\u76f8\u5e94\u8fde\u63a5\uff0c\u4ee5\u53ca\u5728\u53e6\u4e00\u4e2a Vue \u6570\u636e\u6570\u7ec4 (&nbsp;<em>peerIds<\/em>&nbsp;) \u4e2d\u72ec\u7acb\u6620\u5c04\u8fdc\u7a0b\u5bf9\u7b49 ID\uff08\u66f4\u65b0\u89e6\u53d1 Vue \u7ec4\u4ef6\u6e32\u67d3\uff09\u3002\u53e6\u4e00\u79cd\u9009\u62e9\u662f\u5728\u6bcf\u6b21\u6dfb\u52a0\u8fde\u63a5\u65f6\u6ce8\u518c\u6211\u4eec\u6dfb\u52a0\u5230<em>\u8fde\u63a5\u7684\u5c5e\u6027\uff08<\/em>\u4f7f\u7528<a href=\"https:\/\/vuejs.org\/v2\/api\/#Vue-set\" rel=\"noreferrer noopener\" target=\"_blank\"><em>Vue.set<\/em><\/a><em>\uff09 \uff0c\u8fd9\u5c06\u89e6\u53d1\u7ec4\u4ef6\u6e32\u67d3\u3002<\/em><\/li>\n\n\n\n<li>\u6211\u4eec\u5fc5\u987b\u8003\u8651\u540c\u4f34\u5173\u7cfb\uff1b\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u8fd9\u91cc\u6ca1\u6709\u63a5\u53d7\/\u62d2\u7edd\u8fde\u63a5\u3002<br>\u8ba9\u6211\u4eec\u8003\u8651\u804a\u5929\u5ba4<strong>A\u4e2d\u7684<\/strong><strong>Alice<\/strong>\u548c<strong>Everyone<\/strong>\u4ee5\u53ca\u804a\u5929\u5ba4<strong>B\u4e2d\u7684<\/strong><strong>Bob<\/strong>\u548c<strong>Carole<\/strong>\u3002\u4e00\u4e2a\u8fde\u63a5\u8fc7\u7a0b\u5c31\u662f\uff1a<strong><\/strong><strong><\/strong><strong><\/strong><strong><\/strong><\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><\/strong><strong>\u804a\u5929\u5ba4A \u7684<\/strong><strong>Alice<\/strong>\u5411\u804a\u5929\u5ba4<strong>B\u7684<\/strong><strong>Bob<\/strong>\u53d1\u9001\u4e00\u4e2a\u62a5\u4ef7\uff0c\u5176\u4e2d\u5305\u542b\u804a\u5929\u5ba4<strong>A<\/strong>\u4e2d\u5df2\u8fde\u63a5\u7528\u6237\u7684\u5217\u8868\uff08[<strong>\u6240\u6709\u4eba<\/strong>]\uff09<strong><\/strong><strong><\/strong><strong><\/strong><\/li>\n\n\n\n<li><strong>Bob<\/strong>\u56de\u7b54\u4e86<strong>Alice\u7684\u63d0\u8bae\uff0c\u5e76\u4ece<\/strong><strong>Bob<\/strong>\u672a\u8fde\u63a5\u7684\u804a\u5929\u5ba4<strong>A\u5411<\/strong><strong>Everyone<\/strong>\u53d1\u9001\u63d0\u8bae\uff0c\u5e76\u5728\u804a\u5929\u5ba4<strong>B<\/strong>\u4e2d\u5217\u51fa\u4e86\u5df2\u8fde\u63a5\u7684\u7528\u6237\uff08[&nbsp;<strong>Carole<\/strong>&nbsp;]\uff09<strong><\/strong><strong><\/strong><strong><\/strong><strong><\/strong><\/li>\n\n\n\n<li><strong>Alice<\/strong>\u73b0\u5728\u8fde\u63a5\u5230<strong>Bob<\/strong>\u5e76\u5411<strong>Carole\u53d1\u9001\u62a5\u4ef7\uff0c\u5176\u4e2d\u5305\u542b<\/strong><strong>Alice<\/strong>\u8fde\u63a5\u5230\u7684\u7528\u6237\u5217\u8868([&nbsp;<strong>Bob, Everyone<\/strong>&nbsp;])<\/li>\n\n\n\n<li><strong>Everyone<\/strong>\u56de\u7b54<strong>Bob<\/strong>\u7684 offer\uff0c<strong>\u9644\u4e0a Everyone<\/strong>\u8fde\u63a5\u5230\u7684\u7528\u6237\u5217\u8868\uff08[&nbsp;<strong>Alice<\/strong>&nbsp;]\uff09<\/li>\n\n\n\n<li><strong>Carole<\/strong>\u56de\u7b54\u4e86<strong>Alice<\/strong>\u7684\u63d0\u8bae\uff0c\u5e76\u5217\u51fa\u4e86<strong>Carole<\/strong>\u8fde\u63a5\u5230\u7684\u7528\u6237\u5217\u8868 ([&nbsp;<strong>Bob<\/strong>&nbsp;])<\/li>\n\n\n\n<li><strong>Carole<\/strong>\u73b0\u5728\u8fde\u63a5\u5230<strong>Alice<\/strong>\u5e76\u5411<strong>Everyone\u53d1\u9001\u62a5\u4ef7\uff0c\u5176\u4e2d\u5305\u542b<\/strong><strong>Carole<\/strong>\u8fde\u63a5\u5230\u7684\u7528\u6237\u5217\u8868([&nbsp;<strong>Alice, Bob<\/strong>&nbsp;])<\/li>\n\n\n\n<li><strong>\u6bcf\u4e2a\u4eba\u90fd<\/strong>\u56de\u7b54<strong>Carole<\/strong>\u7684\u63d0\u8bae\uff0c\u5e76\u9644\u4e0a<strong>\u6bcf\u4e2a\u4eba<\/strong>\u90fd\u8fde\u63a5\u5230\u7684\u7528\u6237\u5217\u8868 ([&nbsp;<strong>Alice, Bob<\/strong>&nbsp;])<\/li>\n\n\n\n<li><strong>Carole<\/strong>\u73b0\u5728\u8fde\u63a5\u5230<strong>\u6bcf\u4e2a\u4eba<\/strong>\uff08\u5b57\u9762\u610f\u601d\uff09<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6700\u540e\uff0c\u6211\u4eec\u5fc5\u987b\u4e3a\u6211\u4eec\u7684\u5bf9\u7b49\u901a\u4fe1\u5efa\u7acb\u6570\u636e\u683c\u5f0f\u3002<br>\u5bf9\u4e8e\u8fd9\u4e2a\u9879\u76ee\uff0c\u53ea\u8981\u8bf4\u4efb\u4f55\u901a\u4fe1\u7684\u6570\u636e\u5fc5\u987b\u5305\u542b<em>\u7c7b\u578b<\/em>\u5c5e\u6027\u5c31\u8db3\u591f\u4e86\u3002\u6211\u4eec\u5c06\u6839\u636e\u6b64\u5c5e\u6027\u7684\u503c\u4ee5\u4e0d\u540c\u65b9\u5f0f\u67e5\u8be2\u6211\u4eec\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"bd9e\">\u59cb\u7ec8\u8003\u8651 2 \u4e2a\u901a\u4fe1\u7aef\u70b9\u4e4b\u95f4\u7684\u6570\u636e\u683c\u5f0f\u662f\u4e00\u4e2a\u6709\u7528\u7684\u4e60\u60ef\uff0c\u8fd9\u6837\u60a8\u5c31\u6709\u4e86\u5f39\u6027\u7684\u884c\u4e3a\/\u89e3\u91ca\u3002\u60a8\u4e0d\u5e0c\u671b\u67e5\u8be2\u610f\u5916\u629b\u51fa\u9519\u8bef\u7684 API\uff01\u53e6\u5916\uff1a\u5982\u679c\u60a8\u9700\u8981\u6765\u81ea\u540c\u4e00\u7aef\u70b9\u7684\u4e0d\u540c\u884c\u4e3a\uff0c\u4e5f\u8bb8\u60a8\u5e94\u8be5\u5c06\u7aef\u70b9\u62c6\u5206\u4e3a 2 \u4e2a\u4e0d\u540c\u7684\u7aef\u70b9\uff0c\u6bcf\u4e2a\u7aef\u70b9\u90fd\u6709\u81ea\u5df1\u7684\u539f\u5b50\u89d2\u8272\uff01<\/p>\n<\/blockquote>\n\n\n\n<p id=\"211b\">\u60a8\u4f1a\u5728\u4e0b\u9762\u627e\u5230\u6211\u4eec\u7684\u4e3b\u8981 Vue \u7ec4\u4ef6\u4e2d\u7f3a\u5c11\u7684\u65b9\u6cd5\u5217\u8868\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u5b8c\u5168\u5b9e\u73b0\u4e0a\u8ff0\u903b\u8f91\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>initiateConnection<\/em>\u5728\u63d0\u4f9b\u8fde\u63a5\u65f6\u88ab\u8c03\u7528\u3002<br>\u5b83\u5fc5\u987b\u5b9e\u73b0\u8fde\u63a5\u7528\u6237\u53d1\u9001\u7684\u521d\u59cb\u5217\u8868\uff08\u4f5c\u4e3a\u8fde\u63a5\u5143\u6570\u636e\uff09\uff01<\/li>\n\n\n\n<li><em>configureConnection\u5728\u63d0\u4f9b<\/em><strong>\u548c<\/strong>\u5e94\u7b54\u8fde\u63a5\u65f6\u8c03\u7528\uff08\u7528\u4e8e\u6ce8\u518c\u6570\u636e\u548c\u884c\u4e3a\u4fa6\u542c\u5668\uff09<br>\u5b83\u5fc5\u987b\u4f7f\u7528\u6536\u5230\u7684\u8fde\u63a5\u7528\u6237\u5217\u8868\uff08\u4f5c\u4e3a\u8fde\u63a5\u5143\u6570\u636e\uff09\u5b9e\u73b0\u8fde\u63a5\u542f\u52a8\uff01<\/li>\n\n\n\n<li><em>createPeer<\/em>\u8fd8\u5904\u7406 Peer \u914d\u7f6e\uff0c\u5b83\u5b9e\u8d28\u4e0a\u662f\u5728\u6536\u5230\u62a5\u4ef7\u65f6\u5b9e\u73b0\u81ea\u52a8\u5e94\u7b54\uff08\u5982\u679c\u6211\u4eec\u60f3\u5b9e\u73b0\u63a5\u53d7\/\u62d2\u7edd\u5c4f\u5e55\uff0c\u6211\u4eec\u5c06\u505c\u7528\u5b83\uff09<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\n\/\/ we keep track of connections ourselves as suggested in peerjs's documentation\naddConnection: function (conn) {\n    this.connections&#91;conn.peer] = conn;\n    this.updatePeerIds();\n\n    console.log(`Connected to ${conn.peer}!`);\n},\nremoveConnection: function (conn) {\n    delete this.connections&#91;conn.peer];\n    this.updatePeerIds();\n},\nupdatePeerIds: function () {\n    this.peerIds = Object.keys(this.connections);\n},\n\n\/\/ called to properly configure connection's client listeners\nconfigureConnection: function (conn) {\n    conn.on(\"data\", data => {\n        \/\/ if data is about connections (the list of peers sent when connected)\n        if (data.type === \"connections\") {\n            data.peerIds.forEach(peerId => {\n                if (!this.connections&#91;peerId]) {\n                    this.initiateConnection(peerId);\n                }\n            });\n        } else if (data.type === \"chat\") {\n            this.receiveChat(data.chat);\n        }\n        \/\/ please note here that if data.type is undefined, this endpoint won't do anything!\n    });\n    conn.on(\"close\", () => this.removeConnection(conn));\n    conn.on(\"error\", () => this.removeConnection(conn));\n\n    \/\/ if the caller joins have a call, we merge calls\n    conn.metadata.peerIds.forEach(peerId => {\n        if (!this.connections&#91;peerId]) {\n            this.initiateConnection(peerId);\n        }\n    });\n},\n\/\/ called to initiate a connection (by the caller)\ninitiateConnection: function (peerId) {\n    if (!this.peerIds.includes(peerId) &amp;&amp; peerId !== this.peer.id) {\n        this.loading = true;\n        this.peerError = \"\";\n\n        console.log(`Connecting to ${peerId}...`);\n\n        const options = {\n            metadata: {\n                \/\/ if the caller has peers, we send them to merge calls\n                peerIds: this.peerIds\n            },\n            serialization: \"json\"\n        };\n        const conn = this.peer.connect(peerId, options);\n        this.configureConnection(conn);\n\n        conn.on(\"open\", () => {\n            this.addConnection(conn);\n            if (this.getUsername(conn.peer) === this.targetIdInput) {\n                this.targetIdInput = \"\";\n                this.loading = false;\n            }\n        });\n    }\n},\ncreatePeer: function () {\n    \/\/ options are useful in development to connect to local peerjs server\n    this.peer = new Peer(this.getPeerId(this.usernameInput)\/*, {\n        host: 'localhost',\n        port: 8080,\n        path: 'app'\n    }*\/);\n\n    \/\/ when peer is connected to signaling server\n    this.peer.on(\"open\", () => {\n        this.screen = \"chat\"; \/\/ changing screen\n        this.loading = false;\n        this.peerError = \"\";\n    });\n    \/\/ error listener\n    this.peer.on(\"error\", error => {\n        if (error.type === \"peer-unavailable\") { \/\/ if connection with new peer can't be established\n            this.loading = false;\n            this.peerError = `${this.targetIdInput} is unreachable!`; \/\/ custom error message\n            this.targetIdInput = \"\";\n        } else if (error.type === \"unavailable-id\") { \/\/ if requested id (thus username) is already taken\n            this.loading = false;\n            this.peerError = `${this.usernameInput} is already taken!`; \/\/ custom error message\n        } else this.peerError = error; \/\/ default error message\n    });\n\n    \/\/ automatic answer and merge when peer receives a connection\n    this.peer.on('connection', conn => {\n        if (!this.peerIds.includes(conn.peer)) {\n            this.configureConnection(conn);\n\n            conn.on(\"open\", () => {\n                this.addConnection(conn);\n\n                \/\/ send every connection previously established to offerer (to merge chat rooms)\n                conn.send({\n                    type: \"connections\",\n                    peerIds: this.peerIds\n                });\n            });\n        }\n    });\n}\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"dc2a\">\u6240\u4ee5\u6211\u4eec\u5df2\u7ecf\u6784\u5efa\u4e86\u4e00\u4e2a\u6301\u4e45\u7684\u5b9e\u65f6\u804a\u5929 web \u5e94\u7528\u7a0b\u5e8f\uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u4e00\u4e2a\u4fe1\u4ee4\u670d\u52a1\u5668\u6765\u5efa\u7acb\u8fde\u63a5\uff0c\u5b83\u662f\u5982\u4f55\u65e0\u670d\u52a1\u5668\u7684\u5462\uff1f\u5b83\u5982\u4f55\u771f\u6b63\u5b89\u5168\uff1f<\/p>\n<\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"aaae\">WebRTC \u5f53\u7136\u4e0d\u662f\u65e0\u670d\u52a1\u5668\u7684\uff01<\/h1>\n\n\n\n<p id=\"debf\">\u8be5\u89e3\u51b3\u65b9\u6848\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u5355\u6027\u548c\u5b89\u5168\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4efb\u4f55\u7528\u6237\u90fd\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u540d\u767b\u5f55\u5e76\u52a0\u5165\u5b83\u8ba4\u8bc6\u7684\u4efb\u4f55\u5176\u4ed6\u670b\u53cb<\/li>\n\n\n\n<li>\u4efb\u4f55\u7528\u6237\u90fd\u53ef\u4ee5\u5b8c\u5168\u8bbf\u95ee\u5176\u804a\u5929\u8bb0\u5f55<\/li>\n\n\n\n<li>\u6570\u636e\u4ea4\u6362\u662f\u5b8c\u5168\u52a0\u5bc6\u7684\uff0c\u4e2d\u95f4\u4eba\u65e0\u6cd5\u89e3\u91ca\uff08\u6709\u5173\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605<a href=\"https:\/\/webrtc-security.github.io\/\" rel=\"noreferrer noopener\" target=\"_blank\">WebRTC \u5b89\u5168\u6027\u7814\u7a76\uff09<\/a><\/li>\n<\/ul>\n\n\n\n<p id=\"5c71\">\u4f46\u662f\uff0c\u7c7b\u4f3c\u7684\u65e0\u670d\u52a1\u5668\u5e94\u7528\u7684\u5f88\u591a\u65b9\u9762\u8fd8\u6709\u5f85\u518d\u6b21\u8ba8\u8bba\u548c\u601d\u8003\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u60a8\u4e0d\u4f1a\u4f7f\u7528\u5c06\u6570\u636e\u5b58\u50a8\u4e3a javascript cookie \u7684\u5e94\u7528\u7a0b\u5e8f\uff01<\/li>\n\n\n\n<li>\u4ecd\u7136\u9700\u8981\u670d\u52a1\u5668\u6765\u5efa\u7acb\u5bf9\u7b49\u8fde\u63a5\uff08\u8bf7\u53c2\u9605<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/levelup.gitconnected.com\/webrtc-the-ice-framework-stun-and-turn-servers-10b2972483bb\">WebRTC\uff1aICE \u6846\u67b6\u3001STUN \u548c TURN \u670d\u52a1\u5668<\/a>\u4ee5\u4e86\u89e3\u6709\u5173\u539f\u56e0\u7684\u66f4\u591a\u4fe1\u606f\uff09\u3002\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9ed8\u8ba4\u4f7f\u7528\u9ad8\u7ea7 PeerJS \u4fe1\u4ee4\u670d\u52a1\u5668\uff0c\u4e0e\u4f4e\u7ea7 STUN \u670d\u52a1\u5668\uff08\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\uff0c\u5177\u6709\u66f4\u591a\u4f4e\u7ea7\u4ee3\u7801\uff09\u76f8\u6bd4<\/li>\n\n\n\n<li>\u4ea4\u6362\u7684\u6570\u636e\u53ea\u80fd\u7531\u540c\u884c\u8bfb\u53d6\u548c\u5199\u5165\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u751a\u81f3\u4e0d\u9002\u5408\u4ea4\u6362\uff08\u4f8b\u5982\uff09\u6e38\u620f\u6570\u636e\uff08\u4efb\u4f55\u7528\u6237\u90fd\u53ef\u4ee5\u4f5c\u5f0a\uff01\uff09<\/li>\n<\/ul>\n\n\n\n<p id=\"d95a\">\u8fd9\u4e00\u70b9\uff0c\u4ee5\u53ca WebRTC \u548c\u66f4\u666e\u904d\u7684\u6b64\u7c7b\u65e0\u670d\u52a1\u5668\u5e94\u7528\u7a0b\u5e8f\u6240\u5177\u6709\u7684\u6240\u6709\u9650\u5236\uff0c\u7559\u5f85\u4ee5\u540e\u518d\u8bb2\uff01<\/p>\n\n\n\n<p id=\"939c\"><a href=\"https:\/\/aqwu.net\/tools\/peerjs-multichat.html\">\u60a8\u53ef\u4ee5\u5728\u6b64\u5904<\/a>\u627e\u5230\u6b64\u5e94\u7528\u7a0b\u5e8f\u7684\u5728\u7ebf\u7248\u672c\uff0c\u5e76\u5728<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/Rubilmax\/peerjs-multichat\" target=\"_blank\">GitHub<\/a>\u4e0a\u627e\u5230\u8be5\u9879\u76ee\u7684\u5b58\u50a8\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 WebRTC\u3001PeerJS\u3001Vue \u548c javascript vanilla cookies \u4e0d\u5230 3  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[43,168],"tags":[211,210,208],"class_list":["post-930","post","type-post","status-publish","format-standard","hentry","category-infoarticle","category-tools","tag-chat","tag-peer","tag-webrtc"],"views":2074,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=930"}],"version-history":[{"count":1,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/930\/revisions"}],"predecessor-version":[{"id":931,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/930\/revisions\/931"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}