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!
Chrome is the most popular web browser in the wor...
1. Classification of optical fiber Optical fibers...
Mobile data traffic will grow more than 10 times ...
Research firm Omdia believes that several 5G tren...
[[382042]] This article is reprinted from the WeC...
Increasing speed and reducing fees is Premier Li&...
As an operation and maintenance person, operation...
The Fifth Plenary Session of the 19th CPC Central...
BudgetVM is a foreign hosting company that has be...
[[375841]] When it comes to the world's hotte...
RAKsmart has launched a new month of promotions, ...
On October 12, 2021, during the 2021 China IPv6 I...
In the Wi-Fi world, 6GHz will be a major breakthr...
[[354146]] This article is reprinted from the WeC...
Over the next decade, 5G is expected to become on...