什么是长轮询、Websockets、服务器发送事件 (SSE) 和 Comet? [英] What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

查看:45
本文介绍了什么是长轮询、Websockets、服务器发送事件 (SSE) 和 Comet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 长轮询
  2. 服务器发送的事件
  3. Websockets
  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. 当有新信息可用时,服务器向客户端发送一个事件.

    • 从服务器到客户端的实时流量,主要是您需要的
    • 您需要使用具有事件循环的服务器
    • 只有 或 或这篇 文章.

      <小时><块引用>

      现在,我应该将其中的哪一个用于实时应用程序(我需要代码).我已经听说了很多关于 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.

      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.

      这篇关于什么是长轮询、Websockets、服务器发送事件 (SSE) 和 Comet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆