Segmenting AJAX responses in perl CGI?

这一生的挚爱 提交于 2019-12-05 12:33:30
DVK

Please see this SO question for possible approaches, though it's not Perl specific:

Dealing with incremental server response in AJAX (in JavaScript)

From the linked Wiki article, this link seems most relevant: http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest

However, I would strongly suggest considering a polling approach instead of the "server push" one you are considering:

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;
}

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.

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