javascript - Streaming data over ajax - Stack Overflow

I want to access AJAX data before the request has finished, effectively to implement streaming kind of

I want to access AJAX data before the request has finished, effectively to implement streaming kind of like this:

ajax_request.send();
interval = setInterval(function() {
    continueParsing(ajax_request.responseText);
    if (download_plete)
        clearInterval(interval);
}, 64);

Right now I have a php thingy to break the request up into smaller chunks, but I'd rather take it all in one go. What's the best way to do this (I only care about Chrome and Firefox).

I want to access AJAX data before the request has finished, effectively to implement streaming kind of like this:

ajax_request.send();
interval = setInterval(function() {
    continueParsing(ajax_request.responseText);
    if (download_plete)
        clearInterval(interval);
}, 64);

Right now I have a php thingy to break the request up into smaller chunks, but I'd rather take it all in one go. What's the best way to do this (I only care about Chrome and Firefox).

Share Improve this question asked Feb 15, 2012 at 23:40 ChrisChris 6,7327 gold badges45 silver badges55 bronze badges 2
  • great question, I think this works with HTML5 specs, but i will sure try to find out – André Alçada Padez Commented Feb 16, 2012 at 0:30
  • what kind of data are sending from PHP? – André Alçada Padez Commented Feb 16, 2012 at 0:32
Add a ment  | 

2 Answers 2

Reset to default 7

Well, starting with a PHP handler like this:

$content = file_get_contents('http://pplware.sapo.pt/feed/');
for($i = 0; $i < 10; $i++){
   $content .= $content;
}
echo $content;

and a javascript like this:

var client = new XMLHttpRequest();
client.open('get', 'ajax.php');
client.send();
client.onprogress = function(){
    console.log(this.responseText.length);              
}

I get this console:

11183
137415984
1311572876
1313769728

so, it works.... i think you can figure out the rest :)

You're best using WebSockets to do this kind of thing and have the appropriate fallback for older browsers (say in the form of AJAX long polling).

Since you're using PHP a quick google search turned up this project - http://code.google./p/phpwebsocket/ that could provide a simple way to do it. I don't know if it has a fallback to other technologies if the browser doesn't support websockets but if it doesn't you might be able to put socket.io-client over the top of it and just use the phpwebsocket project to provide your server layer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743761957a4502795.html

相关推荐

  • javascript - Streaming data over ajax - Stack Overflow

    I want to access AJAX data before the request has finished, effectively to implement streaming kind of

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信