- 论坛徽章:
- 42
|
参考feersum的chat.feersum例子,这个基本可以了.
需要把handle和timer定义为全局变量.
feersum的文档好少啊.- #!perl
- use warnings;
- use strict;
- use EV;
- use Scalar::Util qw/weaken/;
- my $clients = 0;
- my @handles;
- my @timers;
- my @html_hdrs = (
- 'Content-Type' => 'text/html; charset=UTF-8',
- 'Cache-Control' => 'no-cache, no-store, private',
- 'Pragma' => 'no-cache',
- );
- sub start_stream {
- my ($r, $client) = @_;
- my $w = $r->start_streaming(200, \@html_hdrs);
- $handles[$client] = $w;
- weaken $w;
- $timers[$client] = EV::timer 1,1,sub {
- eval{
- $w->write(' keep-alive') if $w;
- }
- if($@) {
- $w->close;
- undef $timers[$client];
- }
- };
- $w->write(<<EOH);
- <html>
- <head></head>
- <body>
- <p>Hello! (connection $client.)</p>
- EOH
- }
- my $app = sub {
- my $r = shift;
- my $env = $r->env;
- my $path = $env->{PATH_INFO};
- $clients++;
-
- start_stream($r,$clients);
-
- };
复制代码 |
|