{"id":157696,"date":"2020-09-11T08:58:39","date_gmt":"2020-09-11T00:58:39","guid":{"rendered":"http:\/\/4563.org\/?p=157696"},"modified":"2020-09-11T08:58:39","modified_gmt":"2020-09-11T00:58:39","slug":"golang-%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%ef%bc%8c%e6%80%bb%e6%98%af%e4%bc%9a%e6%9c%89%e4%b8%a2%e5%8c%85%e7%9a%84%e6%83%85%e5%86%b5%e5%8f%91","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=157696","title":{"rendered":"golang \u4f7f\u7528 websocket \u53d1\u9001\u6570\u636e\u7ed9\u524d\u7aef\uff0c\u603b\u662f\u4f1a\u6709\u4e22\u5305\u7684\u60c5\u51b5\u53d1\u751f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  golang \u4f7f\u7528 websocket \u53d1\u9001\u6570\u636e\u7ed9\u524d\u7aef\uff0c\u603b\u662f\u4f1a\u6709\u4e22\u5305\u7684\u60c5\u51b5\u53d1\u751f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : swqslwl <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u62a5\u9519\u6709\u65f6\u662f write:broken pipe. \u6709\u65f6\u662f An established connection was aborted by the software in your host machine. \u6709\u65f6\u4e0d\u62a5\u9519\uff0cwrieshark \u6293\u5305\u53d1\u73b0\u5305\u5168\u90e8\u53d1\u51fa\u53bb\u4e86\uff0c\u4f46\u524d\u7aef\u5c31\u662f\u4f1a\u4e22\u5305\u3002<\/p>\n<p>\u4e0a\u4e2a\u5e16\u5b50\u72af\u4e86\u4e2a\u4f4e\u7ea7\u9519\u8bef\u7528\u4e86\u5171\u4eab\u5185\u5b58\u53d8\u91cf\uff0c\u73b0\u5728\u6539\u4e3a\u7528 channel \u4e86\uff0c\u60f3\u77e5\u9053\u662f\u4e0d\u662f\u8fd8\u662f\u6211\u540e\u7aef\u4ee3\u7801\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5982\u679c\u6709\u63d0\u4f9b\u5f88\u5927\u5e2e\u52a9\u601d\u8def\u7684\uff0c\u5c0f\u5f1f\u53ef\u4ee5\u7ed9\u4e88\u4e00\u5b9a\u7684\u916c\u8c22\u3002<\/p>\n<pre><code>var dataTmp = make(chan []byte, 9999) var t = make(chan []byte, 9999)  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  }, }  func init() {  http.HandleFunc(\"\/ws\", wsHandler)  go http.ListenAndServe(\"173.10.10.100:88\", nil) }  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) }  \/\/ \u53d1\u9001\u6d88\u606f\u7ed9\u5ba2\u6237\u7aef func wsWriteLoop(wsConn *websocket.Conn) {  for {   select {   case terror := &lt;-t:    fmt.Println(\"\u9519\u8bef\u91cd\u65b0\u53d1\u9001\")    fmt.Println(terror)    err := wsConn.WriteMessage(websocket.TextMessage, terror)    if err != nil {     fmt.Println(\"\u518d\u6b21\u53d1\u9001\", err.Error())     t &lt;- terror     return    }    case tmp := &lt;-dataTmp:    err := wsConn.WriteMessage(websocket.TextMessage, tmp)    if err != nil {     fmt.Println(\"\u53d1\u9001\u6d88\u606f\u7ed9\u5ba2\u6237\u7aef\u53d1\u751f\u9519\u8bef\", err.Error())     t &lt;- tmp     return    }   }  } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3364099\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xkeyideal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u65e2\u7136\u4e3b\u5927\u5927\u65b9\u65b9\u7684\u8bf4\u8981\u7ed9\u916c\u52b3\u4e86\uff0c\u90a3\u4e48\u5c31\u5148\u8bf4\u8bf4\u80fd\u7ed9\u591a\u5c11\u5427\u3002<br \/>\u8fd9\u6bb5\u4ee3\u7801\u5462\uff0c\u5728\u67d0\u4e9b\u73af\u5883\u4e0b\u6ca1\u6709\u95ee\u9898\uff0c\u4e5f\u770b\u4e0d\u51fa\u95ee\u9898\uff1b\u4f46\u5176\u5b9e\u69fd\u70b9\u6162\u6162\uff0c\u53ef\u4ee5\u7ed9\u4f60\u4e00\u4efd\u6bd4\u8f83\u5b8c\u6574\u7684 golang websocket \u7684\u5904\u7406\u4ee3\u7801\uff0c\u81f3\u4e8e\u80fd\u4e0d\u80fd\u89e3\u51b3\u4f60\u8bf4\u7684\u95ee\u9898\u4e0d\u6e05\u695a\uff0c\u4f60\u8fd9\u79cd\u95ee\u9898\u4e2a\u4eba\u4ece\u6765\u6ca1\u9047\u5230                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364100\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bjt <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3b\u6015\u662f\u60f3\u9519\u4e86\uff0c\u8fd9\u4e2a\u4e0d\u662f\u4e22\u5305\uff0c\u5c31\u662f\u7f51\u7edc\u4e2d\u65ad\u3002\u4f60\u672c\u5730\u9001\u51fa\u53bb\u4e86\u5e76\u4e0d\u4ee3\u8868\u5bf9\u65b9\u80fd\u6536\u5230\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364101\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : est <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e00\u822c\u7684\u8bbe\u8ba1\u4e0d\u662f\u89e3\u51b3\u4e22\u5305\uff0c\u800c\u662f\u5728\u627f\u8ba4\u4e22\u5305\u7684\u524d\u63d0\u4e0b\u518d\u591a\u53d1\u51e0\u904d\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364102\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xx6412223 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5148\u628a\u903b\u8f91\u5199\u6b63\u786e\u5427\u3002\u8fd8\u6240\u6709\u8fde\u63a5\u5171\u7528\u4e00\u4e2a\u9519\u8bef channel                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364103\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sujin190 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u624b\u52a8\u8fd9\u4e24\u9519\u8bef\u672c\u8eab\u5c31\u662f\u6b63\u5e38\u7684\u5427\uff0c\u4f60\u8fd9\u4e2a for \u662f\u6b7b\u5faa\u73af\u7684\uff0c\u53c8\u6ca1\u6709\u68c0\u67e5 websocket \u8fde\u63a5\u662f\u5426\u65ad\u5f00\u7684\u60c5\u51b5\uff0c\u5982\u679c\u524d\u7aef\u6709\u5f02\u5e38\u65ad\u5f00\u4e86\u4f60\u518d\u5199\u5c31\u4f1a\u8fd4\u56de\u8fd9\u4e2a\u9519\u8bef\u554a\uff0c\u524d\u7aef\u65ad\u5f00\u4e00\u822c\u4f1a\u91cd\u8fde\uff0c\u6240\u4ee5\u4f60\u5e94\u8be5\u65ad\u5f00\u540e\u6570\u636e\u5c31\u4fdd\u5b58\u518d chan \u4e2d\uff0c\u91cd\u8fde\u4e4b\u540e\u518d\u5f80\u65b0\u7684\u8fde\u63a5\u4e2d\u5199\uff0c\u5177\u4f53\u662f\u4e0d\u662f\u8fd9\u6837\u548c\u4e3a\u5565\u65ad\u5f00\u4e0d\u884c\u53ef\u4ee5\u6293\u5305\u4ed4\u7ec6\u770b\u770b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364104\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : des <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8fd9\u4e24\u4e2a\u62a5\u9519\uff0c\u5c31\u662f\u7f51\u7edc\u8fde\u63a5\u65ad\u5f00\u4e86\uff0c\u4e0d\u662f\u524d\u7aef\u4e22\u5305\u7684\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364105\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : joesonw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             websocket \u534f\u8bae\u4fdd\u8bc1\u4e86\u4f60\u6536\u5230\u7684 frame \u5c31\u662f\u5b8c\u6574\u7684\u6ca1\u62c6\u5f00\u7684 frame. \u5982\u679c\u4e22\u5305\u4e86\u80af\u5b9a\u4e0d\u662f\u4ee3\u7801\u7684\u95ee\u9898, \u800c\u662f\u5e93\u7684\u95ee\u9898(\u53d1\u9001\u7aef \/\u63a5\u6536\u7aef)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364106\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wysnylc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u60e8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364107\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xdeng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             9999 \u5c31\u5f88\u4e0d\u7a0b\u5e8f\u5458\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3364108\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : suke971219 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/Treblex\/easyStart\/blob\/master\/server\/router\/api\/ws\/chat\/server.go<\/p>\n<p>\u6700\u8fd1\u4e5f\u5728\u5b66 go\uff0c\u53ef\u4ee5\u770b\u4e0b\u6709\u6ca1\u6709\u5e2e\u52a9\uff0c<br \/>\u9884\u89c8\u5730\u5740 https:\/\/go.abadboy.cn\/ws\/                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang \u4f7f\u7528 websock&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\/157696"}],"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=157696"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/157696\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157696"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}