-
How can I send and receive messages on the server side using WebSocket, as per the protocol?
-
Why do I get seemingly random bytes at the server when I send data from the browser to the server? It the data encoded somehow?
-
How does the framing work in both the server → client and client → server directions?
How can I send and receive messages on the server side using WebSocket, as
Share
Note: This is some explanation and pseudocode as to how to implement a very trivial server that can handle incoming and outcoming WebSocket messages as per the definitive framing format. It does not include the handshaking process. Furthermore, this answer has been made for educational purposes; it is not a full-featured implementation.
Specification (RFC 6455)
Sending messages
(In other words, server → browser)
The frames you’re sending need to be formatted according to the WebSocket framing format. For sending messages, this format is as follows:
The first byte will be
1000 0001(or129) for a text frame.The second byte has its first bit set to
0because we’re not encoding the data (encoding from server to client is not mandatory).It is necessary to determine the length of the raw data so as to send the length bytes correctly:
0 <= length <= 125, you don’t need additional bytes126 <= length <= 65535, you need two additional bytes and the second byte is126length >= 65536, you need eight additional bytes, and the second byte is127The length has to be sliced into separate bytes, which means you’ll need to bit-shift to the right (with an amount of eight bits), and then only retain the last eight bits by doing
AND 1111 1111(which is255).After the length byte(s) comes the raw data.
This leads to the following pseudocode:
Receiving messages
(In other words, browser → server)
The frames you obtain are in the following format:
The first byte usually does not matter – if you’re just sending text you are only using the text type. It will be
1000 0001(or129) in that case.The second byte and the additional two or eight bytes need some parsing, because you need to know how many bytes are used for the length (you need to know where the real data starts). The length itself is usually not necessary since you have the data already.
The first bit of the second byte is always
1which means the data is masked (= encoded). Messages from the client to the server are always masked. You need to remove that first bit by doingsecondByte AND 0111 1111. There are two cases in which the resulting byte does not represent the length because it did not fit in the second byte:0111 1110, or126, means the following two bytes are used for the length0111 1111, or127, means the following eight bytes are used for the lengthThe four mask bytes are used for decoding the actual data that has been sent. The algorithm for decoding is as follows:
where
encodedByteis the original byte in the data,encodedByteIndexis the index (offset) of the byte counting from the first byte of the real data, which has index0.masksis an array containing of the four mask bytes.This leads to the following pseudocode for decoding: