使用 Node.js 解析 WebSocket 数据
客户端上的 Websocket:
socket.send('helloworld');
Node.js 上的 Websocket:
socket.ondata = function(d, start, end){
// I suppose that the start and end indicates the location of the
// actual data 'hello world' after the headers
var data = d.toString('utf8', start, end);
// Then I'm just printing it
console.log(data);
});
但我在终端上收到: �����]���1��/��
:O
我试图理解该文档: https://www.rfc-editor.org/rfc/rfc6455#section-5.2 但很难理解,因为我不知道应该使用什么,我的意思是我看不到数据即使使用toString
?
我尝试遵循并测试这个问题的答案 如何在服务器端发送和接收 WebSocket 消息? 但我无法让它工作,有了这个答案,我得到了一个像这样的数组 [false, true, false, false, true , 真的, false] 等等...我真的不知道该怎么办.. :\
所以我有点困惑,从客户端获取数据以获取真实消息后我到底应该做什么?
我使用的是原始客户端和 Node.js API,没有任何库。
Websocket on Client:
socket.send('helloworld');
Websocket on Node.js:
socket.ondata = function(d, start, end){
// I suppose that the start and end indicates the location of the
// actual data 'hello world' after the headers
var data = d.toString('utf8', start, end);
// Then I'm just printing it
console.log(data);
});
but I'm getting this: �����]���1���/��
on the terminal :O
I have tried to understand this doc: https://www.rfc-editor.org/rfc/rfc6455#section-5.2 but it's hard to understand because I don't know what should I work with, I mean I can't see the data even with toString
?
I have tried to follow and test with this questions answer How can I send and receive WebSocket messages on the server side? but I can't get it to work, with this answer I was getting an array like this [false, true, false, false, true, true, false] etc... and I don't really know what to do with it.. :\
So I'm a bit confused, what the hell should I do after I get the data from the client side to get the real message?
I'm using the original client side and node.js API without any library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实上,您可以从 Node 的 HTTP API 开始。这正是我在编写 WebSocket-Node 模块时所做的 https://github.com/Worlize/WebSocket- Node
如果您不想使用现有的 WebSocket 库(尽管您确实应该只使用现有的库),那么您需要能够解析 RFC 定义的二进制数据格式。格式以及如何解释数据非常清楚。从每一帧中,您必须读入所有标志,解释帧大小,可能读取屏蔽键,并在从线路读取内容时取消屏蔽内容。
这是您看不到任何可识别内容的原因之一……在 WebSocket 中,通过使用 XOR 对内容应用随机掩码来混淆所有客户端到服务器的通信,作为安全预防措施,防止可能毒害旧代理服务器的缓存不知道网络套接字。
You can in fact start with Node's HTTP API. That is exactly what I did when writing the WebSocket-Node module https://github.com/Worlize/WebSocket-Node
If you don't want to use an existing WebSocket Library (though you really should just use an existing library) then you need to be able to parse the binary data format defined by the RFC. It's very clear about the format and exactly how to interpret the data. From each frame you have to read in all the flags, interpret the frame size, possibly read the masking key, and unmask the contents as you read them from the wire.
That is one reason you're not seeing anything recognizable... in WebSockets, all client-to-server communications is obfuscated by applying a random mask to the contents using XOR as a security precaution against possibly poisoning the cache of older proxy servers that don't know about websockets.
您使用的是哪个 Node.js 库?从您正在挂钩的 socket.ondata 来看,它看起来像 HTTP 服务器 API。 WebSocket 不是 HTTP。它具有 HTTP 兼容的握手,因此 WebSocket 和 HTTP 服务可以驻留在同一端口上,但相似之处仅此而已。握手后,WebSockets 是一种成帧的全双工、长寿命消息传输,与常规 TCP 套接字比 HTTP 更相似。
如果您想在 node.js 中实现自己的 WebSocket 服务器,您将需要直接使用套接字库(或构建/借用现有的 WebSocket 服务器代码)。
这是一个基于 Node.js 的 WebSocket 服务器,它从 WebSocket 桥接到 TCP 套接字: https://github.com/kanaka/websockify/blob/master/other/websockify.js 请注意,这是针对以前的 Hixie 版本的协议(我还没有机会或动力去更新它) 然而)。该协议的现代 HyBI 版本非常不同,但您也许能够从该实现中收集一些有用的信息。
Which node.js library are you using? Judging by the fact that you are hooking socket.ondata that looks like the HTTP server API. WebSockets is not HTTP. It has an HTTP compatible handshake so that the WebSocket and HTTP service can live on the same port, but that's where the similarity ends. After the handshake, WebSockets is a framed full-duplex, long-lived message transport more similar to regular TCP sockets than to HTTP.
If you want to implement your own WebSocket server in node.js you are going to want to use the socket library directly (or build on/borrow existing WebSocket server code).
Here is a Node.js based WebSocket server that bridges from WebSocket to TCP sockets: https://github.com/kanaka/websockify/blob/master/other/websockify.js Note that it is for the previous Hixie version of the protocol (I haven't had opportunity or motivation to update it yet). The modern HyBI version of the protocol is very different but you might be able to glean some useful information from that implementation.
有两件事 -
您使用的是哪个 Node.js 版本?我从未见过带有 start 和 endpt 的数据事件。发出的事件只是以缓冲区/字符串作为参数的数据。
更重要的是,如果您连接到 HTTP 套接字,则应该注意 HTTP 数据包。它包含听众、正文和预告片。那里可能有垃圾。
There are two things -
Which node.js version are you using? I have never seen a data event with start and endpt. The emitted event is just data with buffer/string as an argument.
More importantly, if you are hooking on to the HTTP socket you should take care of the HTTP Packet. It contains hearders, body and a trailer. There might be garbage in there.
这是这篇文章的解决方案:
https:// medium.com/hackernoon/implementing-a-websocket-server-with-node-js-d9b78ec5ffa8
Here is a solution from this post:
https://medium.com/hackernoon/implementing-a-websocket-server-with-node-js-d9b78ec5ffa8