Websocket is a new protocol of HTML5. It does not change HTTP, but uses HTTP to complete part of the handshake, which is the same in the handshake phase.
The connection process
To complete the handshake phase using HTTP, upgrade the protocol to WebSocket:
The client proposes to upgrade the protocol to webSocket in the HTTP request header
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com
Copy the code
Upgrade and Connection tell the server to set up the Websocket protocol. Sec-WebSocket- Defines information such as the WebSocket version.
The server returns the following response header indicating that the request was received and the WebSocket was successfully established
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
Copy the code
Then communicate according to websocket protocol to establish a persistent connection