gpt4 book ai didi

node.js - 你如何处理一个基本的 Websocket 框架

转载 作者:搜寻专家 更新时间:2023-11-01 00:10:52 24 4
gpt4 key购买 nike

我正在为网络套接字而不是 socket.io 等编写自己的实现。

我的握手很好,但是当客户端向服务器发送数据时,我无法弄清楚如何将该数据转化为有用的东西。它是一个对象吗?它是一个字符串吗?文档说它是 V8 堆外的原始内存位置数组。 ...?

enter image description here

功能示例(客户端是硬编码字符串)

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

最佳答案

主要问题是自该教程编写以来,WebSocket 协议(protocol)已经发展了很多。如果您阅读了链接到的规范,第 5.2 节将讨论数据框架。

https://www.rfc-editor.org/rfc/rfc6455#page-28

使您的数据乱码的主要问题是数据在发送时会自动屏蔽,因此您需要处理帧。

这是一个解码示例代码的示例。您需要扩展它以涵盖更大的长度,并处理规范的其他部分。

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());
};

关于node.js - 你如何处理一个基本的 Websocket 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com