如何在NodeJS中刷新任意大小的块 [英] How to flush chunks of arbitrary sizes in NodeJS

查看:96
本文介绍了如何在NodeJS中刷新任意大小的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Node Web服务器中,我要在特定位置刷新HTML内容,如下所示:

In a Node web server I want to flush HTML content at specific points as follows:

  • 第一块:< html>< head>...</head>
  • 第二个块:< body>...</body>
  • 第三个块:</html>

例如:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});

  res.write('<html><head> ... </head>');                                           

  setTimeout(function() {                                                          
    res.write('<body> ... </body>');                                               

    setTimeout(function() {                                                        
      res.end('</html>');                                                          
    }, 2000);                                                                      

  }, 2000);

}).listen(8000);

上面的代码响应< html>< head>...</head>< body>...</body></html> 在单个块中经过约4秒后,但是我注意到,块应该大于等于4096字节才能立即被刷新:

The code above responds <html><head> ... </head><body> ... </body></html> after ~4s in a single chunk, however I noticed chunks should be >= 4096bytes in order to get flushed immediately:

var http = require('http');

http.createServer(function (req, res) {                                            
  res.writeHead(200, {'Content-Type': 'text/plain'});                              

  res.write(Array(4097).join('*'));

  setTimeout(function() {                                                          
    res.write(Array(4097).join('#'));

    setTimeout(function() {                                                        
      res.end('done!');                                                            
    }, 2000);

  }, 2000);

}).listen(8000);

以上代码的响应也需要约4秒钟,但立即会刷新块.我可以填充小块以填充至少4096bytes,只是想知道是否还有另一种非hacky"方式.

The response from code above also takes ~4s but chunks are flushed immediately. I could pad small chunks to fill at least 4096bytes, just wondering if there's another "non-hacky" way.

在PHP中,这可以通过 flush()/ ob_flush()并禁用 output_buffering

In PHP this could be achieved through flush()/ob_flush() and disabling output_buffering

FWIW,我正在构建一个Web服务器工具,以对HTML块输出的几种配置进行实验,并在它们之间设置给定的延迟,以便分析现代浏览器如何处理并选择最佳配置.

FWIW, I'm building a web server tool to experiment several configurations of HTML chunk output with a given delay between them in order to analyze how modern browsers handle it and pick the optimal configuration.

谢谢

推荐答案

这是半重复的答案是已经完成了您想要的,只是浏览器不会解析并显示任何内容,直到接收到足够多的数据来进行解析为止.发送4097块将使浏览器能够推动文档部分的解析,而不会推动Node以不同的方式发送块.

The answer is that is already does what you want, it's just that the browser doesn't parse and display anything until it has received enough data to bother parsing it. Sending your chunks of 4097 gives the browser a push to parse part of the document, it doesn't push Node to send the chunks differently.

如果将其置于非缓冲模式,则可以使用 curl 对此进行测试.

You can test this out using curl if you put it in non-buffering mode.

curl -N localhost:8000

这篇关于如何在NodeJS中刷新任意大小的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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