问题

我已经尝试阅读一些文章,但我不太清楚的概念.

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

  1. Long Polling
  2. Server-Sent Events
  3. Websockets
  4. Comet

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

现在,其中一个应该用于实时应用程序.我已经听到很多关于websockets(与socket.io [一个node.js库]),但为什么不PHP?



解决方法

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

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

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.

HTTP

Ajax Polling:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The requested webpage executes JavaScript 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投票

Ajax Long-Polling:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The requested webpage executes JavaScript 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.

Ajax长轮询

HTML5 Server Sent Events (SSE) / EventSource:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The requested webpage executes javascript 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
    • Not possible to connect with a server from another domain
    • If you want to read more, I found these very useful: (article), (article), (article), (tutorial).

HTML5 SSE

HTML5 Websockets:

  1. A client requests a webpage from a server using regular http (see HTTP above).
  2. The requested webpage executes JavaScript 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).

HTML5 WebSockets

Comet:

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 ?

您可以使用PHP和WebSockets,请查看棘轮.




相关问题推荐