开发者

chrome - don´t wait until all data is received - node.js

开发者 https://www.devze.com 2023-02-19 17:25 出处:网络
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´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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号