[英]Segmenting AJAX responses in perl CGI?
perl cgi腳本是否有可能將其AJAX響應分段為多個單獨的HTTP響應?
說我有這個代碼:
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(不要告訴我有關xml對象兼容性問題,即我知道,並不在意)。
還有這個:
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。 是否有可能在1s的多個單獨數據包中打印出這個結果,而不是在最終輸出之前產生100000000 1s?
請參閱此SO問題以了解可能的方法,盡管它不是Perl特定的:
從鏈接的Wiki文章中,此鏈接似乎最相關: http : //en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest
但是,我強烈建議考慮使用輪詢方法而不是您正在考慮的“服務器推送” :
服務器將數據塊存儲為可訪問文件(帶有一些訂購元信息)
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輪詢器逐個循環地檢索它們,處理包含下一個塊的響應並查找元信息以了解要輪詢的下一個部分是什么(以及是否)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.