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

我已经尝试阅读一些文章,但我对这些概念还不是很清楚.

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

  1. 长轮询
  2. 服务器发送的事件
  3. 网络套接字
  4. 彗星

我每次遇到的一件事是,服务器保持连接打开并将数据推送到客户端.连接如何保持打开状态,客户端如何获取推送的数据?(客户端如何使用数据,也许一些代码可能会有所帮助?)

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

解决方案

在下面的示例中,客户端是浏览器,服务器是托管网站的网络服务器.

在了解这些技术之前,您必须先了解经典 HTTP 网络流量.

常规 HTTP:

  1. 客户端向服务器请求网页.
  2. 服务器计算响应
  3. 服务器将响应发送给客户端.

Ajax 轮询:

  1. 客户端使用常规 HTTP 向服务器请求网页(请参阅上面的 HTTP).
  2. 客户端收到请求的网页,并在页面上执行 JavaScript,以定期(例如 0.5 秒)从服务器请求文件.
  3. 服务器计算每个响应并将其发回,就像正常的 HTTP 流量一样.

Ajax 长轮询:

  1. 客户端使用常规 HTTP 向服务器请求网页(请参阅上面的 HTTP).
  2. 客户端接收到请求的网页,并在页面上执行向服务器请求文件的 JavaScript.
  3. 服务器不会立即响应请求的信息,而是等到有新的信息可用.
  4. 当有新信息可用时,服务器会以新信息进行响应.
  5. 客户端收到新信息后立即向服务器发送另一个请求,重新启动该过程.

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

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

    • 从服务器到客户端的实时流量,大部分是您需要的
    • 您需要使用具有事件循环的服务器
    • 只有使用正确的 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:

      1. Long Polling
      2. Server-Sent Events
      3. Websockets
      4. 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:

      1. A client requests a webpage from a server.
      2. The server calculates the response
      3. The server sends the response to the client.

      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.

      Ajax Long-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.
      3. The server does not immediately respond with the requested information but waits until there's new information 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.

      HTML5 Server Sent Events (SSE) / EventSource:

      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.

        • 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:

      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 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.

相关文章