Websockets can be called a wire-protocol where the smallest piece of communication is called the frame. For security, all frames from client to server are masked (i.e. XOR-encrypted). (Server-to-client frames are NOT masked.) The key used for masking is chosen by the client for each frame.
Data-frames can be Text-frames or Binary-frames. An application-specific websocket-message could be sent over multiple data-frame’s (especially bigger messages).
Control-frames (like Ping-, Pong and Close-frames) are always <= 125 bytes.
Upon handshake (which happens over the http(s)-protocol), the client can negotiate subprotocols (like wamp) and extensions.
Of course, an application developer can choose their own protocol in stead of one of the existing ones.
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
258EAFA5-E914-47DA-95CA-C5AB0DC85B11 is a special UUID used by websocket-servers to prove to the client a handshake was receivedFrame format:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
FIN (1 bit): set when this frame is the end of a message, if 0 the server keeps listening for more parts of the messageRSV1/2/3: only used by extensionsMASK (1 bit): set to indicate the messages sent by the client (client -> server) are masked AKA XOR-encrypted
Masking-key with which it encoded the payloadMasking-key to decode the payloadopcode (4 bits): indicates what kind of frame this is
0x0: continuation frame0x1: text frame0x2: binary frame0x8: connection close frame0x9: ping frame0xA: pong framepayload len (7 bits): enough for when payload < 125
126: more payload length-bits to fetch from extended payload length127: more payload length-bits to fetch from extended payload length continuedExample of message-flow (1 message sent over 4 frames):
Client: FIN=1, opcode=0x1, msg="hello"
Server: (process complete message immediately) Hi.
Client: FIN=0, opcode=0x1, msg="and a"
Server: (listening, new message containing text started)
Client: FIN=0, opcode=0x0, msg="happy new"
Server: (listening, payload concatenated to previous message)
Client: FIN=1, opcode=0x0, msg="year!"
Server: (process complete message) Happy new year to you too!
0x1) and binary-frames (opcode 0x2) can be fragmented0x0 means this payload should be added to the previous one to complete the message0x0 with FIN=1 means the message is complete and can be processed
pings and pongs
1250x8-control frameRequest-URIHost indicates servers authority (important for Same-Origin Policy / cross-site scripting) (RFC6454)Origin (only required if coming from a browser client)Connection: UpgradeSec-WebSocket-Key: ... (base64-encoded 16-byte value)Sec-WebSocket-Version: 13Sec-WebSocket-Protocol: comma-separated list of Subprotocols (they structure the websocket-payload (e.g. wamp)) the client wishes to speak
Sec-WebSocket-Extensions: which protocol-level extensions (that modify the websocket-payload) the client wishes to speak101: switching protocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: a computed field:
Sec-WebSocket-Key from client with the fixed value 258EAFA5-E914-47DA-95CA-C5AB0DC85B11Sec-WebSocket-ProtocolSec-WebSocket-Extensions (multiple extensions can be used)ws(s)) with specific grammar
#foo)https://www.iana.org/assignments/websocket/websocket.xml
Check this for:
wamp1000: normal closure, 1002: protocol error, …)TextFrame, BinaryFrame, PingFrame, PongFrame, …)