{"id":149438,"date":"2020-08-25T02:00:53","date_gmt":"2020-08-24T18:00:53","guid":{"rendered":"http:\/\/4563.org\/?p=149438"},"modified":"2020-08-25T02:00:53","modified_gmt":"2020-08-24T18:00:53","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%aa%e7%bc%96%e7%a8%8b%e6%8a%80%e5%b7%a7%ef%bc%8c%e6%9c%89%e6%b2%a1%e6%9c%89%e5%8a%9e%e6%b3%95%e6%8a%8a-websocket-%e7%9a%84%e9%80%9a%e8%ae%af%e8%a3%85%e5%88%b0-promise","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=149438","title":{"rendered":"\u8bf7\u6559\u4e00\u4e2a\u7f16\u7a0b\u6280\u5de7\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u628a websocket \u7684\u901a\u8baf\u88c5\u5230 Promise \u91cc\u9762\u53bb\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e00\u4e2a\u7f16\u7a0b\u6280\u5de7\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u628a websocket \u7684\u901a\u8baf\u88c5\u5230 Promise \u91cc\u9762\u53bb\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u73b0\u5728\u6709\u4e00\u4e2a\u9700\u6c42\uff0c\u8981\u4f7f\u7528 websocket \u4f20\u8f93\u6570\u636e\uff0c\u65f6\u95f4\u5927\u6982\u51e0\u79d2\u949f\u3002<br \/> \u60f3\u5728\u4f20\u8f93\u6570\u636e\u7684\u65f6\u5019\uff0c\u540c\u65f6\u88c5\u5907\u4e0b\u4e00\u4efd\u6570\u636e\u3002\u51c6\u5907\u7684\u65f6\u5019\uff0c\u4e5f\u5927\u6982\u51e0\u79d2\u949f\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5c31\u5728\u60f3\uff0c\u80fd\u5426\u628a\u8fd9\u4e24\u5757\u5e76\u884c\u8d77\u6765\u3002<\/p>\n<p>\u521d\u6b65\u60f3\u6cd5 \/\u65b9\u6cd5\uff0c\u5c31\u662f\u7528 Promise.all \uff0c\u4f46\u8c8c\u4f3c\u4ee3\u7801\u4e0d\u662f\u5f88\u597d\u5199\u3002<br \/> \u56e0\u4e3a websocket \u7684\u8c03\u7528\uff0c\u4ec0\u4e48 onmessage \u7b49\u7b49\uff0c\u4e0d\u592a\u597d\u653e\u5165\u5230 promise \u91cc\u9762\u53bb\u3002<\/p>\n<p>\u6709\u6ca1\u6709\u5144\u5f1f\u77e5\u9053\u600e\u4e48\u505a\uff1f\u6216\u8005\u6709\u5176\u5b83\u66f4\u597d\u7684\u65b9\u6cd5\uff1f\u6bd4\u5982 multi-threads?<\/p>\n<p>\u8c22\u8c22\u3002<\/p>\n<p>p.s. JavaScript\/TypeScript \u524d\u7aef<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3128012\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jirajine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/www.npmjs.com\/package\/async-ws                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128013\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jiangzm <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u5f04\u4e2a\u53d1\u9001\u961f\u5217\u4e0d\u5c31\u597d\u4e86\uff0cws \u4ece\u961f\u5217\u62ff\u6570\u636e\u53d1\u9001\uff0c\u7ec4\u88c5\u7684\u6570\u636e\u4e0d\u65ad\u6254\u5230\u961f\u5217\u3002<\/p>\n<p>\u540e\u9762\u4f60\u8981\u5c01\u88c5 Promise \u662f\u8981\u5e72\u561b\uff0c\u6ca1\u770b\u61c2\uff0c \u53ef\u4ee5\u8d34\u4e86\u4f2a\u4ee3\u7801\u793a\u4f8b\u4e00\u4e0b\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128014\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fivge <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             rxjs                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128015\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jiangzm #2 \u4e0d\u592a\u597d\u8d34\u3002legacy \u7684\u9879\u76ee\uff0c\u4e00\u5927\u5806\u4ee3\u7801\u5168\u6324\u5728\u4e00\u8d77\u3002<\/p>\n<p>\u57fa\u672c\u6d41\u7a0b\u5c31\u662f\u5148\u4f20\u8f93\u6587\u4ef6\u8fc7\u53bb\uff0c\u4f20\u8f93\u5b8c\u4e86\uff0c\u5982\u679c\u6210\u529f\uff0c\u518d\u4f20\u8f93\u8bc1\u4e66\u3002\u5373\uff0conmessage \u91cc\u9762\uff0c\u6536\u5230\u6210\u529f\u7684\u56de\u590d\u540e\uff0c\u518d\u53d1\u9001\u8bc1\u4e66\u3002<\/p>\n<p>\u4f46\u5927\u5bb6\u77e5\u9053\uff0c\u7533\u8bf7\u8bc1\u4e66\uff0c\u8c03\u7528 api \u4e5f\u662f\u9700\u8981\u65f6\u95f4\u7684\u3002<br \/>\u6240\u4ee5\uff0c\u60f3\u5728\u53d1\u9001\u6587\u4ef6\u65f6\uff0c\u5f02\u6b65\u8c03\u7528\u51fd\u6570\u7533\u8bf7\u8bc1\u4e66\u3002\u7b49\u6536\u5230\u6587\u4ef6\u4f20\u8f93\u6210\u529f\u6d88\u606f \/\u54cd\u5e94\u540e\uff0c\u5c31\u80fd\u76f4\u63a5\u53d1\u9001\u8bc1\u4e66\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128016\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jiangzm #2 \u770b\u61c2\u4f60\u7684\u610f\u601d\u4e86\u3002<\/p>\n<p>\u53ef\u60dc\uff0c\u6211\u4eec\u8fd9\u4e2a\u903b\u8f91 \/\u6d41\u7a0b\uff0c\u5f53\u521d\u8bbe\u8ba1\u7684\u65f6\u5019\uff0c\u5c31\u6709\u4e25\u683c\u7684\u987a\u5e8f\u3002\u63e1\u624b-\u9a8c\u8bc1\u53cc\u65b9\u8eab\u4efd-\u53d1\u9001\u6587\u4ef6-\u53d1\u9001\u8bc1\u4e66-\u2026\u2026<br \/>\u6709\u987a\u5e8f\u7684\u3002\u76f8\u5f53\u4e8e\u201c\u540c\u6b65\u201d\u6267\u884c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128017\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azcvcza <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`<br \/>onmessage = function(data){<br \/> if(data.type === &#8216;\u67d0\u79cd\u7c7b\u578b&#8217;){<br \/> new Promise(resolve=&gt;{<br \/> resolve(axios.post(&#8216;xxx&#8217;))<br \/> }).then(res=&gt;{<br \/> \/\/ get auth<br \/> websocket.send()<br \/> })<br \/> }<br \/>}<br \/>&#8220;`<br \/>\u5927\u81f4\u903b\u8f91\u8fd9\u6837\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128018\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : peterjose <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u592a\u542c\u61c2\u4f60\u5728\u8bf4\u5565 \u4f46\u662f rxjs-websocket \u4f30\u8ba1\u80fd\u6ee1\u8db3\u4f60                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128019\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u6709\u4ee3\u7801\uff0c\u6ca1\u6cd5\u63d0\u4f9b\u65b9\u6848\u3002<\/p>\n<p>\u6570\u636e\u91cf\u4e0d\u5927\u5230\u81f3\u4e8e\u7206\u6389\u5f15\u64ce\u5185\u5b58\u7a7a\u95f4\u7684\u8bdd\uff0c\u53ef\u4ee5\u51c6\u5907\u4e24\u4e2a\u7ebf\u7a0b\uff08\u6bd4\u5982 Web Workers \uff09\u3001\u4e00\u4e2a\u961f\u5217\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u6301\u7eed\u51c6\u5907\u6570\u636e\u5e76\u5c06\u5f85\u53d1\u9001\u7684\u6570\u636e\u63d2\u5165\u5230\u961f\u5217\u4e2d\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e0d\u65ad\u4ece\u961f\u5217\u4e2d\u8bfb\u53d6\u6570\u636e\u53d1\u9001\u3002\u5982\u679c\u4f60\u7528\u5185\u5b58\u5b58\u50a8\u961f\u5217\u4e14\u6015\u5185\u5b58\u7206\u6389\uff0c\u53ef\u4ee5\u5728\u51c6\u5907\u6570\u636e\u7684\u8fc7\u7a0b\u91cc\u6bcf\u6b21\u5faa\u73af\u90fd\u68c0\u67e5\u4e00\u4e0b\u961f\u5217\u5927\u5c0f\uff0c\u5982\u679c\u8d85\u51fa\u9608\u503c\u5c31\u6682\u505c\u4e00\u6bb5\u65f6\u95f4\u3002<\/p>\n<p>Promise \u662f\u5728\u4e00\u4e2a\u7ebf\u7a0b\u5185\u63a7\u5236\u5f02\u6b65 IO \u7684\uff0cPromise.all \u4e0d\u77e5\u9053\u4f60\u60f3\u600e\u4e48\u7528\uff0c\u5982\u679c\u5355\u7eaf\u5427\u51c6\u5907\u548c\u53d1\u9001\u6570\u636e\u7684\u8fc7\u7a0b\u6254\u5230\u91cc\u9762\u4f30\u8ba1\u662f\u6240\u6709\u8981\u5904\u7406\u7684\u6570\u636e\u540c\u65f6\u51c6\u5907\u548c\u53d1\u9001\u4e86\uff0c\u4f60\u8981\u662f\u5bf9\u6570\u636e\u6ca1\u6709\u5148\u540e\u987a\u5e8f\u8981\u6c42\u7684\u8bdd\u4e5f\u4e0d\u662f\u4e0d\u884c\u3002Promise \u63d0\u5347\u6548\u7387\u7684\u5173\u952e\u5728\u4e8e\u5f02\u6b65 API \u5f02\u6b65\u6267\u884c\uff0c\u540c\u6b65 API \u7528\u4e86 Promise \u4e5f\u4e0d\u4f1a\u5f02\u6b65\u6267\u884c\uff0c\u8fd9\u65f6\u5019\u5c31\u5f97\u8003\u8651\u591a\u7ebf\u7a0b\u4e86\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128020\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hitaoguo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \/\/ \u628a\u4f20\u8f93\u6587\u4ef6\u5c01\u88c5\u6210\u4e00\u4e2a Promise<br \/>const sendFile = () =&gt; Promise.resolve()<br \/>\/\/ \u7533\u8bf7\u8bc1\u4e66\u4e5f\u5c01\u88c5\u4e00\u4e0b<br \/>const requestCert = () =&gt; Promise.resolve()<br \/>const sendCert = () =&gt; { }<br \/>\/\/ \u6700\u540e\u901a\u8fc7 Promise.All \uff0c\u5f53\u6587\u4ef6\u4f20\u8f93\u597d\u4e86\uff0c\u5e76\u4e14\u8bc1\u4e66\u4e5f\u7533\u8bf7\u4e0b\u6765\u4e86\u7684\u65f6\u5019\uff0c\u518d\u53d1\u9001\u8bc1\u4e66<br \/>Promise.all([sendFile(), requestCert()]).then(sendCert)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128021\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : qyvlik <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bd5\u8bd5\u8fd9\u4e2a\uff1a https:\/\/github.com\/qyvlik\/rpc.js\/blob\/master\/index.js                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128022\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jiangzm <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yazoox #4 \u8fd9\u5f88\u7b80\u5355\u554a\uff0c\u5f02\u6b65\u53d1\u9001\u6587\u4ef6\u548c\u5f02\u6b65\u8bf7\u6c42\u8bc1\u4e66\u5c31\u884c\uff0c\u7136\u540e\u5728 onmessage \u91cc\u9762\u540c\u6b65\u7b49\u5f85\u8bf7\u6c42\u8bc1\u4e66\u7684\u7ed3\u679c<br \/>code: https:\/\/gist.github.com\/jiangzm\/4474f2c2f601b23d306235b2233fbe03 <button onclick=\"lazyGist(this)\">\u00a0\u663e\u793a Gist \u4ee3\u7801\u00a0<\/button> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128023\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jiangzm <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6362\u4e2a\u56fe\u5e93\u770b\u4e0b<br \/>https:\/\/sm.ms\/image\/mnsAfHc5GzLt4kK                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128024\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jiangzm <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u539f\u6765\u662f\u94fe\u63a5\u641e\u9519\u4e86<br \/>https:\/\/i.loli.net\/2020\/08\/28\/mnsAfHc5GzLt4kK.png                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3128025\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jiangzm <br \/>\u8c22\u8c22\u3002\u4f60\u7684\u8fd9\u4e2a\u65b9\u6cd5\u662f\u53ef\u4ee5\u7684\u3002<br \/>\u4eca\u5929\u5b66\u4e60\u5230\u4e86\u4e00\u62db\uff0c\u539f\u6765 promise \u53ef\u4ee5\u76f4\u63a5 assgin \u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u8c03\u7528\u540e\u4e0d\u7ba1\u3002<br \/>\u5728\u540e\u9762\u9700\u8981\u7684\u65f6\u5019\uff0cawait promise \u53d8\u91cf\u5c31\u53ef\u4ee5\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e2a\u7f16\u7a0b\u6280\u5de7\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u628a w&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149438"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149438"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149438\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149438"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}