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!
80VPS is a long-established Chinese hosting compa...
resize2fs is a command used to expand or shrink t...
For the 802.11ax wireless LAN standard, which is ...
Ever since Kevin Kelly predicted the bright prosp...
The global software-defined networking market has...
The latest data released by the Ministry of Indus...
The rapid development of information technology h...
A sudden encounter with the pneumonia epidemic re...
The tribe has shared information about HostYun ma...
On February 14, Japanese operator Rakuten Mobile ...
Recently, Huawei's 5G+XR Cloud Industry Summi...
At 14:30 on July 31, 2020, the Kunpeng Applicatio...
Friendhosting has launched a 30% discounts for Va...
【51CTO.com Quick Translation】Last week, AT&T ...
[[339299]] This article is reprinted from the WeC...