What is the WebSocket?

WebSocket is a protocol in HTML5 that supports persistent continuities. HTTP does not support persistent connections. Neither Http1.0 nor HTTP1.1 supports persistent links. Keep-alive in HTTP1.1 consolidates multiple HTTP requests into one

What protocol is WebSocket? What are its advantages?

The HTTP lifecycle is defined by a Request, i.e., a Request for a Response. In Http1.0, this HTTP Request ends. Improved in Http1.1, there is a connection: keep-alive, that is, multiple requests can be sent and multiple responses received within an Http connection. However, it must be remembered that in Http, a Request can only have one Response, and this Response is passive and cannot be initiated actively.

WebSocket is based on THE Http protocol, or borrows Http protocol to do part of the handshake, in the handshake phase is the same as Http. Let’s look at the implementation of a Websocket handshake protocol, basically two attributes, upgrade, connection.

The basic requirements are as follows:

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: example.com

The following two attributes are added:

  • Upgrade:webSocket
  • Connection:Upgrade

Tells the server to send a Websocket

  • Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
  • Sec-WebSocket-Protocol: chat, superchat
  • Sec-WebSocket-Version: 13