define

WebSocket Protocol is a new protocol for HTML5. It implements full-duplex communication between the browser and the server. The initial handshake needs to be done with an HTTP request.

The principle of

WebSocket is also an application-layer protocol like HTTP, but it is a two-way communication protocol built on TOP of TCP.

background

HTTP can only be initiated by the client. If the server status changes continuously, only “polling” can be used, which is inefficient and wasteful of resources.

Compare the HTTP

The same

  • TCP – based application – layer transport protocol

The difference between

  • WebSocket is a two-way communication protocol that simulates the Socket protocol and can send or receive information in both directions. HTTP is one-way

The characteristics of

  • Based on TCP protocol, the implementation of the server side is relatively easy.

  • It has good compatibility with HTTP protocol. The default ports are also 80 and 443, and the handshake phase uses HTTP protocol, so it is not easy to mask the handshake and can pass various HTTP proxy servers.

  • The data format is relatively light, with low performance overhead and high communication efficiency.

  • You can send text or binary data.

  • There are no same-origin restrictions, and clients can communicate with any server.

  • The protocol identifier is WS (or WSS if encrypted), and the server URL is the URL.

The connection process

  • The browser and server establish a TCP connection and shake hands for three times. This is the basis of communication, the transport control layer, and if it fails, it doesn’t execute.
  • After the TCP connection succeeds, the browser sends information such as the version number supported by WebSocket to the server through HTTP. (HTTP handshake before starting)
  • After receiving the handshake request from the client, the server also uses HTTP to send back data.
  • After receiving a successful connection message, communication is transmitted through the TCP channel.

The Socket is defined

Socket is the intermediate software abstraction layer of communication between application layer and TCP/IP protocol family. It is a group of interfaces. In the design mode, Socket is actually a facade mode, it hides the complex TCP/IP protocol family behind the Socket interface, for the user, a simple set of interfaces is all, let the Socket to organize data to conform to the specified protocol.

Socket is different from WebSocket

  • Socket is not a protocol, but a layer abstracted to facilitate the use of TCP or UDP. It is a group of interfaces between the application layer and the transmission control layer.
  • WebSocket is a typical application layer protocol.

Sticking and unpacking problems

why

  • Packet unpacking occurs when the data to be sent is larger than the remaining space of the TCP send buffer.

  • If the data to be sent is larger than MSS (maximum packet length), TCP unpacks the data before transmission.

  • The size of the data to be sent is smaller than the size of the TCP send buffer. TCP sends the data that has been written into the buffer for several times. Packet sticking occurs.

  • The application layer at the receiving end does not read the data in the receiving buffer in time, and sticky packets occur.

  • . .

how

  • The sender adds a header to each packet, and the header should contain at least the length of the packet. In this way, after receiving data, the receiver can know the actual length of each packet by reading the length field in the header.

  • The sender encapsulates each packet into a fixed length (if not, it can be filled by padding zeros), so that the receiver automatically splits each packet by reading a fixed length of data from the receive buffer.

  • A boundary can be set between packets, such as a special symbol, so that the receiver can separate different packets through this boundary.

  • . .