php 什么是长轮询、Websockets、服务器发送事件 (SSE) 和 Comet?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11077857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:42:31  来源:igfitidea点击:

What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

phpwebsocketcometlong-pollingserver-sent-events

提问by user1437328

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:

有人愿意向我解释一下这些技术是什么吗:

  1. Long Polling
  2. Server-Sent Events
  3. Websockets
  4. Comet
  1. 长轮询
  2. 服务器发送的事件
  3. 网络套接字
  4. 彗星

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?

现在,我应该将其中的哪一个用于实时应用程序。我听说过很多关于 websockets(使用 socket.io [a node.js 库]),但为什么不是 PHP?

回答by Tieme

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 classicHTTP web traffic first.

在了解这些技术之前,您必须先了解经典的HTTP Web 流量。

Regular HTTP:

常规 HTTP:

  1. A client requests a webpage from a server.
  2. The server calculates the response
  3. The server sends the response to the client.
  1. 客户端从服务器请求网页。
  2. 服务器计算响应
  3. 服务器将响应发送给客户端。

HTTP

HTTP

Ajax Polling:

阿贾克斯轮询:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. 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).
  3. The server calculates each response and sends it back, just like normal HTTP traffic.
  1. 客户端使用常规 HTTP 从服务器请求网页(参见上面的 HTTP)。
  2. 客户端接收请求的网页并在页面上执行 JavaScript,以定期(例如 0.5 秒)从服务器请求文件。
  3. 服务器计算每个响应并将其发送回,就像正常的 HTTP 流量一样。

Ajax Polling

Ajax 轮询

Ajax Long-Polling:

Ajax 长轮询:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
  3. The server does not immediately respond with the requested information but waits until there's newinformation available.
  4. When there's new information available, the server responds with the new information.
  5. The client receives the new information and immediately sends another request to the server, re-starting the process.
  1. 客户端使用常规 HTTP 从服务器请求网页(参见上面的 HTTP)。
  2. 客户端接收请求的网页并在页面上执行向服务器请求文件的 JavaScript。
  3. 服务器不会立即响应请求的信息,而是等待直到有信息可用。
  4. 当有新信息可用时,服务器用新信息响应。
  5. 客户端收到新信息并立即向服务器发送另一个请求,重新启动该过程。

Ajax Long-Polling

Ajax 长轮询

HTML5 Server Sent Events (SSE) / EventSource:

HTML5 服务器发送的事件 (SSE)/事件源:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
  3. The server sends an event to the client when there's new information available.

  1. 客户端使用常规 HTTP 从服务器请求网页(参见上面的 HTTP)。
  2. 客户端接收请求的网页并在页面上执行 JavaScript,打开与服务器的连接。
  3. 当有新信息可用时,服务器向客户端发送一个事件。

HTML5 SSE

HTML5 SSE

HTML5 Websockets:

HTML5 Websocket:

  1. A client requests a webpage from a server using regular http (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server.
  3. 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 andfrom 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 Pusheror 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).
  1. 客户端使用常规 http(参见上面的 HTTP)从服务器请求网页。
  2. 客户端接收请求的网页并在页面上执行 JavaScript,打开与服务器的连接。
  3. 当新数据(在任何一方)可用时,服务器和客户端现在可以相互发送消息。

    • 从服务器到客户端以及从客户端到服务器的实时流量
    • 您需要使用具有事件循环的服务器
    • 使用 WebSockets 可以连接到来自另一个域的服务器。
    • 也可以使用第三方托管的 websocket 服务器,例如Pusher其他。这样你只需要实现客户端,这很容易!
    • 如果你想阅读更多,我发现这些非常有用:(文章),(文章)教程)。

HTML5 WebSockets

HTML5 WebSockets

Comet:

彗星:

Comet is a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications. Read more on wikipediaor thisarticle.

Comet 是 HTML5 之前的技术集合,它们使用流和长轮询来实现实时应用程序。阅读更多维基百科这篇文章。



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 ?

现在,我应该将其中的哪一个用于实时应用程序(我需要编码)。我听说过很多关于 websockets(使用 socket.io [a node.js 库]),但为什么不是 PHP 呢?

You can use PHP with WebSockets, check out Ratchet.

您可以将 PHP 与 WebSockets 一起使用,请查看Ratchet

回答by JSON

Tieme put a lot of effort into his excellent answer, but I think the core of the OPs question is how these technologies relate to PHP rather than how each technology works.

Tieme 在他出色的回答中投入了大量精力,但我认为 OP 问题的核心是这些技术与 PHP 的关系,而不是每种技术的工作原理。

PHP is the most used language in web development besides the obvious client side html, css, and javascript. Yet PHP has 2 major issues when it comes to real time applications:

除了明显的客户端 html、css 和 javascript 之外,PHP 是 Web 开发中最常用的语言。然而,当涉及到实时应用程序时,PHP 有两个主要问题:

1) PHP started as a very basic CGI. PHP has progressed very far since it's early stage, but it happened in small steps. PHP already had many millions of users by the time it became the embed-able and flexible C library that it is today, most of whom were dependent on it's earlier model of execution, so it hasn't yet made a solid attempt to escape the cgi model internally. Even the commandline interface invokes the PHP library (libphp5.so on linux, php5ts.dll on windows, etc) as if it still a cgi processing a GET/POST request. It still executes code as if it just has to build a "page" and then end it's life cycle. As a result, it has very little support for multi-thread or event driven programming (within PHP userspace), making it currently unpractical for real time, multi-user applications.

1) PHP 开始时是一个非常基本的 CGI。PHP 从早期阶段就取得了很大的进步,但它是在很小的步骤中发生的。当 PHP 成为今天的可嵌入且灵活的 C 库时,它已经拥有数百万用户,其中大多数人依赖于它早期的执行模型,因此它还没有做出切实的尝试来摆脱内部的 cgi 模型。甚至命令行界面也会调用 PHP 库(Linux 上的 libphp5.so,Windows 上的 php5ts.dll 等),就好像它仍然是一个处理 GET/POST 请求的 cgi。它仍然执行代码,就好像它只需要构建一个“页面”然后结束它的生命周期。因此,它几乎不支持多线程或事件驱动编程(在 PHP 用户空间内),这使得它目前对于实时、多用户应用程序不切实际。

Note that PHP does have extensions to provide event loops (such as libevent) and threads (such as pthreads) in PHP userspace, but very, very, few of the applications use these.

请注意,PHP 确实具有在 PHP 用户空间中提供事件循环(例如 libevent)和线程(例如 pthreads)的扩展,但是非常非常少的应用程序使用这些。

2) PHP still has significant issues with garbage collection. Although these issues have been consistently improving (likely it's greatest step to end the life cycle as described above), even the best attempts at creating long running PHP applications require being restarted on a regular basis. This also make it unpractical for real time applications.

2) PHP 在垃圾收集方面仍然存在重大问题。尽管这些问题一直在改善(这可能是结束上述生命周期的最大步骤),即使是创建长时间运行的 PHP 应用程序的最佳尝试也需要定期重新启动。这也使其对于实时应用程序不切实际。

PHP 7 will be a great step to fix these issues as well, and seems very promising as a platform for real-time applications.

PHP 7 也将是解决这些问题的重要一步,并且作为实时应用程序的平台似乎非常有前途。

回答by John

I have tried to make note about these and have collected and written examples from a java perspective.

我试图记录这些,并从Java 的角度收集和编写了示例。

HTTP for Java Developers

面向 Java 开发人员的 HTTP

Reverse Ajax - Old style

反向 Ajax - 旧风格

Async Handling on server side

服务器端的异步处理

Reverse Ajax - New style

反向 Ajax - 新风格

Server Sent Events

服务器发送事件

Putting it here for any java developer who is looking into the same subject.

将它放在这里供任何正在研究同一主题的 Java 开发人员使用。

回答by Supun Kavinda

You can easily use Node.JS in your web app only for real-time communication. Node.JS is really powerful when it's about WebSockets. Therefore "PHP Notifications via Node.js" would be a great concept.

您可以在 Web 应用程序中轻松使用 Node.JS,仅用于实时通信。当涉及到 WebSockets 时,Node.JS 真的很强大。因此,“通过 Node.js 的 PHP 通知”将是一个很好的概念。

See this example: Creating a Real-Time Chat App with PHP and Node.js

请参阅此示例: 使用 PHP 和 Node.js 创建实时聊天应用程序