Node.js WebSocket server does not receive correct data from Chrome
I must be missing something extremely obvious but I can't get this work properly.
The handshaking is going correctly, but as soon as I send a piece of data, I don't get the correct data at the server.
Server:
stream.on("data", function(data) {
if(!handshake) return doHandshake(); // no problems with handshake
console.log(d开发者_开发知识库ata);
});
Client:
ws = new WebSocket("ws://localhost:12345");
ws.onopen = function() {
ws.send(String.fromCharCode(parseInt("89", 16)));
}
What I see in the node.js console:
<Buffer 81 82 ed 68 ae 67 2f e1>
So the keys are ed 68 ae 67
, and the encoded data is 2f e1
. Using xor decoding the decoded data appears to be c2 89
. A c2
got prepended for some unknown reason - the 89
is correct.
Weird things happen with other characters, too:
ws.send(String.fromCharCode(parseInt("ab", 16)));
I get:
<Buffer 81 82 ff 8e 45 34 3d 25>
The decoded data is c2 ab
instead of ab
.
I'm using the new framing format (Chrome 15) and the Windows version of node (node.exe
).
- What is going wrong here?
- Is it possible to see what Chrome sends, so as to see where the problem lies?
Try with Chrome 13 (current stable channel).
Chrome 14+ uses a newer version of the Web Sockets spec which may not be implemented in your version node websocket server.
For more info, see the old version of the spec and http://chromestatus.com.
Furthermore, current versions of Chrome, even the ones that implement the new spec (Chrome 14 and 15 at this time) do not allow sending binary data.
It turned out that it is converting everything to UTF-8.
According to Wikipedia, everything between 127 and 2047 (base 10) will be encoded as two bytes:
110bbbaa 10aaaaaa
For e.g. 89
:
base 16 - 89
base 10 - 137
base 2 - 10001001 so bbb = 000, aaaaaaaa = 10001001
It's more than 127 in base 10. It will thus be encoded as:
110bbbaa 10aaaaaa
11000010 10001001
which is in base 16:
c2 89
Frustrating it was, but at least I now know where the problem lies...
I'm using Chrome 14.0.835.186 on MacOS X. Just had an issue with my app breaking due to WebSockets changes in Chrome.
I switched to: https://github.com/Worlize/WebSocket-Node
As the author says:
WARNING: This is a library implementing only the most recent draft of the WebSocket protocol. It will not work with production browsers until new versions are released that support it.
It's an internal app, so I have the luxury of forcing people to use Chrome 14, but there's a hack to support other drafts https://gist.github.com/1219165. I also use plain-text only.
精彩评论