Skip to content

Instantly share code, notes, and snippets.

@iizukanao
Forked from typester/app.psgi
Created February 14, 2011 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iizukanao/825825 to your computer and use it in GitHub Desktop.
Save iizukanao/825825 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;
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; $t = AnyEvent->timer(
after => 1,
interval => 1,
cb => sub {
eval {
$r->write('Hello ' . ++$i. "\n");
if (10 == $i) {
undef $t;
$r->close;
}
1;
} or do {
if ( $! =~ /Broken pipe/ ) {
print "client disconnected\n";
} else {
print "error: $@\n";
}
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