Home > Web Front-end > JS Tutorial > What is the difference between websocket and ajax

What is the difference between websocket and ajax

WBOY
Release: 2021-12-28 11:29:56
Original
3331 people have browsed it

Difference: 1. Ajax is a development technology for creating interactive applications, and websocket is a protocol used to achieve full-duplex communication between the browser and the server; 2. Ajax establishes a short connection, and websocket establishes a long link; 3. Ajax is used for non-real-time data interaction, and websocket is used for real-time data interaction.

What is the difference between websocket and ajax

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is the difference between websocket and ajax

1. Essentially different

Ajax, that is, asynchronous JavaScript and XML, is A web development technology for creating interactive web applications;

WebSocket is a new protocol in HTML5 that realizes full-duplex communication between the browser and the server. Its essence is to first perform a handshake through the HTTP/HTTPS protocol and then create a TCP connection for exchanging data. The server and the client communicate in real time through this TCP connection.

2. The life cycle is different.

Websocket establishes a long connection and maintains the connection during a session; while ajax is a short connection, and the connection will be disconnected after the data is sent and received.

3. Different scope of application

Websocket is generally used for front-end and front-end real-time data interaction, while ajax front-end and front-end non-real-time data interaction.

4. Different initiators

Ajax technology requires the client to initiate a request (the user can see the data returned by his own request), while the WebSocket server and client can push information to each other. (The things returned by user A’s request can be viewed by user A and also by user B; if it is public, everyone can view it

5. The usage is different

Ajax

$.ajax({
type:"post",
url:"http://localhost:8080/target",
data:"state = yes",
dataType:"json",
success:funciont(data){
}
});
Copy after login

websocket

var monitor = new WebSocket("ws://"+ip+path)
onOpen()、onMessage()、onClose()
Copy after login

[Related tutorial recommendations: AJAX video tutorial]

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template