Introduction to Web Sockets

1. Web Socket is a new protocol of HTML5, which allows the server to send data to the client. It is a full-duplex, two-way communication method, and it can maintain long-term communication.

Concept understanding:

[1] Full-duplex: It means that while the server sends data to the browser, the browser can also transmit data to the server, that is, two-way transmission of information at the same time.

  Expansion: (1) Half-duplex: Allows both parties to communicate to transmit data to each other, but when one party A transmits data to the other party B, B cannot transmit data to A, analogous to a single-plank bridge.

  (2) Simplex: Only one party A is allowed to transmit data to the other party B, and B can never transmit data to A, analogous to a one-way street.

【2】Long-time communication: Analogous to the HTTP protocol, there is one request and one response, and there is no relationship between the two requests unless cookies are added to save information. However, Web Socket only needs one request, and then communicates in the same connection state.

2. Web Socket communication process: first use the HTTP protocol to shake hands to initialize the connection. After the server responds, the connection uses the Upgrade header field to switch the HTTP protocol to the Web Socket protocol. After that, the communication can be maintained in one connection without the need to HTTP protocol, continuous request-response, request-response... [Here is a picture:

 

3. Web Socket features:

【1】Compatible with HTTP protocol

[2] Built on the TCP protocol, using the handshake, it belongs to the same application layer as the HTTP protocol.

[3] The server and client can send a small amount of data, with low performance overhead and efficient communication

[4] No same-origin policy restrictions

[5] The protocol identifiers are ws and wss, the former is an insecure protocol, and the latter is a secure protocol

【6】You can send text or binary data
 

Guess you like

Origin blog.csdn.net/weixin_40205234/article/details/124629440