Suppose I am creating a websocket client. And a specific websocket url returns frame as 'Binary Frame (Opcode 2)' .The questions are
1. Why would the developer want to wrap the original message inside a binary opcode frame?
2. Is retrieving the message implementation centric? In another way, does the the client has to know the same logic that was used to encode at the server?
3. If the above is false then is there a global way to decode/parse the binary opcode to see the actual data that is being sent?
A Websocket Frames basically look like this:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
FIN
FIN bit tells you if this is all the Data you will receive. Large Websocket messages can be fragmented (Send via multiple frames)RSV1-3
opcode
Pong and the Ping Data to validate its reachabilityMASK
MASK bit is setFIN bit is setMASK bit is setFIN bit
& it with 127, the result is your FIN bitOpCode (The OpCode tells you what type of frame you have)
& it with 15, the result is your opcodeMASK Bit
& it with 127, if it is 127 you have a masking keyMASK bit is set the payload needs to be masked
xor operation on every byte with the masking key on index count modulo 4int count = 0;
for (int i = dataIndex; i < totalLength; i++)
{
frameData[i] = (byte)(frameData[i] ^ key[count % 4]);
count++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With