Created
February 14, 2011 11:51
-
-
Save typester/825766 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use lib 'lib'; | |
use MyApp; | |
my $app = MyApp->new; | |
$app->setup; | |
$app->handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyApp; | |
use Ark; | |
__PACKAGE__->meta->make_immutable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyApp::Controller::Root; | |
use Ark 'Controller'; | |
use AnyEvent; | |
use Try::Tiny; | |
has '+namespace' => default => ''; | |
sub default :Path :Args { | |
my ($self, $c) = @_; | |
$c->res->status(404); | |
$c->res->body('404 Not Found'); | |
} | |
sub index :Path { | |
my ($self, $c) = @_; | |
$c->res->content_type('text/html; charset=utf-8'); | |
$c->res->streaming(sub { | |
my $r = shift; | |
my $i = 0; | |
my $t; | |
$r->{handle}->on_read(sub { | |
# ignore | |
delete $_[0]->{rbuf}; | |
}); | |
$r->{handle}->on_eof(sub { | |
warn "client disconnected\n"; | |
$r->close; | |
undef $t; | |
}); | |
$t = AnyEvent->timer( | |
after => 1, | |
interval => 1, | |
cb => sub { | |
my $err; | |
try { | |
$r->write('Hello ' . ++$i. "\n"); | |
} catch { | |
warn "$_\n"; | |
$err = $_; | |
}; | |
if (10 == $i or $err) { | |
undef $t; | |
$r->close; | |
} | |
}, | |
); | |
}); | |
} | |
__PACKAGE__->meta->make_immutable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment