{"id":389,"date":"2016-04-07T16:04:22","date_gmt":"2016-04-07T08:04:22","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=389"},"modified":"2016-04-07T16:21:00","modified_gmt":"2016-04-07T08:21:00","slug":"html5-websocket%e7%9a%84javascript%e6%8e%a5%e5%8f%a3%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=389","title":{"rendered":"HTML5 WebSocket\u7684JavaScript\u63a5\u53e3\u8be6\u89e3"},"content":{"rendered":"<p>\n\t\u5982\u679c\u6211\u4eec\u60f3\u5f7b\u5e95\u7684\u4e86\u89e3WebSocket\uff0c\u5c31\u5fc5\u987b\u5148\u4ece\u5b83\u7684\u63a5\u53e3\u5b9a\u4e49\u5b66\u8d77\u3002WebSocket\u548cHTTP\u534f\u8bae\u4e00\u6837\uff0c\u90fd\u662f\u57fa\u4e8eTCP\u534f\u8bae\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u9700\u5173\u6ce8\u5b83\u4eec\u7684\u63e1\u624b\u534f\u8bae\uff0c\u800c\u53ea\u9700\u5173\u6ce8\u5b9e\u9645\u5e94\u7528\u7684\u4e1a\u52a1\u548c\u76f8\u5173\u63a5\u53e3\u7684\u5b9e\u73b0\u3002<br \/>\n\tWebSocket\u5728\u8bbe\u8ba1\u65f6\uff0c\u9488\u5bf9Web\u5f00\u53d1\u4eba\u5458\u7684WebSocket JavaScript\u5ba2\u6237\u7aef\u63a5\u53e3\u662f\u975e\u5e38\u7b80\u5355\u7684\uff0c\u4ee5\u4e0b\u662fWebSocket JavaScript\u63a5\u53e3\u7684\u5b9a\u4e49\uff1a\n<\/p>\n<pre class=\"brush:cpp;\">\r\n[Constructor(in DOMString url, in optional DOMString protocol)]&nbsp;\r\n&nbsp;interface WebSocket {&nbsp;\r\n&nbsp; &nbsp;readonly attribute DOMString URL;&nbsp;\r\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ ready state&nbsp;\r\n&nbsp; &nbsp;const unsigned short CONNECTING = 0;&nbsp;\r\n&nbsp; &nbsp;const unsigned short OPEN = 1;&nbsp;\r\n&nbsp; &nbsp;const unsigned short CLOSED = 2;&nbsp;\r\n&nbsp; &nbsp;readonly attribute unsigned short readyState;&nbsp;\r\n&nbsp; &nbsp;readonly attribute unsigned long bufferedAmount;&nbsp;\r\n&nbsp; &nbsp;\/\/networking&nbsp;\r\n&nbsp; &nbsp;attribute Function onopen;&nbsp;\r\n&nbsp; &nbsp;attribute Function onmessage;&nbsp;\r\n&nbsp; &nbsp;attribute Function onclose;&nbsp;\r\n&nbsp; &nbsp;attribute Function onerror;&nbsp;\r\n&nbsp; &nbsp;boolean send(in DOMString data);&nbsp;\r\n&nbsp; &nbsp;void close();&nbsp;\r\n&nbsp;};&nbsp;\r\n&nbsp;WebSocket implements EventTarget;<\/pre>\n<p>\n\t<strong>URL<\/strong>\uff1a\u4ee3\u8868WebSocket\u670d\u52a1\u5668\u7684\u7f51\u7edc\u5730\u5740\uff0cHTTP \u534f\u8bae\u5f00\u5934\u4e00\u6837\uff0cWebSocket\u534f\u8bae\u7684URL\u4f7f\u7528ws:\/\/\u5f00\u5934\uff0c\u53e6\u5916\u5b89\u5168\u7684WebSocket\u534f\u8bae\u4f7f\u7528wss:\/\/\u5f00\u5934\u3002<br \/>\n\t<strong>onopen<\/strong>\uff1a\u5f53\u6d4f\u89c8\u5668\u548cWebSocketServer\u8fde\u63a5\u6210\u529f\u540e\uff0c\u4f1a\u89e6\u53d1onopen\u6d88\u606f<br \/>\n\t<strong>onerror<\/strong>\uff1a\u5982\u679c\u8fde\u63a5\u5931\u8d25\uff0c\u53d1\u9001\u3001\u63a5\u6536\u6570\u636e\u5931\u8d25\u6216\u8005\u5904\u7406\u6570\u636e\u51fa\u73b0\u9519\u8bef\uff0cbrowser\u4f1a\u89e6\u53d1onerror\u6d88\u606f<br \/>\n\t<strong>onmessage<\/strong>\uff1a\u5f53Browser\u63a5\u6536\u5230WebSocketServer\u53d1\u9001\u8fc7\u6765\u7684\u6570\u636e\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1onmessage\u6d88\u606f<br \/>\n\t<strong>send<\/strong>\uff1a\u65b9\u6cd5\u5c31\u662f\u53d1\u9001\u6570\u636e\u5230\u670d\u52a1\u5668\u7aef<br \/>\n\t<strong>onclose<\/strong>\uff1a\u5f53\u6d4f\u89c8\u5668\u63a5\u6536\u5230WebSocketServer\u7aef\u53d1\u9001\u7684\u5173\u95ed\u8fde\u63a5\u8bf7\u6c42\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1onclose\u6d88\u606f\n<\/p>\n<p>\n\t\u91cd\u70b9\uff1a<strong>api\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u662f\u91c7\u7528\u5f02\u6b65\u56de\u8c03\u7684\u65b9\u5f0f\u89e6\u53d1\uff0c\u8fd9\u6837\u4e0d\u4f1a\u963b\u585eUI\uff0c\u53ef\u4ee5\u83b7\u5f97\u66f4\u5feb\u7684\u54cd\u5e94\u65f6\u95f4\uff0c\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<br \/>\n\t\u4e3b\u6d41\u7684\u6d4f\u89c8\u5668\uff08\u5305\u62ecPC\u548c\u79fb\u52a8\u7ec8\u7aef\uff09\u73b0\u5df2\u90fd\u652f\u6301\u6807\u51c6\u7684HTML5\u7684WebSocket API\uff0c\u8fd9\u610f\u5473\u7740\u5ba2\u6237\u7aef\u7684WebSocket JavaScirpt\u811a\u672c\u5177\u5907\u826f\u597d\u7684\u4e00\u81f4\u6027\u548c\u8de8\u5e73\u53f0\u7279\u6027\u3002<\/strong><br \/>\n\tWebSockets\u652f\u6301\u53cc\u5411\u3001\u5168\u53cc\u5de5\u901a\u4fe1\u4fe1\u9053\u3002\u53ea\u8981\u6211\u4eec\u901a\u8fc7HTTP\u8bf7\u6c42\uff08WebSockets\u7684\u63e1\u624b\uff09\u6210\u529f\uff0c\u90a3\u4e48\u8fde\u63a5\u4e00\u76f4\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7js\u811a\u672c\u5b9e\u65f6\u7684\u548c\u670d\u52a1\u5668\u8fdb\u884c\u4ea4\u4e92\u3002\n<\/p>\n<p>\n\t\u4ee5\u4e0b\u5217\u4e3e\u90e8\u5206\u4e3b\u6d41\u6d4f\u89c8\u5668\u652f\u6301\u60c5\u51b5\uff1a\n<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:500px\">\n<thead>\n<tr>\n<th scope=\"col\">\n\t\t\t\t\u6d4f\u89c8\u5668\n\t\t\t<\/th>\n<th scope=\"col\">\n\t\t\t\t\u652f\u6301\u60c5\u51b5\n\t\t\t<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n\t\t\t\tChrome\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u652f\u6301 version 4+\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tFirefox\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u652f\u6301 version 4+\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tInternet Explorer\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u652f\u6301 version 10+\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tOpera\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u652f\u6301 version 10+\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tSafari\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u652f\u6301 version 5+\n\t\t\t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\t\u539f\u6587\u5730\u5740\uff1a<a id=\"http:\/\/www.xttblog.com\/?p=389\" name=\"http:\/\/www.xttblog.com\/?p=389\">http:\/\/www.xttblog.com\/?p=389<\/a><\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=389\">HTML5 WebSocket\u7684JavaScript\u63a5\u53e3\u8be6\u89e3<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u6211\u4eec\u60f3\u5f7b\u5e95\u7684\u4e86\u89e3WebSocket\uff0c\u5c31\u5fc5\u987b\u5148\u4ece\u5b83\u7684\u63a5\u53e3\u5b9a\u4e49\u5b66\u8d77\u3002WebSocket\u548cHTTP\u534f\u8bae\u4e00\u6837\uff0c\u90fd\u662f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[138,137,139],"class_list":["post-389","post","type-post","status-publish","format-standard","hentry","category-html5","tag-echoendpoint","tag-websocket","tag-139"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=389"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":391,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/389\/revisions\/391"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}