是否有可能为一个Perl CGI脚本段的AJAX响应成无数个个体的HTTP响应?
Is it possible for a perl cgi script to segment its AJAX responses into numerous individual HTTP responses?
说我有这个code:
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
onDataReceived(xmlhttp.responseText);
}
else if(xmlhttp.status!=200 && xmlhttp.status!=0) { }
}
xmlhttp.open("POST","script.cgi",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(toURLString(options));
如JavaScript(不要告诉我,与IE浏览器,我知道,XML对象的兼容性问题并不在意)。
as javascript (dont tell me about xml object compatibility issues with ie, I know, and don't care).
和这样的:
print "Content-type: text/html\n\n";
my %form = Vars();
if($ENV{REQUEST_METHOD} eq "POST" )
{
$|=1;
for(my $i, (1..100000000))
{
print "1\n";
}
}
和Perl CGI。是否可以打印出1秒的无数单个数据包这样的结果,而不是生成亿1秒,最后才具有输出?
as perl cgi. Is it possible to print out this result in numerous individual packets of 1s, instead of generating 100000000 1s before finally having an output?
请参见这太问题的可能的方法,虽然它不使用Perl具体的:
Please see this SO question for possible approaches, though it's not Perl specific:
http://stackoverflow.com/questions/1155066/dealing-with-incremental-server-response-in-ajax-in-javascript
从链接的维基文章,这个环节似乎最相关的: http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHtt$p$pquest
From the linked Wiki article, this link seems most relevant: http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest
但是,我强烈建议考虑轮询方法,而不是服务器推送一个你正在考虑
服务器存储数据的可访问文件的数据块(有一些订货元信息)
The server stores the chunks of data as accessible files (with some ordering meta info)
print "Location: xxxx";
# Sorry, forgot the exact form of Location HTTP response.
# Location points to URL mapped to /home/htdocs/webdocs/tmp/chunk_0.html
my %form = Vars();
if($ENV{REQUEST_METHOD} eq "POST" )
{
$|=1;
$file_num = 0;
my $fh;
for(my $i, (1..100000000))
{
if ($i % 1000 == 0) {
close $fh if $fh;
open $fh, ">", "/home/htdocs/webdocs/tmp/chunk_${file_num}.html";
# Add the usual error handling on open/close i'm too lazy to type
$file_num++;
}
print $fh "1\n";
}
print $fh "\n##############END_TRANSMISSION__LAST_FILE####################\n";
# This was a singularly dumb way of marking EOF but you get the drift
close $fh;
}
的AJAX的轮询器检索它们在循环中一个接一个,处理含下一个块并寻找元信息知道什么(如果)下一段轮询是响应
The AJAX poller retrieves them in a loop one by one, processing the response containing the next chunk and looking for meta-info to know what (and if) the next piece to poll for is.