如何在服务器推送 Perl CGI 程序中发送多个图像?
我是 Perl CGI 等方面的初学者。我正在用一段 Perl 代码尝试服务器推送概念。它应该每三秒向客户端发送一个 jpeg 图像。
不幸的是,似乎没有任何作用。有人可以帮助找出问题吗?
这是代码:
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;
}
编辑:添加关闭 i/o 缓冲,添加“use strict”和“@file_list”,“$file”设为本地
I am a beginner in Perl CGI etc. I was experimenting with server-push concept with a piece of Perl code. It is supposed 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
冲洗输出。
最有可能的是,服务器将响应保留在缓冲区中。您可能希望在每次
print
或autoflush STDOUT
后执行一次fflush(STDOUT)
操作。看看 http://www.abiglime.com/webmaster/articles/ cgi/032498.htm
[引用]
[/引用]
Flush the output.
Most probably, the server is keeping the response in the buffer. You may want to do
fflush(STDOUT)
after everyprint
orautoflush STDOUT
once.Have a look at http://www.abiglime.com/webmaster/articles/cgi/032498.htm
[quote]
[/quote]