Learning webSoket (1) Understanding webSoket

WebSocket  is a network communication protocol that is required for many advanced functions

1. Why do you need WebSocket?

The answer is simple, because the HTTP protocol has a flaw: communication can only be initiated by the client.

For example, if we want to know today's weather, the client can only make a request to the server, and the server returns the query results. The HTTP protocol cannot allow the server to actively push information to the client.

The characteristics of this one-way request are destined to make it very troublesome for the client to know if the server has continuous status changes. We can only use "polling" : every once in a while, a query is issued to find out whether the server has new information. The most typical scenario is a chat room.

Polling is inefficient and wastes resources (because you must keep connecting, or the HTTP connection is always open). Therefore, engineers have been thinking about whether there is a better way. This is how WebSocket was invented.

In short, in order for the server to actively send messages to the client, weSoket needs to be used.

Features: The server can actively push information to the client, and the client can also actively send information to the server. It is a true two-way equal dialogue and is a type of server push technology .

Other features include:

(1) Based on the TCP protocol, server-side implementation is relatively easy.

(2) It has good compatibility with HTTP protocol. The default ports are also 80 and 443, and the HTTP protocol is used in the handshake phase, so it is not easy to block during the handshake and can pass various HTTP proxy servers.

(3) The data format is relatively lightweight, has low performance overhead, and efficient communication.

(4) You can send text or binary data.

(5) There is no origin restriction and the client can communicate with any server.

(6) The protocol identifier is ws(or if encrypted wss) the server address is the URL.

 

Guess you like

Origin blog.csdn.net/ljy_1024/article/details/121740900