Skip to content

Commit

Permalink
It is possible to pass class name and method instead of inline handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Apr 9, 2011
1 parent 31817b4 commit f53a7cd
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
78 changes: 75 additions & 3 deletions lib/Plack/Middleware/SocketIO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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__
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions t/lib/Handler.pm
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 21 additions & 0 deletions t/middleware.t
Original file line number Diff line number Diff line change
@@ -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.