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

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

问题描述

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

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. 长轮询
  2. 服务器发送的事件
  3. Websockets
  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?)

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

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?

推荐答案

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

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

Before you can understand these technologies, you have to understand classic HTTP web traffic first.

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

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

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

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

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

  1. 客户端使用常规http(请参阅上面的HTTP)从服务器请求网页.
  2. 客户端收到请求的网页,并在打开与服务器的连接的页面上执行JavaScript.
  3. 服务器(服务器)和客户端(客户端)现在可以在新数据(任一侧)可用时互相发送消息.

  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是HTML5之前的技术的集合,这些技术使用流传输和长轮询来实现实时应用程序.在维基百科

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.

现在,我应该在实时应用中使用哪一个(我需要 代码).我已经听到很多关于websockets的信息(使用socket.io [a node.js库]),但为什么不使用PHP?

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结合使用,查看 Ratchet .

You can use PHP with WebSockets, check out Ratchet.

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

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