{"id":276827,"date":"2024-05-16T15:04:43","date_gmt":"2024-05-16T07:04:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/276827.html"},"modified":"2024-05-16T15:04:53","modified_gmt":"2024-05-16T07:04:53","slug":"go%e6%9c%89%e5%93%aa%e4%ba%9b%e5%85%b3%e4%ba%8ewebsocket%e7%9a%84%e5%bc%80%e6%ba%90%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/276827.html","title":{"rendered":"go\u6709\u54ea\u4e9b\u5173\u4e8ewebsocket\u7684\u5f00\u6e90\u9879\u76ee"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24201149\/a9c6e56d-9a1a-4a69-ada8-b44f85af558c.webp\" alt=\"go\u6709\u54ea\u4e9b\u5173\u4e8ewebsocket\u7684\u5f00\u6e90\u9879\u76ee\" \/><\/p>\n<p><p><strong>Go\u8bed\u8a00\u5728WebSocket\u65b9\u9762\u62e5\u6709\u591a\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u4f8b\u5982Gorilla WebSocket\u3001gobwas\/ws\u3001websocket\u548cnhooyr.io\/websocket\u3002<strong>\u8fd9\u4e9b\u5e93\u5404\u6709\u7279\u70b9\uff0c\u4f46<\/strong>Gorilla WebSocket<\/strong> \u65e0\u7591\u662f\u6700\u4e3a\u51fa\u8272\u548c\u6700\u53d7\u6b22\u8fce\u7684\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u3001\u5f3a\u5927\u4e14\u7ecf\u8fc7\u5e7f\u6cdb\u6d4b\u8bd5\u7684API\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570WebSocket\u9700\u8981\u7684\u573a\u666f\u3002\u8fd9\u4e2a\u5e93\u5173\u6ce8\u4e8e\u63d0\u4f9bGo\u5f00\u53d1\u8005\u4e00\u4e2a\u9075\u5faaWebSocket\u534f\u8baeRFC 6455\u6807\u51c6\u7684\u5b9e\u73b0\uff0c\u540c\u65f6\u63d0\u4f9b\u4e86\u6613\u4e8e\u63a5\u5165\u7684\u63a5\u53e3\uff0c\u4ee5\u53ca\u5bf9\u9ad8\u7ea7\u529f\u80fd\u5982\u538b\u7f29\u3001\u591a\u8def\u590d\u7528\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001GORILLA WEBSOCKET<\/p>\n<p>Gorilla WebSocket\u662f\u4e00\u4e2a\u4e13\u95e8\u4e3aGo\u8bed\u8a00\u8bbe\u8ba1\u7684WebSocket\u5e93\u3002\u5b83\u65e2\u652f\u6301\u5ba2\u6237\u7aef\u4e5f\u652f\u6301\u670d\u52a1\u7aef\u5b9e\u73b0\uff0c\u5e76\u7b80\u5316\u4e86\u591a\u79cd\u7528\u4f8b\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u7b80\u5355\u4e14\u5f3a\u5927\u7684API\uff1a<\/strong> Gorilla WebSocket\u63d0\u4f9b\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u7684API\u3002\u5b83\u5904\u7406\u4e86\u5e95\u5c42\u7684\u534f\u8bae\u5de5\u4f5c\uff0c\u5982\u63e1\u624b\u3001\u5e27\u5904\u7406\u3001\u63a7\u5236\u6d88\u606f\uff0c\u5e76\u5141\u8bb8\u7528\u6237\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e7f\u6cdb\u7684\u6d4b\u8bd5\u548c\u6587\u6863\u8d44\u6599\uff1a<\/strong> \u8be5\u9879\u76ee\u62e5\u6709\u5e7f\u6cdb\u7684\u5355\u5143\u6d4b\u8bd5\u548c\u6587\u6863\uff0c\u786e\u4fddWebSocket\u534f\u8bae\u7684\u5b9e\u73b0\u662f\u53ef\u9760\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001GOBWAS\/WS<\/p>\n<p>gobwas\/ws\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u3001\u7075\u6d3b\u7684WebSocket\u5e93\u3002\u5b83\u5141\u8bb8\u7528\u6237\u7ec6\u81f4\u5730\u63a7\u5236WebSocket\u8fde\u63a5\u7684\u5404\u4e2a\u65b9\u9762\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316\uff1a<\/strong> \u7279\u522b\u6ce8\u91cd\u4e8e\u6027\u80fd\uff0c\u4f7f\u7528\u4e86\u8bf8\u5982\u5185\u5c42\u51fd\u6570\u548c\u624b\u52a8\u5185\u5b58\u7ba1\u7406\u7b49\u6280\u672f\uff0c\u4ee5\u53ca\u63d0\u4f9b\u4e86\u65e0GC\u538b\u529b\u7684\u5b9e\u73b0\u7b56\u7565\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7075\u6d3b\u7684API\u8bbe\u8ba1\uff1a<\/strong> \u4e0eGorilla WebSocket\u4e0d\u540c\uff0c\u8be5\u5e93\u66f4\u5173\u6ce8\u63d0\u4f9b\u4f4e\u7ea7\u522b\u7684\u64cd\u4f5c\uff0c\u5141\u8bb8\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u548c\u63a7\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u4e09\u3001WEB SOCKET<\/p>\n<p>websocket\u5e93\u662f\u4e00\u4e2aGo\u6807\u51c6\u5e93 net\/http \u8f7b\u91cf\u7ea7\u7684\u589e\u5f3a\uff0c\u5b83\u65e8\u5728\u63d0\u4f9b\u66f4\u76f4\u63a5\u7684WebSocket\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6613\u4e8e\u96c6\u6210\uff1a<\/strong> \u5b83\u6613\u4e8e\u4e0eGo\u8bed\u8a00\u7684\u6807\u51c6net\/http\u5e93\u96c6\u6210\uff0c\u4e0d\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u4f9b\u57fa\u672c\u7684WebSocket\u7279\u6027\uff1a<\/strong> \u867d\u7136\u529f\u80fd\u4e0d\u5982\u4e0a\u8ff0\u4e24\u4e2a\u5e93\u4e30\u5bcc\uff0c\u4f46\u5b83\u8db3\u591f\u5e94\u4ed8\u57fa\u672c\u7684WebSocket\u901a\u4fe1\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u56db\u3001NHOORY.IO\/WEBSOCKET<\/p>\n<p>nhooyr.io\/websocket\u662f\u4e00\u4e2a\u6700\u65b0\u7684\u3001\u73b0\u4ee3\u7684WebSocket\u5e93\uff0c\u5b83\u4e13\u6ce8\u4e8e\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u3001\u5065\u58ee\u7684WebSocket\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u73b0\u4ee3API\uff1a<\/strong> nhooyr.io\/websocket\u5229\u7528\u6700\u65b0\u7684Go\u8bed\u8a00\u7279\u6027\uff0c\u4f8b\u5982context\u4e0a\u4e0b\u6587\u548c\u9519\u8bef\u5904\u7406\uff0c\u6765\u63d0\u4f9b\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684API\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b26\u5408\u6807\u51c6\uff1a<\/strong> \u548cGorilla\u4e00\u6837\uff0c\u8be5\u5e93\u4e25\u683c\u9075\u5faaRFC 6455\u6807\u51c6\uff0c\u5e76\u63d0\u4f9b\u826f\u597d\u7684\u6587\u6863\u548c\u6d4b\u8bd5\u6765\u652f\u6301\u5176\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u5728\u6df1\u5165\u6bd4\u8f83\u8fd9\u4e9b\u5e93\u4e4b\u524d\uff0c\u91cd\u8981\u7684\u662f\u60f3\u6e05\u695a\u4f60\u7684\u9879\u76ee\u9700\u6c42\u3002\u4f60\u662f\u5426\u9700\u8981\u4e00\u4e2a\u5b8c\u5168\u7b26\u5408\u6807\u51c8\u7684\u5b9e\u73b0\uff1f\u4f60\u662f\u5426\u9700\u8981\u7279\u522b\u5173\u6ce8\u6027\u80fd\uff1f\u6216\u8005\u4f60\u53ea\u662f\u9700\u8981\u4e00\u4e2a\u80fd\u5feb\u901f\u96c6\u6210\u548c\u4f7f\u7528\u7684\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u5e93\u7684\u7279\u6027\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u6709\u54ea\u4e9b\u503c\u5f97\u63a8\u8350\u7684Go\u5f00\u6e90\u9879\u76ee\u53ef\u4ee5\u7528\u4e8eWebSocket\u5f00\u53d1\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p><strong>Gorilla WebSocket\uff1a<\/strong> \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u53d7\u6b22\u8fce\u7684Go WebSocket\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u548c\u6613\u7528\u6027\uff0c\u652f\u6301WebSocket\u7684\u6240\u6709\u6807\u51c6\u529f\u80fd\u3002<\/p>\n<\/li>\n<li>\n<p><strong>gobwas\/ws\uff1a<\/strong> \u8fd9\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Go WebSocket\u5e93\uff0c\u7279\u70b9\u662f\u4f4e\u5185\u5b58\u5360\u7528\u548c\u9ad8\u6027\u80fd\u3002\u5b83\u4e13\u6ce8\u4e8e\u63d0\u4f9b\u9ad8\u6548\u7684\u6d88\u606f\u5904\u7406\u548c\u6269\u5c55\u6027\u3002<\/p>\n<\/li>\n<li>\n<p><strong>nhooyr\/websocket\uff1a<\/strong> \u8fd9\u662f\u4e00\u4e2a\u65b0\u7684Go WebSocket\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u4f18\u96c5\u7684API\uff0c\u652f\u6301\u57fa\u4e8e\u4e8b\u4ef6\u7684\u56de\u8c03\u548c\u9ad8\u7ea7\u7279\u6027\uff0c\u5982\u5206\u7247\uff0c\u538b\u7f29\u548c\u5b50\u534f\u8bae\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>2. \u5728\u4f7f\u7528Go\u7684WebSocket\u9879\u76ee\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u6570\u636e\u7684\u4f20\u8f93\u5b89\u5168\u6027\uff1a<\/strong>\u7531\u4e8eWebSocket\u4f20\u8f93\u7684\u6570\u636e\u662f\u660e\u6587\u7684\uff0c\u5982\u679c\u4f20\u8f93\u7684\u662f\u654f\u611f\u4fe1\u606f\uff0c\u5efa\u8bae\u4f7f\u7528TLS\/SSL\u534f\u8bae\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u7ba1\u7406\u548c\u8d44\u6e90\u91ca\u653e\uff1a<\/strong>\u53ca\u65f6\u5173\u95edWebSocket\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\uff0c\u5c24\u5176\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u3002\u540c\u65f6\uff0c\u6839\u636e\u9700\u6c42\u9002\u65f6\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u7ba1\u7406WebSocket\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6d88\u606f\u5904\u7406\u548c\u534f\u8bae\u8bbe\u8ba1\uff1a<\/strong>\u5408\u7406\u8bbe\u8ba1\u6d88\u606f\u7684\u683c\u5f0f\u548c\u534f\u8bae\uff0c\u786e\u4fdd\u5728\u63a5\u6536\u7aef\u80fd\u591f\u6b63\u786e\u89e3\u6790\u548c\u5904\u7406\uff0c\u907f\u514d\u51fa\u73b0\u6570\u636e\u4e22\u5931\u6216\u89e3\u6790\u9519\u8bef\u7684\u60c5\u51b5\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>3. \u5982\u4f55\u4f18\u5316Go\u7684WebSocket\u6027\u80fd\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u5e76\u53d1\u5904\u7406\uff1a<\/strong>\u5408\u7406\u5229\u7528Goroutine\u5e76\u53d1\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u786e\u4fddWebSocket\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u652f\u6301\u5e76\u53d1\u8bf7\u6c42\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u7f13\u5b58\uff1a<\/strong>\u5bf9\u4e8e\u91cd\u590d\u7684\u8ba1\u7b97\u6216\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u6bd4\u5982\uff0c\u7f13\u5b58\u5df2\u89e3\u6790\u7684JSON\u6570\u636e\uff0c\u907f\u514d\u91cd\u590d\u89e3\u6790\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u62c6\u5206\u903b\u8f91\uff1a<\/strong>\u5c06\u590d\u6742\u7684\u5904\u7406\u903b\u8f91\u62c6\u5206\u4e3a\u591a\u4e2a\u5c0f\u7684\u5de5\u4f5c\u5355\u5143\uff0c\u4ee5\u66f4\u597d\u5730\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u548c\u5e76\u53d1\u6027\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u76d1\u6d4b\u548c\u8c03\u4f18\uff1a<\/strong>\u4f7f\u7528Go\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u5982pprof\u548ctrace\uff0c\u5b9a\u671f\u76d1\u6d4b\u548c\u6539\u8fdbWebSocket\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"Go\u8bed\u8a00\u5728WebSocket\u65b9\u9762\u62e5\u6709\u591a\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u4f8b\u5982Gorilla WebSocket\u3001gobwas\/ws\u3001 [&hellip;]","protected":false},"author":3,"featured_media":276842,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/276827"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=276827"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/276827\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/276842"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=276827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=276827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=276827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}