Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the communication mode of WebSocket?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge about what the communication mode of WebSocket is. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

The traditional HTTP is based on the request response model, so the client and server can not achieve real-time communication. There are also technologies that imitate real-time communication before, but from polling, long polling to Comet can not completely solve the problem of real-time communication. The emergence of WebSocket changes this situation, greatly reducing the number of useless message headers in communication greatly improves the communication efficiency, but also provides a very low delay.

Note that WebSocket is not without latency, but is relatively stable and has a short latency. In the actual engineering application, the delay of WebSocket is random, depending on the network situation. )

The header of the WebSocket request and response is given below. The request code from the client to the server is as follows:

From client to server:

GET / demo HTTP/1.1

Host: example.com

Connection: Upgrade

Sec-WebSocket-Key2: 12998 5 Y3 1 .P00

Sec-WebSocket-Protocol: sample

Upgrade: WebSocket

Sec-WebSocket-Key1: 4: 1 46546xW%0l 1 5

Origin: HTTP://example.com

[8-byte security key]

The response code from the server to the client is as follows:

From server to client:

HTTP/1.1 101 WebSocket Protocol Handshake

Upgrade: WebSocket

Connection: Upgrade

WebSocket-Origin: HTTP://example.com

WebSocket-Location: ws://example.com/demo

WebSocket-Protocol: sample

[16-byte hash response]

After the message header of the WebSocket is constructed, the client must follow the following seven steps to generate communication.

(1) parse the URL address of WebSocket to obtain the host, port, resource name and security mode. If the connection is unsuccessful, an SYNTAX_ERR exception is returned and execution is aborted. )

(2) check whether the port is occupied. If the initiated port is already occupied, a SECURITY_ERR exception is thrown and execution is aborted. In general, ports 80 and 443 are not blocked. )

(3) if the protocol is not supported by the browser, the protocol will be set to an empty array.

(4) if the Unicode code value of any content character in the communication process is less than Ubun0021 or greater than Ubun007E (in other words, there is a space character or any ASCII code that cannot be displayed), an error SYNTAX_ERR exception will be reported and the communication step will be disconnected.

(5) the WebSocket constructor script first turns the source into a serialized ASCII code, and then converts it to the subscript format of the ASCII code.

(6) return a new WebSocket object and continue these actions in the background (do not lock the script).

(7) establish a WebSocket connection to the specified host port that initiates a connection request from the source with security authentication, source host name, protocol list (which may be empty), and compliance with the cookie flag setting.

These are all the contents of the article "what is the communication mode of WebSocket?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report