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

Websocket and http.

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

Share

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

This article is about websocket and http. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

The difference between websocket and http: 1. WebSocket is a two-way communication protocol, which simulates Socket protocol, which can send or receive information in both directions, while HTTP is one-way; 2. WebSocket requires a handshake between the browser and the server to establish a connection, while http is the connection initiated by the browser to the server.

What is WebSocket?

WebSocket is a protocol proposed by the HTML5 specification; at present, all browsers basically support it except the IE browser. It is a kind of agreement, which remains the same, and is also based on the TCP agreement; it and the HTTP agreement are two kinds of protocols that coexist.

WebSocket is a protocol in HTML5. The HTML5 Web Sockets specification defines Web Sockets API, which supports full-duplex communication between pages and remote hosts using Web Socket protocol. It introduces the WebSocket interface and defines a full-duplex communication channel that operates on the Web through a single socket.

HTML5 Web Sockets provides efficient Web connectivity with minimal overhead. This greatly reduces unnecessary network traffic and latency compared to the old polling or long polling (Comet) that often requires pushing real-time data to the client or even by maintaining two HTTP connections to simulate a full-duplex connection.

To use HTML5 Web Sockets to connect from a Web client to a remote endpoint, you create a new WebSocket instance and provide it with a URL to represent the remote endpoint you want to connect to.

The specification defines ws:// and wss:// schemas to represent WebSocket and secure WebSocket connections, which is similar to the difference between http:// and https://.

A WebSocket connection is established by upgrading it to WebSocket during the initial handshake phase of the HTTP protocol between the client and the server, and the underlying layer is still a TCP/IP connection.

2. The relationship between WebSocket and Socket

Socket is not really a protocol, but a layer abstracted to facilitate the use of TCP or UDP. It is a set of interfaces between the application layer and the transport control layer.

Socket is the intermediate software abstraction layer for the communication between the application layer and the TCP/IP protocol family. It is a set of interfaces that provide a set of API that invokes the TCP/IP protocol.

In the design pattern, Socket is actually a facade pattern, which hides the complex TCP/IP protocol family behind the Socket interface. For users, a simple set of interfaces is all, allowing Socket to organize the data to comply with the specified protocol. "

When two hosts communicate, they must connect through Socket, and Socket uses TCP/IP protocol to establish TCP connection. The TCP connection depends more on the underlying IP protocol, while the connection of the IP protocol depends on lower levels such as the link layer.

WebSocket, like HTTP, is a typical application layer protocol.

Summary:

Socket is the transmission control layer interface and WebSocket is the application layer protocol.

III. The relationship between WebSocket and HTTP

Diagram:

Identical point

They are all based on TCP, and they are all reliable transport protocols.

They are all application layer protocols.

Difference

WebSocket is a two-way communication protocol that simulates the Socket protocol and can send or receive messages in both directions. The HTTP is unidirectional.

WebSocket requires the browser to shake hands with the server to establish a connection. Http, on the other hand, is a connection initiated by the browser to the server, which the server does not know in advance.

contact

When WebSocket establishes a handshake, the data is transmitted through HTTP. But after it is established, the HTTP protocol is not needed for real transmission.

Summary:

In WebSocket, only the server and browser need to shake hands through HTTP protocol, and then establish a separate communication channel of TCP for data transmission.

The process of WebSocket connection is:

First of all, the client initiates a http request, and after three handshakes, the TCP connection is established. The http request stores information such as the version number supported by WebSocket, such as Upgrade, Connection, WebSocket-Version, etc.

Then, after receiving the handshake request from the client, the server also uses HTTP protocol to give back the data.

Finally, after receiving the message that the connection is successful, the client starts full-duplex communication with the help of the TCP transmission channel.

Thank you for reading! So much for sharing about websocket and http. I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

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