AnyEvent::Mojo::Server - Run Mojo apps using AnyEvent framework
version 0.8003
use strict; use warnings; use AnyEvent; use AnyEvent::Mojo::Server; my $server = AnyEvent::Mojo::Server->new; $server->port(3456)->listen_queue_size(10); $server->max_keep_alive_requests(100)->keep_alive_timeout(3); $server->handler_cb(sub { my ($self, $tx) = @_; # Do whatever you want here $you_mojo_app->handler($tx); # Cool stats $tx->res->headers( 'X-AnyEvent-Mojo-Request-Count' => $server->request_count ); return $tx; }); # Start it up and keep it running $server->run # integrate with other AnyEvent stuff $server->listen # other AnyEvent stuff here # Run the loop AnyEvent->condvar->recv; # Advanced usage: use your own Connection class $server->connection_class('MyConnectionClass');
This is a first beta release. The interface should not change in a backwards incompatible way until version 1.0.
Basic HTTP/1.0 and HTTP/1.1 works, including keep-alives.
Support for 100-Continue is still missing.
This module allows you to integrate Mojo applications with the AnyEvent framework. For example, you can run a web interface for a long-lived AnyEvent daemon.
The AnyEvent::Mojo::Server extends the Mojo::Server class.
To use you need to create a AnyEvent::Mojo::Server object. You can set the port with the port() method.
port()
Then set the request callback with the Mojo::Server method, handler_cb().
handler_cb()
This callback will be called on every request. The first parameter is the AnyEvent::Mojo::Server server object itself, and the second parameter is a Mojo::Transaction.
The code should build the response and return.
Optionally, you can pause the current request and resume it later. This is useful if you need to request information from a remote service to answer the current request. You pause the current Mojo request, ask the information asyncronously, and resume when the answer arrives. In the meantime, you can keep answering other requests.
Creates a new AnyEvent::Server::Mojo instance.
Accepts an hash/hashref with options. All the following methods can be used as options. Check the method documentation below for valid values and an explanation about each one.
Returns a AnyEvent::Server::Mojo object.
The hostname or IP address to which the server will bind to. By default it will bind to all interfaces.
Port where the server will listen on. Defaults to 3000.
Defines the size of the listening queue. Defaults to SOMAXCONN .
SOMAXCONN
Use
perl -MSocket -e 'print Socket::SOMAXCONN,"\n"'
to discover the default for your operating system.
Number of requests that each connection will allow in keep-alive mode.
Use 0 for unlimited requests. Default is 100 requests.
Number of seconds (can be fractional) that the server lets open connections stay idle.
Default is 5 seconds.
Returns the number of requests the server has answered since it started.
Sets the class name that will be used to process each connection.
Defaults to " AnyEvent::Mojo::Server::Connection ".
Starts the listening socket.
Returns nothing.
Starts the listening socket and kickstarts the " AnyEvent " runloop.
Closes the listening socket and stops the runloop initiated by a call to run() .
run()
Called after the listening socket is started. You can override this method on your " AnyEvent::Mojo::Server " subclasses to setup other components.
The default startup_banner is empty.
startup_banner
Pedro Melo, <melo at cpan.org>
<melo at cpan.org>
Copyright 2008-2009 Pedro Melo.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
3 POD Errors
The following errors were encountered while parsing the POD:
L<> starts or ends with whitespace
To install AnyEvent::Mojo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent::Mojo
CPAN shell
perl -MCPAN -e shell install AnyEvent::Mojo
For more information on module installation, please visit the detailed CPAN module installation guide.