如何在长时间内以增量方式向浏览器显示HTML? [英] How to display HTML to the browser incrementally over a long period of time?

查看:86
本文介绍了如何在长时间内以增量方式向浏览器显示HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否需要传回所有HTTP标头,以告知浏览器我的服务器不会立即关闭连接并在收到HTML时显示?是否有必要让HTML像flush()那样逐步显示?

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 anything necessary to get the HTML to incrementally display like flush()?

该技术曾经用于诸如聊天之类的事情,但我正在考虑将其用于COMET类型的应用程序.

This technique used to be used for things like chat, but I'm thinking about using it for a COMET type application.

推荐答案

长时间轮询是执行此类操作的常用技术;简要总结一下,它的工作方式如下:

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

  1. 客户端将XHR发送到服务器.

  1. The client sends an XHR to the server.

  • 如果已经准备好数据,服务器将立即返回.
  • 如果没有,则服务器将保持打开连接状态,直到数据变为可用为止,然后它将返回此连接.
  • 如果请求超时,请返回1).

在客户端上运行的页面接收此数据,并对其进行处理.

The page running on the client receives this data, and does what it does with it.

这就是Facebook 实现聊天功能的方式.

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.

这篇关于如何在长时间内以增量方式向浏览器显示HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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