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

僤鯓⒐⒋嵵緔 提交于 2019-12-07 22:04:33

问题


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?


回答1:


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!




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!