search
HomeCommon ProblemThe difference between websocket and http

The difference between websocket and http

Dec 07, 2020 am 10:24 AM
httpwebsocket

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.