Skip to content

Instantly share code, notes, and snippets.

@hiroaki
Created February 23, 2012 17:23
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 hiroaki/1893896 to your computer and use it in GitHub Desktop.
Save hiroaki/1893896 to your computer and use it in GitHub Desktop.
A sample of streaming by Mojolicious
#!/usr/bin/env perl
=pod
A sample of streaming.
This sends dynamic contents on "Transfer-Encoding: chunked".
See also the source: t/mojolicious/longpolling_lite_app.t
=cut
use Mojolicious::Lite;
get '/' => sub {
my $c = shift;
my $i = 0;
my $drain;
$drain = sub {
my $me = shift;
return $me->finish if( $i >= 10 );
sleep 1;
my $chunk = sprintf '%d', $i++;
$me->write_chunk($chunk, $drain);
};
$c->$drain;
};
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment