{"id":156113,"date":"2020-09-14T21:38:17","date_gmt":"2020-09-14T13:38:17","guid":{"rendered":"http:\/\/4563.org\/?p=156113"},"modified":"2020-09-14T21:38:17","modified_gmt":"2020-09-14T13:38:17","slug":"%e4%bd%bf%e7%94%a8-websocket-%e5%8f%91%e9%80%81%e6%95%b0%e6%8d%ae%e7%bb%99%e5%89%8d%e7%ab%af%e8%bf%87%e6%ae%b5%e6%97%b6%e9%97%b4%e5%90%8e%e5%90%8e%e7%ab%af%e7%a8%8b%e5%ba%8f%e5%8d%a1%e6%ad%bb","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=156113","title":{"rendered":"\u4f7f\u7528 websocket \u53d1\u9001\u6570\u636e\u7ed9\u524d\u7aef\u8fc7\u6bb5\u65f6\u95f4\u540e\u540e\u7aef\u7a0b\u5e8f\u5361\u6b7b"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4f7f\u7528 websocket \u53d1\u9001\u6570\u636e\u7ed9\u524d\u7aef\u8fc7\u6bb5\u65f6\u95f4\u540e\u540e\u7aef\u7a0b\u5e8f\u5361\u6b7b               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : swqslwl <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u540e\u7aef\u7a0b\u5e8f\u542f\u52a8\u540e 120 \u79d2\u5de6\u53f3\u5c31\u5361\u6b7b\u4e86\uff0c\u65e0\u8bba\u63a8\u9001\u901f\u5ea6\u662f\u5feb\u8fd8\u662f\u6162\uff0c\u603b\u662f\u4f1a\u5361\u6b7b\u3002\u6ca1\u6709\u4efb\u4f55\u62a5\u9519\u4fe1\u606f\u3002<\/p>\n<p>\u540e\u7aef\u7528\u7684\u662f go\uff0cgin \u6846\u67b6\u4e2d\u5f00\u4e86 2 \u4e2a\u534f\u7a0b\uff0c1 \u4e2a\u7528\u6765\u63a5\u53d7\u6570\u636e\uff0c1 \u4e2a\u7528\u6765\u5165\u5e93\u548c\u7528 websocket \u53d1\u7ed9\u524d\u7aef\u3002<\/p>\n<p>\u63a5\u53d7\u6570\u636e\u548c\u6570\u636e\u5e93\u5199\u5165\u90fd\u6ca1\u95ee\u9898\u3002\u5e94\u8be5\u5c31\u662f websocket \u63a8\u7ed9\u524d\u7aef\u65f6\u7684\u95ee\u9898\u3002\u4e5f\u68c0\u67e5\u4e86\u534f\u7a0b\u6570\uff0c\u90fd\u662f\u6b63\u5e38\u7684\u3002<\/p>\n<pre><code>var upgrader = websocket.Upgrader{  \/\/ ReadBufferSize: 1024,  \/\/WriteBufferSize: 1024,  \/\/ \u5141\u8bb8\u6240\u6709\u7684 CORS \u8de8\u57df\u8bf7\u6c42\uff0c\u6b63\u5f0f\u73af\u5883\u53ef\u4ee5\u5173\u95ed  CheckOrigin: func(r *http.Request) bool {   return true  }, } var dataTmp []byte  func init() {  http.HandleFunc(\"\/ws\", wsHandler) }  \/\/ \u542f\u52a8\u7a0b\u5e8f func StartWebsocket(addrPort string) {  http.ListenAndServe(addrPort, nil) }  func(){     ...     go StartWebsocket(\"173.10.10.100:88\")     ... }   func wsHandler(resp http.ResponseWriter, req *http.Request) {  \/\/ \u7b54\u5ba2\u6237\u7aef\u544a\u77e5\u5347\u7ea7\u8fde\u63a5\u4e3a websocket  wsSocket, err := upgrader.Upgrade(resp, req, nil)  if err != nil {   fmt.Println(\"\u5347\u7ea7\u4e3a websocket \u5931\u8d25\", err.Error())   return  }  go wsWriteLoop(wsSocket) }  func wsWriteLoop(wsConn *websocket.Conn) {  for {   if len(dataTmp) != 0 {    fmt.Println(time.Now())    err := wsConn.WriteMessage(websocket.TextMessage, dataTmp)    dataTmp = dataTmp[0:0]    if err != nil {     fmt.Println(\"\u53d1\u9001\u6d88\u606f\u7ed9\u5ba2\u6237\u7aef\u53d1\u751f\u9519\u8bef\", err.Error())     wsConn.Close()     return    }   }  } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3315887\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ghjacky <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             for \u8fd9\u6837\u7528\u5408\u9002\u5417\uff0c\u4e3a\u5565\u4e0d\u628a dataTmp \u6362\u6210 channel \u5462\uff0cfor \u914d\u5408 select \u8bfb dataTmp \u4e0d\u597d\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3315888\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : swqslwl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ghjacky \u4e0d\u662f\u5f88\u597d\uff0c\u4f46\u611f\u89c9\u95ee\u9898\u4e0d\u662f\u51fa\u5728\u8fd9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3315889\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : back0893 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             dataTmp \u7684\u95ee\u9898\u5427.<br \/>\u4e00\u822c\u6765\u8bf4\u4f60\u5199\u7684 loop \u6570\u636e\u5e94\u8be5\u662f\u9694\u79bb\u7684,\u5c31\u7b97\u8981\u5171\u6027,\u4e5f\u9700\u8981\u52a0\u9501                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3315890\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kangsheng9527 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             300 \u5143\u5e2e\u4f60\u8fdc\u7a0b\u8c03\u8bd5\u4fdd\u8bc1\u80fd\u627e\u51fa\u95ee\u9898\u5e76\u5904\u7406\u597d\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3315891\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : swqslwl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ghjacky \u8001\u54e5\u6211\u9519\u4e86\uff0c\u5c31\u662f\u6ca1\u7528 channel \u7684\u95ee\u9898\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3315892\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : swqslwl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @back0893 \u662f\u7684\u662f\u7684\uff0c\u6362\u4e86\u7ba1\u9053\u5c31\u597d\u4e86\u3002\u8001\u54e5\u53ef\u4ee5\u8be6\u7ec6\u8bf4\u8bf4\u8fd9\u91cc\u4f7f\u7528\u5207\u7247\u548c\u7ba1\u9053\u7684\u533a\u522b\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3315893\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JustDoIt221 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5171\u4eab\u5185\u5b58\u53ef\u89c1\u6027\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3315894\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : swqslwl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JustDoIt221 \u61c2\u4e86\u61c2\u4e86\uff0c\u592a\u83dc\u4e86\uff0c\u592a\u4e45\u4e0d\u5199\u5e76\u53d1\u5c45\u7136\u5fd8\u4e86\u8fd9\u832c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 websocket \u53d1\u9001\u6570\u636e&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\/156113"}],"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=156113"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/156113\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}