北城已荒凉 2012-06-18 06:28 采纳率: 0%
浏览 469
已采纳

什么是 Long-Polling、 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. 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?

转载于:https://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet

  • 写回答

3条回答 默认 最新

  • hurriedly% 2012-10-12 08:57
    关注

    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.

    HTTP

    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 Polling

    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.

    Ajax Long-Polling

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

    HTML5 WebSockets

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题