未加星标

How do you process a basic Websocket frame

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I am writing my own implementation for web sockets instead of socket.io and the like.

I have a good handshake but when the client sends to the server I can't figure out how to turn that data into anything useful. Is it an object? Is it a string? Docs say it is an array of raw memory locations outside the V8 heap. ...?


How do you process a basic Websocket frame
FUNCTIONING EXAMPLE (client is hard coded string) var http = require("http"); var crypto = require("crypto"); var server = http.createServer(); server.on("upgrade", function (req, socket, upgradeHead) { var crypto = require("crypto"); var shasum = crypto.createHash("sha1"); shasum.update(req.headers["sec-websocket-key"]); shasum.update("258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); var hash = shasum.digest("hex"); var myVal = new Buffer(hash, "hex").toString('base64'); var head = ""; head += "HTTP/1.1 101 Switching Protocols\r\n"; head += "Connection: Upgrade\r\n"; head += "Upgrade: WebSocket\r\n"; head += "Sec-WebSocket-Accept: " + myVal + "\r\n\r\n"; socket.setEncoding("utf8"); socket.write(head); socket.ondata = function (data, start, end) { var data = data.toString("utf8", start, end); console.log(" start: " + start + "\n end: " + end + "\n data: " + data); }; }); server.on("request", function (req, res) { if (req.url === "/e") process.exit(); if (req.url.indexOf("favicon") !== -1) return; var html = "\ <!DOCTYPE html>\r\n\ <html>\r\n\ <head>\r\n\ <script>\r\n\ var connection = new WebSocket('ws:localhost:80');\r\n\ connection.onopen = function () {\r\n\ console.log('OPEN SUCCESS');\r\n\ connection.send('I am a message from the client.');\r\n\ };\ connection.onmessage = function(msg) {\r\n\ console.log(msg);\r\n\ }\r\n\ connection.onerror = function (e) { console.log('ERROR'); console.log(e); };\r\n\ connection.onclose = function (e) { console.log('CLOSE'); console.log(e);};\r\n\ </script>\r\n\ </head>\r\n\ </html>"; res.writeHead(200, { "Content-Type": "text/html" }); res.write(html); res.end(); }); server.listen(80);

node docs - socket.on(data, myFunc);

node docs - Buffer object

Tutorial I am using

Detailed WebSocket Documentation

eazy-peezy wikipedia handshake explanation

Problem courtesy of: user1873073

Solution

The main issue is that the WebSocket protocol has evolved a lot since that tutorial was written. If you read the spec that you linked to, section 5.2 talks about the data framing.

http://tools.ietf.org/html/rfc6455#page-28

The main issue that makes your data gibberish is that the data is automatically masked when sent, so you need to process the frame.

Here's an example to decode your sample code. You will need to expand it to cover larger lengths, and handle other parts of the spec.

socket.ondata = function (data, start, end) { var message = data.slice(start, end); var FIN = (message[0] & 0x80); var RSV1 = (message[0] & 0x40); var RSV2 = (message[0] & 0x20); var RSV3 = (message[0] & 0x10); var Opcode = message[0] & 0x0F; var mask = (message[1] & 0x80); var length = (message[1] & 0x7F); var nextByte = 2; if (length === 126) { // length = next 2 bytes nextByte += 2; } else if (length === 127){ // length = next 8 bytes nextByte += 8; } var maskingKey = null; if (mask){ maskingKey = message.slice(nextByte, nextByte + 4); nextByte += 4; } var payload = message.slice(nextByte, nextByte + length); if (maskingKey){ for (var i = 0; i < payload.length; i++){ payload[i] = payload[i] ^ maskingKey[i % 4]; } } console.log(payload.toString()); };

Solution courtesy of: loganfsmyth

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:How do you process a basic Websocket frame
本站链接:https://www.codesec.net/view/611819.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(11)