【发布时间】:2015-11-09 10:06:15
【问题描述】:
客户端上的 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 我也看不到数据?
我试图关注并测试这个问题的答案How can I send and receive WebSocket messages on the server side?,但我无法让它工作,通过这个答案我得到了一个像这样的数组 [false, true, false, false, true, true, false] 等等...我真的不知道该怎么处理它.. :\
所以我有点迷茫,从客户端获取数据后我到底应该怎么做才能得到真正的消息?
我使用的是原始客户端和 node.js API,没有任何库。
【问题讨论】:
-
I'm using the original client side and node.js API without any library.为什么不直接使用socket.io库? -
@pkyeck 因为我想了解 websockets 是如何工作的 :),我对 socket.io 没有任何问题,我已经使用了它,它很酷,但我是那种想要的人更深入:)
标签: node.js parsing websocket server-side