The problem is that the length does not always fit in 7 bits (you can only express the numbers 0 to 127 with 7 bits), and in that case either the following 2 or 8 bytes will be used to make the length fit:
- 126 means the following 2 bytes are used for the length
- 127 means the following 8 bytes are used for the length
So the payload starts at either index 2, 4 or 10, if not encoded. When encoded, it starts at either 6, 8 or 14 (because there are 4 mask bytes).
I previously posted some pseudocode about decoding the payload data.
To actually get the length as a "real number" (instead of separate bytes), you can use bitwise shift operators as follows (in case there are two bytes for the length):
var length = (bytes[2] << 8) | (bytes[3] << 0);
This will calculate it like this:
Suppose:
bytes[2]
is 01101001
(105
in base 10)
bytes[3]
is 10100101
(165
in base 10)
Then <<
will be doing:
01101001 00000000 // moved 8 places to the left, filled with zeroes
10100101 // moved 0 places (nothing really happening, you can eliminate '<< 0')
|
is basically adding them:
01101001 00000000
10100101
----------------- |
01101001 10100101 (in base 10 that's 27045)
So if you have the bytes 105
and 165
, then they represent a length of 27045
.