Home >Common Problem >The difference between websocket and http

The difference between websocket and http

藏色散人
藏色散人Original
2020-12-07 10:24:1956395browse

The difference between websocket and http: 1. WebSocket is a two-way communication protocol, which simulates the Socket protocol and 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 connected, while http is where the browser initiates a connection to the server.

The difference between websocket and http

#The operating environment of this article: Windows 7 system, Dell G3 computer.

Recommended: "Programming Video"

1. What is WebSocket?

WebSocket is a protocol proposed by the HTML5 specification; currently, except for Wanduzi's IE browser, other browsers basically support it. It is a protocol that never changes without its roots, and is also based on the TCP protocol; it coexists with the HTTP protocol.

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

HTML5 Web Sockets efficiently provide Web connections with minimal overhead. Compared with the old polling or long polling (Comet) that often needs to push real-time data to the client or even maintain two HTTP connections to simulate a full-duplex connection, this greatly reduces unnecessary Network traffic and latency.

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 that represents the remote endpoint you want to connect to.

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

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

2. The relationship between WebSocket and Socket

Socket is not actually a protocol, but a layer abstracted for the convenience of using TCP or UDP. It is located between the application layer and the transmission control layer. a set of interfaces between.

"Socket is an intermediate software abstraction layer that communicates between the application layer and the TCP/IP protocol family. It is a set of interfaces that provide a set of APIs for calling the TCP/IP protocol.

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

When two hosts communicate, they must connect through Socket, and Socket uses the TCP/IP protocol to establish a TCP connection. TCP connections rely more on the underlying IP protocol, while IP protocol connections rely on lower layers such as the link layer.

WebSocket is a typical application layer protocol just like HTTP.

The difference between websocket and http

Summary:

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

3. The relationship between WebSocket and HTTP

Relationship diagram:

The difference between websocket and http

Same points

Both are based on TCP , are all reliable transmission protocols.

are all application layer protocols.

Difference

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

WebSocket requires a handshake between the browser and the server to establish a connection. In http, the browser initiates a connection to the server, and the server does not know this connection in advance.

Contact

When WebSocket establishes a handshake, data is transmitted over HTTP. But after establishment, the HTTP protocol is not required during actual transmission.

Summary:

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

The process of WebSocket connection is:

First, the client initiates an http request, and after three handshakes, a 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 the server receives the client’s handshake request, it also uses the HTTP protocol to feedback data;

Finally, the client receives the message that the connection is successful. Finally, full-duplex communication begins via the TCP transport channel.

The above is the detailed content of The difference between websocket and http. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn