This article is reprinted from the WeChat public account "Learn Front-end in Three Minutes", author sisterAn. Please contact the WeChat public account "Learn Front-end in Three Minutes" to reprint this article. introductionFirst, to answer this question, http determines whether the current protocol needs to be upgraded to websocket by judging whether the header contains Connection: Upgrade and Upgrade: websocket. Let's take a look at the WebSocket protocol and its origin. WebSocket OriginBefore WebSocket, if two-way communication between the client and the service is required, it needs to be implemented through HTTP polling. HTTP polling is divided into polling and long polling: Among them, polling means that the browser starts a timer through JavaScript, and then sends requests to the server at fixed intervals to ask the server whether there is any new message. Disadvantages:
Long polling means that when a browser sends a request, the server will wait for a while and reply only when there is a message. This mechanism temporarily solves the real-time problem, but it brings new problems:
Therefore, HTML5 adds the WebSocket protocol, which can establish an unrestricted two-way communication channel between the browser and the server. Why can WebSocket connections achieve full-duplex communication but HTTP connections cannot? In fact, the HTTP protocol is built on the TCP protocol. The TCP protocol itself implements full-duplex communication, but the request-response mechanism of the HTTP protocol limits full-duplex communication. After the WebSocket connection is established, it is actually just a simple provision: Next, we will not use the HTTP protocol for communication, but send data directly to each other. Advantages of WebSocket:
Supports extensions: WebSocket defines extensions, and users can extend the protocol and implement some customized sub-protocols WebSocket ProtocolWebSocket uses a Uniform Resource Identifier (URI) of ws or wss, where wss indicates WebSocket using TLS. ws:// data is not encrypted and is visible to any middleman. wss:// is a WebSocket based on TLS, similar to HTTPS being HTTP based on TLS). The transport security layer encrypts the data at the sender and decrypts it at the receiver. http determines whether it needs to be upgraded to the websocket protocol by checking whether the header contains Connection: Upgrade and Upgrade: websocket. In addition, there are other headers:
When the server agrees to the WebSocket connection, it returns a response code of 101. Test address: https://www.websocket.org/echo.html Once the socket is established, we should listen for events on the socket. There are 4 events in total:
If we want to send a message, we can use socket.send(data)
SummarizeWebSocket uses the uniform resource identifier of ws or wss. It determines whether it needs to be upgraded to the websocket protocol by judging whether the header contains Connection: Upgrade and Upgrade: websocket. In addition, it also contains headers such as Sec-WebSocket-Key and Sec-WebSocket-Version. When the server agrees to the WebSocket connection, it returns a response code of 101. Its API is very simple. method:
event:
refer to: WebSocket: https://www.liaoxuefeng.com/wiki/1022910821149312/1103303693824096 WebSocket: https://zh.javascript.info/websocket You don't know WebSocket: https://juejin.cn/post/6854573221241421838 Source: https://github.com/Advanced-Frontend/Daily-Interview-Question |
<<: Graph Algorithm Series: Shortest Path in Computational Graph
>>: If you still don’t recognize the USB interface after reading this article, come and find me!
On April 20, Ruijie Networks held an online launc...
Tencent Cloud has launched the Double 11 warm-up ...
Preface This article conducts an in-depth analysi...
VMISS is a foreign hosting service provider regis...
Making machines communicate with humans is the be...
With Wi-Fi 6 and 5G convergence, the connectivity...
As technology advances, the need for faster and m...
[51CTO.com original article] With the continuous ...
DiyVM is a Chinese hosting company founded in 200...
TNAHosting's Black Friday promotion includes ...
After learning the DR/BDR selection rules in OSPF...
In the TCP/IP protocol, a connection is usually c...
Among the three major layers of the Internet of T...
For the past few decades, wireless communication ...
[51CTO.com original article] The rapid developmen...