{"id":238300,"date":"2022-04-30T09:50:45","date_gmt":"2022-04-30T01:50:45","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=238300"},"modified":"2022-04-19T11:53:03","modified_gmt":"2022-04-19T03:53:03","slug":"websocket-contact","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/websocket-contact.html","title":{"rendered":"\u4ec0\u4e48\u662fWebSocket\u534f\u8bae"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nWebSocket \u662fHTML5\u5f00\u59cb\u63d0\u4f9b\u7684\u4e00\u79cd\u6d4f\u89c8\u5668\u4e0e\u670d\u52a1\u5668\u95f4\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u8baf\u7684\u7f51\u7edc\u6280\u672f,\u4e00\u79cd\u57fa\u4e8e TCP \u8fde\u63a5\u4e0a\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u534f\u8bae\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u968f\u7740\u79d1\u6280\u53d1\u5c55\uff0c\u4eba\u4eec\u9700\u6c42\u8d8a\u6765\u8d8a\u591a\uff0c\u751f\u6d3b\u7684\u65b9\u65b9\u9762\u9762\u90fd\u79bb\u4e0d\u5f00\u4e00\u4e9b\u5b9e\u65f6\u4fe1\u606f\u3002\u6bd4\u5982\uff1a\u75ab\u60c5\u671f\u95f4\u5728\u5bb6\u534f\u540c\u529e\u516c\u3001\u75ab\u60c5\u76d1\u63a7\u76ee\u6807\u4eba\u7684\u5b9e\u65f6\u8fd0\u52a8\u8f68\u8ff9\u3001\u793e\u4ea4\u4e2d\u7684\u5b9e\u65f6\u6d88\u606f\u3001\u591a\u73a9\u5bb6\u4e92\u52a8\u6e38\u620f\u3001\u6bcf\u79d2\u77ac\u606f\u4e07\u53d8\u7684\u80a1\u5e02\u57fa\u91d1\u62a5\u4ef7\u3001\u4f53\u80b2\u5b9e\u51b5\u64ad\u653e\u3001\u97f3\u89c6\u9891\u804a\u5929\u3001\u89c6\u9891\u4f1a\u8bae\u3001\u5728\u7ebf\u6559\u80b2\u7b49\u7b49\uff0c\u90fd\u53ef\u4ee5\u501f\u7528WebSocket TCP\u94fe\u63a5\u53ef\u4ee5\u8ba9\u6570\u636e\u98de\u8d77\u6765\u3002\u4e0b\u9762\u5c31\u804a\u4e00\u4e0bWebSocket\u534f\u8bae\u3002<\/p>\n

WebSocket \u662fHTML5\u5f00\u59cb\u63d0\u4f9b\u7684\u4e00\u79cd\u6d4f\u89c8\u5668\u4e0e\u670d\u52a1\u5668\u95f4\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u8baf\u7684\u7f51\u7edc\u6280\u672f,\u4e00\u79cd\u57fa\u4e8e TCP \u8fde\u63a5\u4e0a\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u534f\u8bae\uff0c\u76f8\u5bf9\u4e8e HTTP \u8fd9\u79cd\u975e\u6301\u4e45\u7684\u534f\u8bae\u6765\u8bf4\uff0cWebSocket \u662f\u4e00\u4e2a\u6301\u4e45\u5316\u7f51\u7edc\u901a\u4fe1\u7684\u534f\u8bae\u3002\u4f9d\u9760\u8fd9\u79cd\u6280\u672f\u53ef\u4ee5\u5b9e\u73b0\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7aef\u7684\u957f\u8fde\u63a5\uff0c\u53cc\u5411\u5b9e\u65f6\u901a\u4fe1\u3002<\/p>\n

\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5b9e\u73b0\u5ba2\u6237\u7aef\u8bf7\u6c42\u670d\u52a1\u5668\uff0c\u540c\u65f6\u53ef\u4ee5\u5141\u8bb8\u670d\u52a1\u7aef\u4e3b\u52a8\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u6570\u636e\u3002\u662f\u771f\u6b63\u7684\u53cc\u5411\u5e73\u7b49\u5bf9\u8bdd\uff0c\u5c5e\u4e8e\u670d\u52a1\u5668\u63a8\u9001\u6280\u672f\u7684\u4e00\u79cd\u3002\u5728 WebSocket API \u4e2d\uff0c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u53ea\u9700\u8981\u5b8c\u6210\u4e00\u6b21\u63e1\u624b\uff0c\u4e24\u8005\u4e4b\u95f4\u5c31\u76f4\u63a5\u53ef\u4ee5\u521b\u5efa\u6301\u4e45\u6027\u7684\u8fde\u63a5\uff0c\u5e76\u8fdb\u884c\u53cc\u5411\u6570\u636e\u4f20\u8f93\u3002<\/p>\n

\u300c\u5176\u4ed6\u7279\u70b9\u5305\u62ec\uff1a\u300d<\/p>\n

  • \u5efa\u7acb\u5728 TCP \u534f\u8bae\u4e4b\u4e0a\uff0c\u670d\u52a1\u5668\u7aef\u7684\u5b9e\u73b0\u6bd4\u8f83\u5bb9\u6613\u3002<\/li>\n
  • \u4e0e HTTP \u534f\u8bae\u6709\u7740\u826f\u597d\u7684\u517c\u5bb9\u6027\u3002\u9ed8\u8ba4\u7aef\u53e3\u4e5f\u662f80\u548c443\uff0c\u5e76\u4e14\u63e1\u624b\u9636\u6bb5\u91c7\u7528 HTTP \u534f\u8bae\uff0c\u56e0\u6b64\u63e1\u624b\u65f6\u4e0d\u5bb9\u6613\u5c4f\u853d\uff0c\u80fd\u901a\u8fc7\u5404\u79cd HTTP \u4ee3\u7406\u670d\u52a1\u5668\u3002<\/li>\n
  • \u6570\u636e\u683c\u5f0f\u6bd4\u8f83\u8f7b\u91cf\uff0c\u6027\u80fd\u5f00\u9500\u5c0f\uff0c\u901a\u4fe1\u9ad8\u6548\u3002<\/li>\n
  • \u53ef\u4ee5\u53d1\u9001\u6587\u672c\uff0c\u4e5f\u53ef\u4ee5\u53d1\u9001\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/li>\n
  • \u6ca1\u6709\u540c\u6e90\u9650\u5236\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u4e0e\u4efb\u610f\u670d\u52a1\u5668\u901a\u4fe1\u3002<\/li>\n
  • \u534f\u8bae\u6807\u8bc6\u7b26\u662fws(\u5982\u679c\u52a0\u5bc6\uff0c\u5219\u4e3awss)\uff0c\u670d\u52a1\u5668\u7f51\u5740\u5c31\u662f URL\u3002<\/li>\n
  • \u534f\u8bae\u6807\u8bc6\u7b26\u662fws(\u5982\u679c\u52a0\u5bc6\uff0c\u5219\u4e3awss)\uff0c\u670d\u52a1\u5668\u7f51\u5740\u5c31\u662f URL\u3002<\/li>\n
    ws:\/\/xxx.ayunw.cn:80\/some\/path\r\nwss:\/\/xxx.ayunw.cn:443\/some\/path<\/pre>\n

    \u53e6\u5916\u5ba2\u6237\u7aef\u4e0d\u53ea\u662f\u6d4f\u89c8\u5668\uff0c\u53ea\u8981\u5b9e\u73b0\u4e86ws\u6216\u8005wss\u534f\u8bae\u7684\u5ba2\u6237\u7aefsocket\u90fd\u53ef\u4ee5\u548c\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n

    \u5148\u8bf4\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u9700\u8981 WebSocket \u534f\u8bae?<\/strong><\/div>\n

    \u5728 Web \u5e94\u7528\u67b6\u6784\u4e2d\uff0c\u8fde\u63a5\u7531 HTTP\/1.0 \u548c HTTP\/1.1 \u5904\u7406\u3002HTTP \u662f\u5ba2\u6237\u7aef\/\u670d\u52a1\u5668\u6a21\u5f0f\u4e2d \u8bf7\u6c42\u4e00\u54cd\u5e94 \u6240\u7528\u7684\u534f\u8bae\uff0c\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e2d\uff0c\u5ba2\u6237\u7aef(\u4e00\u822c\u662f\u6d4f\u89c8\u5668)\u5411\u670d\u52a1\u5668\u63d0\u4ea4 HTTP \u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u54cd\u5e94\u8bf7\u6c42\u7684\u8d44\u6e90(\u4f8b\u5982 HTML \u9875\u9762)\u3002<\/p>\n

    HTTP \u662f\u65e0\u72b6\u6001\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u5c06\u6bcf\u4e2a\u8bf7\u6c42\u5f53\u6210\u552f\u4e00\u548c\u72ec\u7acb\u7684\u3002\u65e0\u72b6\u6001\u534f\u8bae\u5177\u6709\u4e00\u4e9b\u4f18\u52bf\uff0c\u4f8b\u5982\uff0c\u670d\u52a1\u5668\u4e0d\u9700\u8981\u4fdd\u5b58\u6709\u5173\u4f1a\u8bdd\u7684\u4fe1\u606f\uff0c\u4ece\u800c\u4e0d\u9700\u8981\u5b58\u50a8\u6570\u636e\u3002\u4f46\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u5728\u6bcf\u6b21 HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u4e2d\u90fd\u4f1a\u53d1\u9001\u5173\u4e8e\u8bf7\u6c42\u7684\u5197\u4f59\u4fe1\u606f\uff0c\u6bd4\u5982\u4f7f\u7528 Cookie \u8fdb\u884c\u7528\u6237\u72b6\u6001\u7684\u9a8c\u8bc1\u3002<\/p>\n

    \u968f\u7740\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4ea4\u4e92\u7684\u589e\u52a0\uff0cHTTP \u534f\u8bae\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u901a\u4fe1\u6240\u9700\u8981\u7684\u4fe1\u606f\u91cf\u5feb\u901f\u589e\u52a0\u3002<\/p>\n

    \u4ece\u6839\u672c\u4e0a\u8bb2\uff0cHTTP \u8fd8\u662f \u534a\u53cc\u5de5 \u7684\u534f\u8bae\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u540c\u4e00\u65f6\u523b\u4fe1\u606f\u7684\u6d41\u5411\u53ea\u80fd\u5355\u5411\u7684\uff1a\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42(\u5355\u5411)\uff0c\u7136\u540e\u670d\u52a1\u5668\u54cd\u5e94\u8bf7\u6c42(\u5355\u5411)\u3002\u534a\u53cc\u5de5\u65b9\u5f0f\u7684\u901a\u4fe1\u6548\u7387\u662f\u975e\u5e38\u4f4e\u7684\u3002<\/p>\n

    \u540c\u65f6 HTTP \u534f\u8bae\u6709\u4e00\u4e2a\u7f3a\u9677\uff1a\u901a\u4fe1\u53ea\u80fd\u7531\u5ba2\u6237\u7aef\u53d1\u8d77\u3002<\/p>\n

    \u8fd9\u79cd\u5355\u5411\u8bf7\u6c42\u7684\u7279\u70b9\uff0c\u6ce8\u5b9a\u4e86\u5982\u679c\u670d\u52a1\u5668\u6709\u72b6\u6001\u53d8\u5316\uff0c\u662f\u65e0\u6cd5\u4e3b\u52a8\u901a\u77e5\u5ba2\u6237\u7aef\u7684\u3002<\/p>\n

    \u4e3a\u4e86\u80fd\u591f\u53ca\u65f6\u7684\u83b7\u53d6\u670d\u52a1\u5668\u7684\u53d8\u5316\uff0c\u6211\u4eec\u5c1d\u8bd5\u8fc7\u5404\u79cd\u5404\u6837\u7684\u65b9\u5f0f\uff1a<\/p>\n

  • \u8f6e\u8be2(polling)\uff1a\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\uff0c\u5c31\u53d1\u51fa\u4e00\u4e2a\u8bf7\u6c42\uff0c\u4e86\u89e3\u670d\u52a1\u5668\u6709\u6ca1\u6709\u65b0\u7684\u4fe1\u606f\u3002\u4e0d\u7cbe\u51c6\uff0c\u6709\u5ef6\u65f6\uff0c\u5927\u91cf\u65e0\u6548\u6570\u636e\u4ea4\u6362\u3002<\/li>\n
  • \u957f\u8f6e\u8be2( long polling)\uff1a\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u8bf7\u6c42\u4fe1\u606f\uff0c\u5e76\u5728\u8bbe\u5b9a\u7684\u65f6\u95f4\u6bb5\u5185\u4fdd\u6301\u8fde\u63a5\u3002\u76f4\u5230\u670d\u52a1\u5668\u6709\u65b0\u6d88\u606f\u54cd\u5e94\uff0c\u6216\u8005\u8fde\u63a5\u8d85\u65f6\uff0c\u8fd9\u79cd\u6280\u672f\u5e38\u5e38\u79f0\u4f5c\u201c\u6302\u8d77GET\u201d\u6216\u201c\u6401\u7f6ePOST\u201d\u3002\u5360\u7528\u670d\u52a1\u5668\u8d44\u6e90\uff0c\u76f8\u5bf9\u8f6e\u8be2\u5e76\u6ca1\u6709\u4f18\u52bf\uff0c\u6ca1\u6709\u6807\u51c6\u5316\u3002<\/li>\n
  • \u6d41\u5316\u6280\u672f\uff1a\u5728\u6d41\u5316\u6280\u672f\u4e2d\uff0c\u5ba2\u6237\u7aef\u53d1\u9001\u4e00\u4e2a\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u53d1\u9001\u5e76\u7ef4\u62a4\u4e00\u4e2a\u6301\u7eed\u66f4\u65b0\u548c\u4fdd\u6301\u6253\u5f00(\u53ef\u4ee5\u662f\u65e0\u9650\u6216\u8005\u89c4\u5b9a\u7684\u65f6\u95f4\u6bb5)\u7684\u5f00\u653e\u54cd\u5e94\u3002\u6bcf\u5f53\u670d\u52a1\u5668\u6709\u9700\u8981\u4ea4\u4ed8\u7ed9\u5ba2\u6237\u7aef\u7684\u4fe1\u606f\u65f6\uff0c\u5b83\u5c31\u66f4\u65b0\u54cd\u5e94\u3002\u670d\u52a1\u5668\u4ece\u4e0d\u53d1\u51fa\u5b8c\u6210 HTTP \u54cd\u5e94\u3002\u4ee3\u7406\u548c\u9632\u706b\u5899\u53ef\u80fd\u7f13\u5b58\u54cd\u5e94\uff0c\u5bfc\u81f4\u4fe1\u606f\u4ea4\u4ed8\u7684\u5ef6\u8fdf\u589e\u52a0\u3002<\/li>\n

    \u4e0a\u8ff0\u65b9\u6cd5\u63d0\u4f9b\u4e86\u8fd1\u4e4e\u5b9e\u65f6\u7684\u901a\u4fe1\uff0c\u4f46\u662f\u5b83\u4eec\u4e5f\u6d89\u53ca HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u9996\u6807\uff0c\u5305\u542b\u4e86\u8bb8\u591a\u9644\u52a0\u548c\u4e0d\u5fc5\u8981\u7684\u9996\u6807\u6570\u636e\u4e0e\u5ef6\u8fdf\u3002\u6b64\u5916\uff0c\u5728\u6bcf\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u5ba2\u6237\u7aef\u90fd\u5fc5\u987b\u7b49\u5f85\u8bf7\u6c42\u8fd4\u56de\uff0c\u624d\u80fd\u53d1\u51fa\u540e\u7eed\u7684\u8bf7\u6c42\uff0c\u800c\u8fd9\u663e\u8457\u5730\u589e\u52a0\u4e86\u5ef6\u9000\u3002\u540c\u65f6\u4e5f\u6781\u5927\u5730\u589e\u52a0\u4e86\u670d\u52a1\u5668\u7684\u538b\u529b\u3002<\/p>\n

    \u4ec0\u4e48\u662fwebsocket\u534f\u8bae?<\/strong><\/div>\n

    Websocket\u5176\u5b9e\u662f\u4e00\u4e2a\u65b0\u534f\u8bae\uff0c\u501f\u7528\u4e86HTTP\u7684\u534f\u8bae\u6765\u5b8c\u6210\u4e00\u90e8\u5206\u63e1\u624b\uff0c\u53ea\u662f\u4e3a\u4e86\u517c\u5bb9\u73b0\u6709\u6d4f\u89c8\u5668\u7684\u63e1\u624b\u89c4\u8303\u800c\u5df2\u3002Websocket \u662f\u4e00\u79cd\u81ea\u7136\u7684\u5168\u53cc\u5de5\u3001\u53cc\u5411\u3001\u5355\u5957\u63a5\u5b57\u8fde\u63a5\uff0c\u89e3\u51b3\u4e86 HTTP \u534f\u8bae\u4e2d\u4e0d\u9002\u5408\u4e8e\u5b9e\u65f6\u901a\u4fe1\u7684\u95ee\u9898\u3002<\/p>\n

    \u300c\u4e00\u4e2a\u5178\u578b\u7684Websocket\u63e1\u624b\u5982\u4e0b\uff1a\u300d<\/p>\n

    GET \/chat HTTP\/1.1\r\nHost: server.example.com\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\nSec-WebSocket-Protocol: chat, superchat\r\nSec-WebSocket-Version: 13\r\nOrigin: http:\/\/example.com<\/pre>\n

    \u5176\u4e2dWebsocket\u7684\u6838\u5fc3\u5982\u4e0b\uff0c\u5b83\u544a\u8bc9Apache\u3001Nginx\u7b49\u670d\u52a1\u5668\uff1a\u6ce8\u610f\uff0c\u6211\u53d1\u8d77\u7684\u662fWebsocket\u534f\u8bae\uff0c\u5feb\u70b9\u5e2e\u6211\u627e\u5230\u5bf9\u5e94\u7684\u52a9\u7406\u5904\u7406\u800c\u4e0d\u662f\u90a3\u4e2a\u8001\u571f\u7684HTTP\u3002<\/p>\n

    Upgrade: websocket\r\nConnection: Upgrade<\/pre>\n

    \u300c\u670d\u52a1\u5668\u8fd4\u56de\u5982\u4e0b\uff1a\u300d<\/p>\n

    HTTP\/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Protocol: chat<\/pre>\n

    \u81f3\u6b64\uff0cHTTP\u5df2\u7ecf\u5b8c\u6210\u5b83\u6240\u6709\u5de5\u4f5c\u4e86\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u5b8c\u5168\u6309\u7167Websocket\u534f\u8bae\u8fdb\u884c\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u968f\u7740\u79d1\u6280\u53d1\u5c55\uff0c\u4eba\u4eec\u9700\u6c42\u8d8a\u6765\u8d8a\u591a\uff0c\u751f\u6d3b\u7684\u65b9\u65b9\u9762\u9762\u90fd\u79bb\u4e0d\u5f00\u4e00\u4e9b\u5b9e\u65f6\u4fe1\u606f\u3002\u6bd4\u5982\uff1a\u75ab\u60c5\u671f\u95f4\u5728\u5bb6\u534f\u540c\u529e\u516c\u3001\u75ab\u60c5\u76d1\u63a7\u76ee\u6807\u4eba […]<\/p>\n","protected":false},"author":1920,"featured_media":211288,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-238300","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=238300"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238300\/revisions"}],"predecessor-version":[{"id":240822,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238300\/revisions\/240822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/211288"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=238300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=238300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=238300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}