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!
Browsers do not have to implement the feature of partial ajax responses, have a look at google, there are many quite differnt workarounds for this problem for each browser.
来源:https://stackoverflow.com/questions/5420162/chrome-don%c2%b4t-wait-until-all-data-is-received-node-js