我试着读了一些文章,但我对概念还不是很清楚。

有没有人能给我解释一下这些技术是什么

长轮询 服务器发送的事件 尚 彗星

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

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


当前回答

在下面的例子中,客户端是浏览器,服务器是托管网站的web服务器。

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

常规的HTTP:

客户端从服务器请求一个网页。 服务器计算响应 服务器将响应发送到客户机。

Ajax投票:

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

Ajax长轮询:

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服务器发送事件(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是HTML5之前的技术集合,它使用流和长轮询来实现实时应用程序。在维基百科或这篇文章上阅读更多。


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

你可以在WebSockets中使用PHP,看看Ratchet。

其他回答

在下面的例子中,客户端是浏览器,服务器是托管网站的web服务器。

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

常规的HTTP:

客户端从服务器请求一个网页。 服务器计算响应 服务器将响应发送到客户机。

Ajax投票:

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

Ajax长轮询:

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服务器发送事件(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是HTML5之前的技术集合,它使用流和长轮询来实现实时应用程序。在维基百科或这篇文章上阅读更多。


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

你可以在WebSockets中使用PHP,看看Ratchet。

Tieme在他的精彩回答上花了很多精力,但我认为OP问题的核心是这些技术如何与PHP相关,而不是每种技术如何工作。

除了客户端HTML、CSS和Javascript之外,PHP是web开发中使用最多的语言。然而,PHP在实时应用程序方面有两个主要问题:

PHP started as a very basic CGI. PHP has progressed very far since its 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 its earlier model of execution, so it hasn't yet made a solid attempt to escape the CGI model internally. Even the command line 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 its 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.

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

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

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

你可以很容易地在你的web应用程序中使用Node.JS进行实时通信。Node.JS在WebSockets方面非常强大。因此,“通过Node.js的PHP通知”将是一个很棒的概念。

请看这个例子: 用PHP和Node.js创建一个实时聊天应用程序

轮询

基本上,轮询是一种定期从服务器请求信息的技术。这种连接通过HTTP协议实现。轮询有两种类型:

短的轮询 长轮询

短的轮询

In short polling, the client requests information from the server. The server processes the request. If data is available for the request, server responds to the request with the required information. However, if the server has no data available for the client, server returns an empty response. In both the situation, the connection will be closed after returning the response. Clients keep issuing new requests even after server sends the empty responses. This mechanism increases the network cost on the server.

长轮询

In long polling, the clients can request information from the server with the expectation that the server may not respond immediately. When the server receives the request, if it has no fresh data for the client, rather than returning an empty response, the server keeps the request open and waits for data to arrive. When the server receives new data, it delivers the response to the client right away, completing the open request. The client can then send another request for new updates after getting the answer from the server. Long polling reduces costs by reducing the number of empty responses.

WebSocket

WebSocket is a protocol that provides two-way(bi-directional) communication channels over a single TCP connection. Websocket facilitates a persistent connection between a client and a server, allowing both parties to begin transferring data at any moment. The WebSocket handshake is the procedure through which the client creates a WebSocket connection. If the operation is successful, the server and client can send and receive data at any time. Mostly used in real-time web applications such as WhatsApp, Uber.

服务器发送事件(SSE)

与WebSockets不同,我们不能使用SSE从客户端向服务器发出请求,因为它是单向连接。当我们需要从服务器到客户端的“近实时”传输时,或者如果服务器在循环中生成数据,SSE是理想的选择。

彗星

Comet是一种web应用程序设计范式,它使用本地HTTP方法描述了服务器和web浏览器之间的持续双向交互。彗星是一个总称。Ajax Push、HTTP Streaming和HTTP Server Push是一些可用于提供这种事件驱动交互的HTTP机制。