Skip to content

Instantly share code, notes, and snippets.

@typester
Created February 14, 2011 11:51
Show Gist options
  • Save typester/825766 to your computer and use it in GitHub Desktop.
Save typester/825766 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use MyApp;
my $app = MyApp->new;
$app->setup;
$app->handler;
package MyApp;
use Ark;
__PACKAGE__->meta->make_immutable;
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