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!
Recently, with the improvement of this round of e...
[[356210]] This article is reprinted from WeChat ...
The three major operators have already commercial...
iWebFusion (iWFHosting) is still selling Clearanc...
Hello everyone, I am Bernie, an IT pre-sales engi...
HostKvm is a Hong Kong VPS provider founded in 20...
I searched the blog and found that the earliest i...
"The first year of 5G" has become synon...
Wang Zhiqin, deputy director of the China Academy...
[51CTO.com original article] Although the COVID-1...
During the Christmas and New Year holidays, we ha...
Wireless AP is the access point for users to ente...
[51CTO.com original article] On April 14, 2017, W...
Edge computing is one of the most exciting new co...
Song Ge has been serializing gRPC with his friend...