Mrg Gek
Mrg Gek

Reputation: 936

Questions about websocket framing

According to the RFC 6455 specification about websocket's. Data frame structure is follows:

                          frame-fin           ; 1 bit in length
                          frame-rsv1          ; 1 bit in length
                          frame-rsv2          ; 1 bit in length
                          frame-rsv3          ; 1 bit in length
                          frame-opcode        ; 4 bits in length
                          frame-masked        ; 1 bit in length
                          frame-payload-length   ; either 7, 7+16,
                                                 ; or 7+64 bits in
                                                 ; length
                          [ frame-masking-key ]  ; 32 bits in length
                          frame-payload-data     ; n*8 bits in
                                                 ; length, where
                                                 ; n >= 0

So the minimum length of byte array to hold a frame would be 224 bytes (56 bits)? As I read on internet to represent a bit in byte array we need 4 bytes (1000).

How do I mask data? And what data should I mask? Only frame-payload-data or all the frame except the mask key?

Upvotes: 0

Views: 344

Answers (1)

ottomeister
ottomeister

Reputation: 5828

The frame-masking-key field is only present when the frame is masked, which is only done for frames sent by a client to a server. And the frame-payload-data is optional; a frame may be empty, containing no data. Therefore the minimum length of a frame in the client-to-server direction is (1+1+1+1+4+1+7+32)=48 bits or 6 bytes, and the minimum length of a frame in the server-to-client direction is (1+1+1+1+4+1+7)=16 bits or 2 bytes.

Those would be frames that carry no payload. Obviously frames that carry payload data will require additional space.

As I read on internet to represent a bit in byte array we need 4 bytes (1000).

Umm, no, each byte holds 8 bits. It might be convenient within a program to use larger data units to represent bit values, but that is completely independent of the format that is used in the actual frame.

How do I mask data? And what data should I mask? Only frame-payload-data or all the frame except the mask key?

You mask by XOR-ing the frame-masking-key over the frame-payload-data. This is described in section 5.3 of RFC 6455.

Upvotes: 2

Related Questions