Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Analysis of the implementation principle of WebSocket Protocol in HTML5

Shulou Source: shulou.com Published: 2022-05-31 12:48:25 09月24日 Update

This article mainly introduces the relevant knowledge of "Analysis of the implementation principle of WebSocket Protocol in HTML5". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "Analysis of the implementation principle of WebSocket Protocol in HTML5" can help you solve the problem.

The purpose of the WebSocket protocol is to work on the existing network infrastructure. As part of this design principle, the protocol specification for WebSocket connections defines a HTTP connection as its initial life cycle, thus ensuring its full backward compatibility with the pre-WebSocket world. Generally speaking, switching WebSocket from HTTP protocol is called WebSocket handshake.

The browser sends a request to the server, which indicates that it wants to switch from the HTTP WebSocket protocol. The wish expressed by the client by upgrading the title, the code is as follows:

The code is as follows:

GET ws://echo.websocket.org/?encoding=text HTTP/1.1

Origin: http://websocket.org

Cookie: _ _ utma=99as

Connection: Upgrade

Host: echo.websocket.org

Sec-WebSocket-Key: uRovscZjNol/umbTt5uKmw==

Upgrade: websocket

Sec-WebSocket-Version: 13

If the server understands the WebSocket protocol, it agrees to the protocol switch through the upgrade header.

The code is as follows:

HTTP/1.1 101 WebSocket Protocol Handshake

Date: Fri, 10 Feb 2012 17:38:18 GMT

Connection: Upgrade Server: Kaazing Gateway

Upgrade: WebSocket

Access-Control-Allow-Origin: http://websocket.org

Access-Control-Allow-Credentials: true

Sec-WebSocket-Accept: rLHCkw/SKsO9GAH/ZSFhBATDKrU=

Access-Control-Allow-Headers: content-type

At this point, the HTTP connection breaks down and replaces the WebSocket connection in the same underlying TCP / IP connection. The WebSocket connection uses the same HTTP (80) as the HTTPS port (443), by default.

After the connection is established, WebSocket data frames can be sent back and forth between the client and the server in full-duplex mode. Both text and binary frames can be sent in both directions. There are only two bytes of data minimum framing. For text frames, each frame starts at 0 x00 bytes, ends with 0 xff bytes, and contains utf-8 data. WebSocket text frames use a Terminator, while binary frames use a prefix length.

This is the end of the content about "Analysis of the implementation principle of WebSocket Protocol in HTML5". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Principle analysis code bytes data text server knowledge service two binary customer industry switching upgrade different practical minimum between prefix Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Microsoft Shulou Technology Shulou Tech Info Docker