如何长时间向浏览器增量显示 HTML?

发布于 2024-07-24 07:00:00 字数 133 浏览 1 评论 0原文

我是否需要传回任何 HTTP 标头来告诉浏览器我的服务器不会立即关闭连接并在收到 HTML 时显示? 是否需要像flush()那样让HTML逐渐显示?

该技术过去用于聊天等内容,但我正在考虑将其用于 COMET 类型的应用程序。

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()?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

小霸王臭丫头 2024-07-31 07:00:00

长轮询是执行此类操作的常用技术; 简而言之,它的工作原理如下:

  1. 客户端向服务器发送 XHR。

    • 如果有数据准备好,服务器会立即返回。
    • 如果没有,服务器将保持连接打开,直到数据可用,然后返回该连接。
    • 如果请求超时,请返回 1)。
  2. 在客户端上运行的页面接收此数据,并对其执行相应操作。

  3. 返回 1)

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

这篇文章还澄清了一些长期的误解民意调查,并详细介绍了这样做的一些好处。

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.

风蛊 2024-07-31 07:00:00

当客户端在一定时间内没有收到任何数据时,将关闭连接。 此超时不受 HTTP 标头的影响。 它是特定于客户端的,通常设置为 120 秒 IIRC。

因此,您所要做的就是定期发送少量数据以避免超时。

The client will close the connection when it does not receive any data for a certain time. This timeout cannot be influenced by HTTP headers. It is client-specific and usually set to 120 seconds IIRC.

So all you have to do is send small amounts of data regularly to avoid hitting the timeout.

尛丟丟 2024-07-31 07:00:00

我认为更强大的解决方案是带有 Javascript 计时器的页面,该计时器轮询服务器以获取新数据。 保持响应开放并不是 HTTP 协议的设计目的。

I think a more robust solution is a page with a Javascript timer that polls the server for new data. Keeping the response open is not something the HTTP protocol was designed for.

青萝楚歌 2024-07-31 07:00:00

我只会在执行过程中回显/打印 HTML。 有几种不同的方法可以让脚本在发送下一位之前暂停。 您不需要对标头或任何特殊代码执行任何操作来告诉浏览器等待。 只要您的脚本仍在运行,它就会渲染从脚本接收到的 HTML。

echo "<HTML><HEAD.../HEAD><BODY>";
while (running)
{
    echo "printing html... </br>";
}
echo "</BODY></HTML>";  //all done

I would just echo / print the HTML as I went. There are a few different ways you can have the script pause before sending the next bit. You shouldn't need to do anything with headers or any special code to tell the browser to wait. As long as your script is still running it will render the HTML it receives from the script.

echo "<HTML><HEAD.../HEAD><BODY>";
while (running)
{
    echo "printing html... </br>";
}
echo "</BODY></HTML>";  //all done
小鸟爱天空丶 2024-07-31 07:00:00

尝试永远的框架(就像在 gmail 中一样)

所有这些技术都只是黑客,http 不是设计来做到这一点的。

Try forever frame (like in gmail)

All of these technics are just hacks, http isn't designed to do this.

热风软妹 2024-07-31 07:00:00

在脚本末尾,使用类似的内容(假设您通过将 ob_start() 放在页面顶部来打开输出缓冲

<?php

set_time_limit(0); // Stop PHP from closing script after 30 seconds

ob_start();

echo str_pad('', 1024 * 1024, 'x'); // Dummy 1 megabyte string

$buffer = ob_get_clean();

while (isset($buffer[0])) {

$send = substr($buffer, 0, 1024 * 30); // Get 30kbs bytes from buffer :D
$buffer = substr($buffer, 1024 * 30); // Shorten buffer

echo $send; // Send buffer
echo '<br />'; // forces browser to reload contents some how :P

ob_flush(); // Flush output to browser
flush(); 

sleep(1); // Sleep for 1 second

}

?>

该脚本基本上以 30kbs(模拟)输出 1 MB 的文本,无论用户和服务器的速度有多快连接群岛。

at the end of your script, use something like this (assuming you had output buffering on by putting ob_start() at the top of your page

<?php

set_time_limit(0); // Stop PHP from closing script after 30 seconds

ob_start();

echo str_pad('', 1024 * 1024, 'x'); // Dummy 1 megabyte string

$buffer = ob_get_clean();

while (isset($buffer[0])) {

$send = substr($buffer, 0, 1024 * 30); // Get 30kbs bytes from buffer :D
$buffer = substr($buffer, 1024 * 30); // Shorten buffer

echo $send; // Send buffer
echo '<br />'; // forces browser to reload contents some how :P

ob_flush(); // Flush output to browser
flush(); 

sleep(1); // Sleep for 1 second

}

?>

That script basically outputs 1 megabyte of text at 30kbs (simulated) no matter how fast the user and server connection is.

蓝眼泪 2024-07-31 07:00:00

根据您正在做的事情,您可以在脚本继续时进行回显,然后在回显时将 html 发送到浏览器。

Depending on what you are doing, you could just echo as your script proceeds, this will then send the html to the browser as it is echoed.

错爱 2024-07-31 07:00:00

我建议您研究使用 Ajax 而不是普通的旧 HTML 来实现此类功能。 这使您在架构设计和用户界面方面具有更大的灵活性

I would suggest you investigate implementing such functionality using Ajax, rather than plain old HTML. This allows you much more flexibility in terms of architectural design and user interface

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文