铬 - 不要等到收到所有数据 - node.js [英] chrome - don´t wait until all data is received - node.js
问题描述
我正在尝试使用node.js,并且可以保持http连接处于打开状态,以便稍后发送新数据。就像一个单向套接字。
我有以下的node.js http服务器:
var http = require(http);
var s = http.createServer(function(req,res){
var i = 0;
res.writeHead(200,{'content-type':'text ($ i $)
var i = setInterval(function(){
res.write((i ++)+);
if(i == 50){
clearInterval(iv);
res.end();
}
},1000);
});
s.listen(8000);
当我在firefox上向服务器发出请求时,每隔一秒我会得到一个新的数字,直到数字50已到达。
在chrome中,它等待50秒,然后它立即给我所有的数字。如何在chrome中实现firefox的相同行为?
我找到了答案:
奇怪的是,我必须首先发送这么多的字节才能启用流式传输,但是它可以正常工作。
我的Chrome首先需要1024个字节。
更新
或者,您可以设置内容类型为文本/ JavaScript。然后立即开始流式传输!
I´m experimenting with node.js and the possibility to keep a http connection open, to send new data later. Just like a one-way socket.
I have the following node.js http server:
var http = require("http");
var s = http.createServer(function(req,res){
var i = 0;
res.writeHead(200,{'content-type': 'text/plain'});
var iv = setInterval(function(){
res.write((i++)+"");
if(i == 50){
clearInterval(iv);
res.end("");
}
},1000);
});
s.listen(8000);
When I make a request to this server in firefox, I get every second a new number, until number 50 is reached.
In chrome, it waits 50 seconds and then it gives me all numbers at once. How can I achieve the same behaviour of firefox in chrome?
I found the answer:
Google Chrome and Streaming HTTP connections?
It´s strange that I must send so much bytes first to enable streaming, but it works.
My chrome needs exatcly 1024 bytes first.
UPDATE
Alternatively, you can set the content-type to text/javascript. Then the streaming begins immediately!
这篇关于铬 - 不要等到收到所有数据 - node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!