{"id":767,"date":"2011-11-03T03:51:52","date_gmt":"2011-11-03T03:51:52","guid":{"rendered":"http:\/\/fdream.net\/blog\/?p=767"},"modified":"2020-09-07T04:37:42","modified_gmt":"2020-09-07T04:37:42","slug":"websocket-draft-10%e5%90%91%e5%ae%a2%e6%88%b7%e7%ab%af%e5%86%99%e6%96%87%e6%9c%ac%e6%95%b0%e6%8d%ae%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/767","title":{"rendered":"WebSocket Draft 10\u5411\u5ba2\u6237\u7aef\u5199\u6587\u672c\u6570\u636e\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>\u5728Draft 10\u4e2d\uff0c\u5982\u679c\u89e3\u6790\u6570\u636e\u7684\u8fc7\u7a0b\u5f04\u6e05\u695a\u4e86\uff0c\u8fd9\u4e2a\u5c31\u66f4\u7b80\u5355\u4e86\uff0c\u8fd4\u56de\u6570\u636e\u7684\u683c\u5f0f\u548c\u4e4b\u524d\u63a5\u53d7\u5230\u7684\u6570\u636e\u683c\u5f0f\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u662f\u4f60\u4e0d\u7528\u751f\u6210mask\u4e86\uff0c\u5934\u90e8\u7684\u5176\u4ed6\u683c\u5f0f\u8fd8\u662f\u4e00\u6a21\u4e00\u6837\u7684\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u5b57\u8282\u8fd8\u662f\u56fa\u5b9a\u7684\uff0c\u662f0x81\uff0c\u610f\u4e49\u548c\u63a5\u53d7\u6570\u636e\u7684\u610f\u4e49\u4e00\u6837\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u8282\u4e5f\u662f\uff0c\u540e\u4e03\u4e2a\u4f4d\u8868\u793a\u6570\u636e\u957f\u5ea6\uff0c\u7531\u4e8e\u6ca1\u6709mask\uff0c\u6240\u4ee5\u7b2c\u4e00\u4f4d\u662f0\uff1b\u957f\u5ea6\u7684\u8868\u793a\u65b9\u6cd5\u548c\u63a5\u53d7\u7684\u6807\u8bc6\u65b9\u6cd5\u4e00\u81f4\uff0c\u53ef\u80fd\u75287\u4f4d\u8868\u793a\uff0c\u4e5f\u53ef\u80fd\u752816\u4f4d\u8868\u793a\u3002<\/p>\n<p>\u7528NodeJS\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre lang=\"js\">\nvar socketWriter = {\n    'draft10': function(socket, data){\n        var frames,\n            length = new Buffer(data, 'utf8').length;\n        if(data.length > 0x7d){\n            frames = new Buffer(4);\n            frames[0] = 0x81;\n            frames[1] = 0x7e;\n            frames[2] = length >> 8;\n            frames[3] = length & 0xFF; \/\/1111 1111\n        }\n        else{\n            frames = new Buffer(2);\n            frames[0] = 0x81;\n            frames[1] = length;\n        }\n        \n        if (socket.writable) {\n            socket.write(frames, 'binary');\n            socket.write(data, 'utf8');\n            return true;\n        }\n        return false;\n    },\n    'draft76': function(socket, data){\n        var byteLen = Buffer.byteLength(data, 'utf8'),\n            bytes = new Buffer(byteLen + 2);\n\n        bytes[0] = 0x00;\n        bytes.write(data, 1, 'utf8');\n        bytes[byteLen + 1] = 0xFF;\n\n        return socket.write(bytes);\n    }\n};\n\nsocketWriter['draft75'] = socketWriter['draft76'];\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Draft 10\u4e2d\uff0c\u5982\u679c\u89e3\u6790\u6570\u636e\u7684\u8fc7\u7a0b\u5f04\u6e05\u695a\u4e86\uff0c\u8fd9\u4e2a\u5c31\u66f4\u7b80\u5355\u4e86\uff0c\u8fd4\u56de\u6570\u636e\u7684\u683c\u5f0f\u548c\u4e4b\u524d\u63a5\u53d7\u5230\u7684\u6570\u636e\u683c\u5f0f\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u662f\u4f60\u4e0d\u7528\u751f\u6210mask\u4e86\uff0c\u5934\u90e8\u7684\u5176\u4ed6\u683c\u5f0f\u8fd8\u662f\u4e00\u6a21\u4e00\u6837\u7684\u3002 \u7b2c\u4e00\u4e2a\u5b57\u8282\u8fd8\u662f\u56fa\u5b9a\u7684\uff0c\u662f0x81\uff0c\u610f\u4e49\u548c\u63a5\u53d7\u6570\u636e\u7684\u610f\u4e49\u4e00\u6837\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u8282\u4e5f\u662f\uff0c\u540e\u4e03\u4e2a\u4f4d\u8868\u793a\u6570\u636e\u957f\u5ea6\uff0c\u7531\u4e8e\u6ca1\u6709mask\uff0c\u6240\u4ee5\u7b2c\u4e00\u4f4d\u662f0\uff1b\u957f\u5ea6\u7684\u8868\u793a\u65b9\u6cd5\u548c\u63a5\u53d7\u7684\u6807\u8bc6\u65b9\u6cd5\u4e00\u81f4\uff0c\u53ef\u80fd\u75287\u4f4d\u8868\u793a\uff0c\u4e5f\u53ef\u80fd\u752816\u4f4d\u8868\u793a\u3002 \u7528NodeJS\u5b9e\u73b0\u5982\u4e0b\uff1a var socketWriter = { &#8216;draft10&#8217;: function(socket, data){ var frames, length = new Buffer(data, &#8216;utf8&#8217;).length; if(data.length > 0x7d){ frames = new Buffer(4); frames[0] = 0x81; frames[1] = 0x7e; frames[2] = length >> 8; frames[3] = length &#038; 0xFF; \/\/1111 1111 } else{ frames = new Buffer(2); frames[0] = 0x81; frames[1] = length; &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[365,364],"class_list":["post-767","post","type-post","status-publish","format-standard","hentry","category-coding","tag-nodejs","tag-websocket"],"views":29125,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=767"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/767\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}