WebSocket
WebSocket Protocol allows to create full duplex communication channels over a single TCP/HTTP connection and enables streams of simple structured messages. It use small and efficient headers to describe those messages which offer a better solution when it come to send small data at higher rate. In addition, WebSocket Protocol is more network-friendly since it use the default HTTP ports.
Connection Upgrade
First, the relationship between WebSocket and HTTP happen only at the very beginning of the protocol. To open a WebSocket, a client must first establish a HTTP Connection to the remote server in order to perform the WebSocket opening handshake. Once it done, the client send a request to upgrade the actual connection to WebSocket. The HTTP request include Connection, Upgrade and some new HTTP Header that has been standardized for the purpose of the WebSocket opening Handshake. Then, if the server accept the Upgrade request, the server will send back a response with the 101 Switching Protocols status code. Otherwise, it will response an error code.
Message exchangesOnce the handshake is performed and successful, the connection has upgraded to the WebSocket Protocol and the relationship with the HTTP protocol end. At this point, both Client and Server have agreed to switch the protocol of this connection to WebSocket and cannot switch back to HTTP.
In the WebSocket Protocol, data are exchanged using sequences of frame to form WebSocket Messages. A message can transport either protocol-level signaling (Control Messages) or information (Data Messages). It can be summarized by its Type, Payload Length and Contents. WebSocket Messages can have 5 differents Types.
Control Message | Data Message |
---|---|
Close | Text |
Ping | Binary |
Pong |
Control messages payload length cannot exceed 125 bytes and they cannot be fragmented (only one frame per message). Data message length can vary from 0 to 2^64 bytes and can be fragmented. A WebSocket message can always be sent into a single frame, but can be fragmented into multiple frames if it's a Data message.
µC/HTTP-server
µC/HTTP-server doesn't support WebSockets yet.
µC/HTTP-client
µC/HTTP-client supports WebSockets.
See Section WebSocket Configuration to enable the WebSocket feature.
See section WebSocket for examples.