Since Chrome updated to v14, they went from version three of the draft to version eight of the draft.
I have an internal chat application running on WebSocket, and although I’ve gotten the new handshake working, the data framing apparently has changed as well. My WebSocket server is based on Nugget.
Does anybody have WebSocket working with version eight of the draft and have an example on how to frame the data being sent over the wire?
(See also: How can I send and receive WebSocket messages on the server side?)
It’s fairly easy, but it’s important to understand the format.
The first byte is almost always
1000 0001, where the1means "last frame", the three0s are reserved bits without any meaning so far and the0001means that it’s a text frame (which Chrome sends with thews.send()method).(Update: Chrome can now also send binary frames with an
ArrayBuffer. The last four bits of the first byte will be0002, so you can differ between text and binary data. The decoding of the data works exactly the same way.)The second byte contains of a
1(meaning that it’s "masked" (encoded)) followed by seven bits which represent the frame size. If it’s between000 0000and111 1101, that’s the size. If it’s111 1110, the following 2 bytes are the length (because it wouldn’t fit in seven bits), and if it’s111 1111, the following 8 bytes are the length (if it wouldn’t fit in two bytes either).Following that are four bytes which are the "masks" which you need to decode the frame data. This is done using xor encoding which uses one of the masks as defined by
indexOfByteInData mod 4of the data. Decoding simply works likeencodedByte xor maskByte(wheremaskByteisindexOfByteInData mod 4).Now I must say I’m not experienced with C# at all, but this is some pseudocode (some JavaScript accent I’m afraid):
You can also download the specification which can be helpful (it of course contains everything you need to understand the format).