Skip to content

Commit

Permalink
made Mojo::IOLoop examples a little more versatile
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 24, 2012
1 parent f9580c0 commit a584a86
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
3 changes: 3 additions & 0 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -550,6 +550,9 @@ object from everywhere inside the process.
Start the loop, this will block until C<stop> is called or no events are
being watched anymore.
# Start loop only if it is not running already
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<stop>
Mojo::IOLoop->stop;
Expand Down
38 changes: 14 additions & 24 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -602,15 +602,15 @@ Mojo::UserAgent - Non-blocking I/O HTTP 1.1 and WebSocket user agent
# WebSocket request
$ua->websocket('ws://websockets.org:8787' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(finish => sub { say 'WebSocket closed.' });
$tx->on(message => sub {
my ($tx, $message) = @_;
say $message;
say "WebSocket message: $message";
$tx->finish;
});
$tx->send('hi there!');
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head1 DESCRIPTION
Expand Down Expand Up @@ -854,9 +854,8 @@ append a callback to perform requests non-blocking.
$ua->delete('http://kraih.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<detect_proxy>
Expand All @@ -878,9 +877,8 @@ append a callback to perform requests non-blocking.
$ua->get('http://kraih.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<head>
Expand All @@ -894,9 +892,8 @@ append a callback to perform requests non-blocking.
$ua->head('http://kraih.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<need_proxy>
Expand All @@ -916,9 +913,8 @@ append a callback to perform requests non-blocking.
$ua->options('http://kraih.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<patch>
Expand All @@ -932,9 +928,8 @@ append a callback to perform requests non-blocking.
$ua->patch('http://kraih.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<post>
Expand All @@ -948,9 +943,8 @@ append a callback to perform requests non-blocking.
$ua->post('http://kraih.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<post_form>
Expand All @@ -964,9 +958,8 @@ perform requests non-blocking.
$ua->post_form('http://kraih.com' => {q => 'test'} => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<put>
Expand All @@ -980,9 +973,8 @@ append a callback to perform requests non-blocking.
$ua->put('http://kraih.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<start>
Expand All @@ -994,9 +986,8 @@ transactions non-blocking.
$ua->start($tx => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 C<websocket>
Expand All @@ -1007,14 +998,13 @@ the exact same arguments as L<Mojo::UserAgent::Transactor/"websocket">.
$ua->websocket('ws://localhost:3000/echo' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(message => sub {
my ($tx, $message) = @_;
say "$message\n";
say $message;
});
$tx->send('Hi!');
});
Mojo::IOLoop->start;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head1 DEBUGGING
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -898,8 +898,8 @@ you can keep many parallel connections active at the same time.
# Start a bunch of parallel crawlers sharing the same user agent
$crawl->($_) for 1 .. 3;

# Start reactor
Mojo::IOLoop->start;
# Start reactor if necessary
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

You can take full control of the L<Mojo::IOLoop> reactor.

Expand Down

0 comments on commit a584a86

Please sign in to comment.