Permalink
Browse files

It is possible to pass class name and method instead of inline handler

  • Loading branch information...
1 parent 31817b4 commit f53a7cd938a9150f64351418b89a01acb5d1d9d3 @vti committed Apr 9, 2011
Showing with 115 additions and 3 deletions.
  1. +75 −3 lib/Plack/Middleware/SocketIO.pm
  2. +19 −0 t/lib/Handler.pm
  3. +21 −0 t/middleware.t
@@ -5,20 +5,29 @@ use warnings;
use base 'Plack::Middleware';
-our $VERSION = '0.00902';
+our $VERSION = '0.00903';
-use Plack::Util::Accessor qw(resource handler);
+use Plack::Util ();
+use Plack::Util::Accessor qw(resource handler class method);
use Plack::Middleware::SocketIO::Resource;
+sub new {
+ my $self = shift->SUPER::new(@_);
+
+ $self->handler($self->_get_handler);
+
+ return $self;
+}
+
sub call {
my $self = shift;
my ($env) = @_;
my $resource = $self->resource || 'socket.io';
$resource = quotemeta $resource;
- if ($env->{PATH_INFO} =~ m{^/$resource/}) {
+ if (defined $env->{PATH_INFO} && $env->{PATH_INFO} =~ m{^/$resource/}) {
my $instance = Plack::Middleware::SocketIO::Resource->instance;
return $instance->finalize($env, $self->handler)
@@ -28,6 +37,22 @@ sub call {
return $self->app->($env);
}
+sub _get_handler {
+ my $self = shift;
+
+ return $self->handler if $self->handler;
+
+ my $class = $self->class
+ or die q{Either 'handler' or 'class' must be specified};
+ my $method = $self->method || 'run';
+
+ Plack::Util::load_class($class);
+
+ $class->new;
+
+ return sub { $class->run };
+}
+
1;
__END__
@@ -58,6 +83,14 @@ Plack::Middleware::SocketIO - Socket.IO middleware
$app;
};
+ # or
+
+ builder {
+ enable "SocketIO", class => 'MyApp::Handler', method => 'run';
+
+ $app;
+ };
+
=head1 DESCRIPTION
L<Plack::Middleware::SocketIO> is a server implmentation of SocketIO in Perl.
@@ -83,6 +116,45 @@ All the transports are supported.
For TLS/SSL a secure proxy is needed. C<stunnel> or L<App::TLSMe> is
recommended.
+=head1 CONFIGURATIONS
+
+=over 4
+
+=item handler
+
+ enable "SocketIO",
+ handler => sub {
+ my $socket = shift;
+
+ $socket->on_message(sub {
+ my $socket = shift;
+ });
+
+ $socket->send_message('hello');
+ };
+
+=item class, method
+
+ enable "SocketIO", class => 'MyHandler', method => 'run';
+
+ package MyHandler;
+
+ sub new { ... } # or use Moose, Boose, Goose, Doose
+
+ sub run {
+ my $self = shift;
+
+ return sub {
+
+ # same code as above
+ }
+ }
+
+Loads C<class> using L<Plack::Util::load_class>, creates a new object and runs
+C<run> method expecting it to return an anonymous subroutine.
+
+=back
+
=head1 DEVELOPMENT
=head2 Repository
View
@@ -0,0 +1,19 @@
+package Handler;
+
+use strict;
+use warnings;
+
+sub new {
+ my $class = shift;
+
+ return bless {}, $class;
+}
+
+sub run {
+ my $self = shift;
+
+ return sub {
+ };
+}
+
+1;
View
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+
+use lib 't/lib';
+
+use Test::More tests => 4;
+
+use_ok('Plack::Middleware::SocketIO');
+
+eval {
+ Plack::Middleware::SocketIO->new(app => sub { });
+};
+like $@ => qr/Either 'handler' or 'class' must be specified/;
+
+my $middleware =
+ Plack::Middleware::SocketIO->new(app => sub { }, handler => sub { });
+is ref($middleware->handler) => 'CODE';
+
+$middleware =
+ Plack::Middleware::SocketIO->new(app => sub { }, class => 'Handler');
+is ref($middleware->handler) => 'CODE';

0 comments on commit f53a7cd

Please sign in to comment.