{"id":351820,"date":"2021-02-04T16:06:26","date_gmt":"2021-02-04T08:06:26","guid":{"rendered":"http:\/\/4563.org\/?p=351820"},"modified":"2021-02-04T16:06:26","modified_gmt":"2021-02-04T08:06:26","slug":"%e6%b1%82%e5%a4%a7%e4%bd%ac%e6%8c%87%e6%95%99%ef%bc%8c-swift-%e6%9c%8d%e5%8a%a1%e7%ab%af-vapor4-0-websocket-client-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=351820","title":{"rendered":"\u6c42\u5927\u4f6c\u6307\u6559\uff0c Swift \u670d\u52a1\u7aef vapor4.0 websocket client \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u5927\u4f6c\u6307\u6559\uff0c Swift \u670d\u52a1\u7aef vapor4.0 websocket client \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shenfu1991 <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5c0f\u5f1f\u6700\u8fd1\u5728\u5b66\u4e60 swift \u670d\u52a1\u7aef\u6846\u67b6 vapor4.0\uff0c\u60f3\u5f00\u53d1\u4e2a\u7b80\u5355\u7684 app\uff0c\u5728 websocket client \u9047\u5230\u4e86\u95ee\u9898\u3002\u8bdd\u8bf4\u8fd9\u91cc\u7684\u6587\u6863\u771f\u662f\u4e00\u8a00\u96be\u5c3d\u3002\u3002<\/p>\n<p>\u5728\u8fde\u63a5\u5230\u670d\u52a1\u5668\u65f6\uff0c\u5b98\u65b9\u7ed9\u7684 demo URL \u80fd\u591f\u6b63\u5e38\u8fde\u63a5\uff0c\u4f46\u662f\u5f53\u6211\u6362\u6210\u522b\u7684 URL \u7684\u65f6\u5019\u603b\u662f\u62a5\u8fde\u63a5\u8d85\u65f6\u9519\u8bef\u3002<\/p>\n<pre>connectTimeout(NIO.TimeAmount(nanoseconds: 10000000000)) <\/pre>\n<p>Google \u641c\u7d22\u597d\u51e0\u5929\u4e5f\u6ca1\u89e3\u51b3\uff0cgithub \u4e0a\u7684 issue \u4e5f\u6ca1\u770b\u5230\u7c7b\u4f3c\u7684\u95ee\u9898\u3002<\/p>\n<p>\u6e90\u7801\uff1a<\/p>\n<pre>import Vapor  \/\/ configures your application public func configure(_ app: Application) throws {     try webs(app)     try routes(app) }  private func webs(_ app: Application) throws {      \/\/ \u76f8\u5173\u6587\u6863   https:\/\/docs.vapor.codes\/4.0\/websockets\/      \/\/    let url = \"ws:\/\/echo.websocket.org\"   \/\/\u6b64 url \u6b63\u5e38\u63a5\u6536     let url = \"wss:\/\/fstream.binance.com\/[email&#160;protected]\/[email&#160;protected]_15m\/[email&#160;protected]\/\" \/\/\u6b64 URL \u603b\u662f\u62a5\u8d85\u65f6          WebSocket.connect(to: url, on: app.eventLoopGroup) { (ws) in         print(\"CONNECTED!\")         ws.onText { _, text in             print(\"Received text: (text)\")         }          ws.onBinary { _, bin in             print(\"Received binary message: (bin)\")         }         ws.send(\"Sup!\")         ws.send([1,2,3,4,5])   }.flatMapErrorThrowing { (error) in        debugPrint(\"error=(error)\")   }     } <\/pre>\n<p>\u9879\u76ee\u6e90\u7801\uff1a https:\/\/wenjian.xuanyuanhuangdi.org\/websocket.zip<\/p>\n<p>\u76f8\u5173\u6587\u6863\uff1a https:\/\/docs.vapor.codes\/4.0\/websockets\/<\/p>\n<p>\u5e0c\u671b\u5927\u4f6c\u6307\u6559\uff01\u4e5f\u53ef\u4ee5\u7ea2\u5305\u6c42\u52a9\uff0c\u4e07\u5206\u611f\u6fc0\u3002<\/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=\"5316790\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CommandZi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u786e\u5b9a\u4f60\u7684 url \u80fd\u6b63\u5e38\u8fde\u63a5\u7684\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316791\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : shenfu1991 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @CommandZi \u53ef\u4ee5\u7684\uff0ciOS \u5de5\u7a0b\u4e0b\u7528\u522b\u7684\u7b2c\u4e09\u65b9\u5e93\u6b63\u5e38\u8fde\u63a5\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316792\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Ariver <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             demoUrl \u53ef\u4ee5\u8fde\u63a5\uff0c\u4f60\u7684 url \u4e0d\u884c\u7684\u8bdd\uff0c\u6211\u89c9\u5f97\u8fd8\u662f\u8003\u8651\u4e00\u4e0b\u4f60\u7684\u670d\u52a1\u7aef\u5427\u3002<br \/>\u81f3\u4e8e\u4f60\u8bf4\u7684\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u8fde\u63a5\u7684\u8bdd\uff0c\u4ed6\u4eec\u8fde\u63a5\u7684\u662f\u5176\u4ed6 api\uff0c\u800c\u4e0d\u662f ws \u5427\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316793\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : shenfu1991 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Ariver \u6211\u7684\u8fd9\u4e2a URL \u5176\u5b9e\u4e5f\u662f\u522b\u4eba\u7684\uff0c\u4e0d\u662f\u6211\u4eec\u7684\u670d\u52a1\u7aef\u63d0\u4f9b\u7684\uff0c\u4e0d\u662f api\uff0c\u5c31\u662f ws \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316794\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CommandZi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6293\u5305\u770b\u770b\u5427\uff0c\u6211\u6000\u7591\u662f tls \u7684\u539f\u56e0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316795\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DianQK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u80fd\u590d\u73b0\uff1a<br \/>https:\/\/sm.ms\/image\/TdfupmWO6FiHg4a<\/p>\n<p>\u4f7f\u7528\u7684 Docker \u73af\u5883\u5417\uff1f\uff08\u90a3\u795d\u4e3b\u597d\u8fd0                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316796\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DianQK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u554a\u3002\u3002\u3002== \u8c8c\u4f3c\u662f\u88ab\u5899\u4e86<br \/>!&#8212;Digital Currency Exchange(CRYPTO)&#8212;<br \/>||aex.com<br \/>||allcoin.com<br \/>||adcex.com<br \/>||bcex.ca<br \/>||bibox.com<br \/>||big.one<br \/>||binance.com \u57df\u540d\u8fd9\u5462<\/p>\n<p>\u64b8\u4e86\u4e2a B \u7ad9\u7684 let url = &#8220;wss:\/\/ks-live-dmcmt-sh2-pm-03.chat.bilibili.com\/sub&#8221;<br \/>\u4e3b\u53ef\u4ee5\u8bd5\u8bd5\u8fd9\u4e2a<\/p>\n<p>== \u4e0d\u77e5\u9053\u8fd9\u4e2a\u7b54\u590d\u503c\u4e0d\u503c 200                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316797\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : shenfu1991 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @DianQK #7                                                             <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316798\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DianQK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @shenfu1991 \u4e3e\u624b\u4e4b\u52b3\uff0c\u4e3b\u613f\u9001\u4e2a\u7ea2\u5305\u5956\u52b1\u90a3\u771f\u662f\u592a\u6b23\u6170\u4e86<\/p>\n<p>https:\/\/sm.ms\/image\/yFQjReEO9GCJLHS                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316799\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : shenfu1991 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @DianQK \u521a\u56de\u5230\u5bb6\uff0c\u62b1\u6b49\u3002\u5927\u4f6c\u80fd\u5426\u8bf4\u4e00\u4e0b\u4f60\u90a3\u8fb9\u7684\u73af\u5883\uff0c\u770b\u4f60\u7684 IDE \u597d\u50cf\u4e0d\u662f XCode \uff1f\u6211\u7528 XCode \u548c\u7ec8\u7aef\u90fd\u62a5\u540c\u6837\u7684\u9519\u8bef\uff0c\u6211\u516c\u53f8\u7535\u8111\u6ca1\u6709\u62a5\u9519\uff0c\u4f30\u8ba1\u662f XCode \u7248\u672c\u6709\u95ee\u9898\uff0c\u6211\u66f4\u65b0\u5728\u8bd5\u8bd5\u3002\u7ea2\u5305\u5148\u9001\u4e0a\uff0c\u8bf7\u67e5\u6536\u3002<\/p>\n<p>https:\/\/wenjian.xuanyuanhuangdi.org\/<span data-cfemail=\"85d2ddb7b5b7b4b5b7b4bca8b7b7b5b2b7b5c5b7fdabf5ebe2\">[email&#160;protected]<\/span> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316800\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oxromantic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6d4b\u4e86\u4e0b\uff0c\u9996\u5148\u8981\u7ffb\u5899\uff0c\u5176\u6b21 dns \u6709\u8bb2\u7a76\uff0c\u8b6c\u5982\u4e0d\u80fd\u7528 114.114.114.114 \u7684\uff0c\u6d4b\u8bd5\u4e86 223.5.5.5 \u662f\u53ef\u4ee5\u89e3\u6790\u5230\u6b63\u786e\u8282\u70b9<\/p>\n<p>[ NOTICE ] Server starting on http:\/\/127.0.0.1:8080<br \/>CONNECTED!<br \/>Received text: {&#8220;id&#8221;: null, &#8220;error&#8221;: {&#8220;code&#8221;: 3, &#8220;msg&#8221;: &#8220;Invalid json&#8221;}}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316801\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : shenfu1991 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @oxromantic \u611f\u8c22\u70ed\u5fc3\u5e2e\u52a9\uff0c\u4e0d\u8fc7\u7ea2\u5305\u53ea\u6709\u4e00\u4e2a\uff0c\u5df2\u7ecf\u53d1\u7ed9\u4e0a\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316802\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DianQK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @shenfu1991 \u6536\u5230                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5316803\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DianQK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @shenfu1991 \u622a\u56fe address already in use \uff08\u6216\u8bb8\u662f\u7ec8\u7aef\u4f60\u5728\u8dd1\u4e00\u4e2a\uff0cXcode \u518d\u8dd1\u7aef\u53e3\u5360\u7528\u76f4\u63a5 \u62a5 crash \u4e86 XD                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u5927\u4f6c\u6307\u6559\uff0c Swift \u670d\u52a1\u7aef &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\/351820"}],"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=351820"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/351820\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351820"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}