How can I send multiple images in a server push Perl CGI program?
I am a beginner in Perl CGI etc. I was experimenting with server-push concept with a piece of Perl code. It is sup开发者_如何学Goposed to send a jpeg image to the client every three seconds.
Unfortunately nothing seems to work. Can somebody help identify the problem?
Here is the code:
use strict;
# turn off io buffering
$|=1;
print "Content-type: multipart/x-mixed-replace;";
print "boundary=magicalboundarystring\n\n";
print "--magicalboundarystring\n";
#list the jpg images
my(@file_list) = glob "*.jpg";
my($file) = "";
foreach $file(@file_list )
{
open FILE,">", $file or die "Cannot open file $file: $!";
print "Content-type: image/jpeg\n\n";
while ( <FILE> )
{
print "$_";
}
close FILE;
print "\n--magicalboundarystring\n";
sleep 3;
next;
}
EDIT: added turn off i/o buffering, added "use strict" and "@file_list", "$file" are made local
Flush the output.
Most probably, the server is keeping the response in the buffer. You may want to do fflush(STDOUT)
after every print
or autoflush STDOUT
once.
Have a look at http://www.abiglime.com/webmaster/articles/cgi/032498.htm
[quote]
To use the script below, you'll need to implement a called "non-parsed" CGIs on your site. Normally, the web server will buffer all output from your CGI program until it the program finishes. We don't want that to happen here. With Apache, it's quite easy. If the name of your CGI program starts with "nph-" it won't be parsed. Also, change the glob "/some/path/*" to the path where you want to look for files.
[/quote]
精彩评论