How to display HTML to the browser incrementally over a long period of time?

前端 未结 8 743
生来不讨喜
生来不讨喜 2020-12-05 11:58

Do I need to pass back any HTTP headers to tell the browser that my server won\'t be immediately closing the connection and to display as the HTML is received? Is there anyt

8条回答
  •  北海茫月
    2020-12-05 12:13

    Long polling is a common technique to do something like this; to briefly summarise, it works as follows:

    1. The client sends an XHR to the server.

      • If there is data ready, the server returns this immediately.
      • If not, the server keeps the connection open until data does become available, then it returns this.
      • If the request times-out, go back to 1).
    2. The page running on the client receives this data, and does what it does with it.

    3. Go back to 1)

    This is how Facebook implements its chat feature.

    This article also clears up some of the misconceptions of long-polling, and details some of the benefits of doing so.

提交回复
热议问题