java - how to work out payload size from html5 websocket -


how can tell html5 message payload length on websockets?

im aware base protocol consists of op code, length determine next 1 8 bytes, following values hashing , rest payload!

im creating java serverside application receivea message html5 client , handle, ii can handle messages 256 bytes.

any work out manually great (i.e how habdle bytes determine payload)

cheers

there 3 ways how payload length of websocket frame can encoded, depend on length of payload:

  1. 125 or less
  2. 126-65535
  3. 65536 +

which way used can told looking @ value of 7 last bits of 2nd byte (the first bit of 2nd byte masking flag).

when it's below 126, it's payload length. when it's 126, payload length in following 2 bytes. when it's 127, payload length in following 8 bytes.

so algorithm have follow interpret websocket frame is:

  1. read 1st byte ("byte0") fin flag , opcode
  2. read 2nd byte ("byte1")
  3. when byte1 greater 127, subtract 127 byte1 , set masked true
  4. when byte1 126, read next 2 bytes , convert short. payload_length.
  5. when byte1 127, read next 8 bytes , convert long. payload_length.
  6. when byte1 else, payload_length equal value of byte1
  7. when masked true, read next 4 byte. masking_key.
  8. read number of bytes have in payload_length payload.
  9. when masked true, apply masking_key payload.

there no hash-code in websocket frame, must have mixed up. integrity of websocket frame ensured tcp layer beneath it.

for more information websocket protocol, refer rfc 6455.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -