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

Analysis of the implementation principle of WebSocket Protocol in HTML5

2025-02-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

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

Internet Technology

Wechat

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

12
Report