What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?(什么是长轮询、Websocket、服务器发送事件 (SSE) 和 Comet?)
问题描述
我已经尝试阅读一些文章,但我对这些概念还不是很清楚.
有人愿意向我解释一下这些技术是什么吗:
- 长轮询
- 服务器发送的事件
- 网络套接字
- 彗星
我每次遇到的一件事是,服务器保持连接打开并将数据推送到客户端.连接如何保持打开状态,客户端如何获取推送的数据?(客户端如何使用数据,也许一些代码可能会有所帮助?)
现在,我应该将其中哪一个用于实时应用.我听说过很多关于 websockets(使用 socket.io [一个 node.js 库]),但为什么不是 PHP?
在下面的示例中,客户端是浏览器,服务器是托管网站的网络服务器.
在了解这些技术之前,您必须先了解经典 HTTP 网络流量.
常规 HTTP:
- 客户端向服务器请求网页.
- 服务器计算响应
- 服务器将响应发送给客户端.
Ajax 轮询:
- 客户端使用常规 HTTP 向服务器请求网页(请参阅上面的 HTTP).
- 客户端收到请求的网页,并在页面上执行 JavaScript,以定期(例如 0.5 秒)从服务器请求文件.
- 服务器计算每个响应并将其发回,就像正常的 HTTP 流量一样.
Ajax 长轮询:
- 客户端使用常规 HTTP 向服务器请求网页(请参阅上面的 HTTP).
- 客户端接收到请求的网页,并在页面上执行向服务器请求文件的 JavaScript.
- 服务器不会立即响应请求的信息,而是等到有新的信息可用.
- 当有新信息可用时,服务器会以新信息进行响应.
- 客户端收到新信息后立即向服务器发送另一个请求,重新启动该过程.
HTML5 服务器发送事件(SSE)/事件源:
- 客户端使用常规 HTTP 向服务器请求网页(请参阅上面的 HTTP).
- 客户端收到请求的网页并在页面上执行 JavaScript,从而打开与服务器的连接.
当有新信息可用时,服务器会向客户端发送一个事件.
- 从服务器到客户端的实时流量,大部分是您需要的
- 您需要使用具有事件循环的服务器
- 只有使用正确的 CORS,才能 或 或 这篇文章.<小时><块引用>
现在,我应该将其中哪一个用于实时应用程序(我需要代码).我听说过很多关于 websockets (使用 socket.io [anode.js 库]) 但为什么不是 PHP 呢?
您可以将 PHP 与 WebSockets 一起使用,请查看 Ratchet.
I have tried reading some articles, but I am not very clear on the concepts yet.
Would someone like to take a shot at explaining to me what these technologies are:
- Long Polling
- Server-Sent Events
- Websockets
- Comet
One thing that I came across every time was, the server keeps a connection open and pushes data to the client. How is the connection kept open, and how does the client get the pushed data? (How does the client use the data, maybe some code might help?)
Now, which one of them should I use for a real-time app. I have been hearing a lot about websockets (with socket.io [a node.js library]) but why not PHP?
解决方案In the examples below the client is the browser and the server is the webserver hosting the website.
Before you can understand these technologies, you have to understand classic HTTP web traffic first.
Regular HTTP:
- A client requests a webpage from a server.
- The server calculates the response
- The server sends the response to the client.
Ajax Polling:
- A client requests a webpage from a server using regular HTTP (see HTTP above).
- The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at regular intervals (e.g. 0.5 seconds).
- The server calculates each response and sends it back, just like normal HTTP traffic.
Ajax Long-Polling:
- A client requests a webpage from a server using regular HTTP (see HTTP above).
- The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
- The server does not immediately respond with the requested information but waits until there's new information available.
- When there's new information available, the server responds with the new information.
- The client receives the new information and immediately sends another request to the server, re-starting the process.
HTML5 Server Sent Events (SSE) / EventSource:
- A client requests a webpage from a server using regular HTTP (see HTTP above).
- The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
The server sends an event to the client when there's new information available.
- Real-time traffic from server to client, mostly that's what you'll need
- You'll want to use a server that has an event loop
- Connections with servers from other domains are only possible with correct CORS settings
- If you want to read more, I found these very useful: (article), (article), (article), (tutorial).
HTML5 Websockets:
- A client requests a webpage from a server using regular http (see HTTP above).
- The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server.
The server and the client can now send each other messages when new data (on either side) is available.
- Real-time traffic from the server to the client and from the client to the server
- You'll want to use a server that has an event loop
- With WebSockets it is possible to connect with a server from another domain.
- It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to implement the client side, which is very easy!
- If you want to read more, I found these very useful: (article), (article) (tutorial).
Comet:
Comet is a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications. Read more on wikipedia or this article.
Now, which one of them should I use for a realtime app (that I need to code). I have been hearing a lot about websockets (with socket.io [a node.js library]) but why not PHP ?
You can use PHP with WebSockets, check out Ratchet.
这篇关于什么是长轮询、Websocket、服务器发送事件 (SSE) 和 Comet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是长轮询、Websocket、服务器发送事件 (SSE) 和 Comet?
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01