Decode a websocket frame

前端 未结 2 1360
广开言路
广开言路 2021-02-09 12:30

I am trying to decode a websocket frame, but I\'m not successful when it comes to decoding the extended payload. Here what I did achieve so far:

char *in = data;         


        
2条回答
  •  迷失自我
    2021-02-09 13:10

    If packet_length is 126, the following 2 bytes give the length of data to be read.
    If packet_length is 127, the following 8 bytes give the length of data to be read.
    The mask is contained in the following 4 bytes (after the length).
    The message to be decoded follows this.

    The data framing section of the spec has a useful illustration of this.

    If you re-order your code to something like

    • Read packet_length
    • Check for packet_length of 126 or 127. Reassign packet_length to value of following 2/4 bytes if required.
    • Read mask (the 4 bytes after packet_length, including any additional 2 or 8 bytes read for the step above).
    • Decode message (everything after the mask).

    then things should work.

提交回复
热议问题