Search code examples
httpwebsocketutf-8dataformat

How does this data frame example in the Websocket RFC equal “Hello”?


The first example of Section 5.7 of Websocket Protocol RFC 6455 reads:

A single-frame unmasked text message

  • 0x81 0x05 0x48 0x65 0x6c 0x6c 0x6f (contains "Hello")

What are the first two octets in this example and why are they there?

Looking up the UTF-8 codes for the last five octets I can see how those bytes would translate to Hello.


Solution

  • The first two octets in this example contain metadata about the data frame and the payload it contains. The last five octets represent the payload or Hello.

    The first two octets 0x81 0x05 are hexadecimal representations of two bytes or 16 bits of data. In binary they translate to: 1000 0001 0000 0101.

    Using the Data Frame Diagram from the RFC you can see what these bits tell us about the data frame. To summarize:

    • 1st bit (FIN): indicates this is the final fragment of the message being sent
    • 5th-8th bits (opcode): the payload is a text message in UTF-8
    • 9th-16th bits (payload length): the length of the payload is 5