Skip to content

Adds possibility to provide custom dispather #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/Mojo/WebSocketProxy/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ sub add_backend {
return;
}

sub add_dispatcher {
my ($self, $dispatcher) = @_;

die 'Custom dipatcher should have dispatch method'
unless blessed($dispatcher) and $dispatcher->can('dispatch');

$self->{dispatcher} = $dispatcher;

return;
}

1;

__END__
Expand Down
39 changes: 27 additions & 12 deletions lib/Mojo/WebSocketProxy/Dispatcher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,24 @@ sub on_message {
$c->send({json => $err}, $req_storage);
return $c->_run_hooks($config->{after_dispatch} || [])->retain;
}

my $action = $c->dispatch($args) or do {
my $err = $c->wsp_error('error', UnrecognisedRequest => 'Unrecognised request');
$c->send({json => $err}, $req_storage);
return $c->_run_hooks($config->{after_dispatch} || [])->retain;
};

@{$req_storage}{keys %$action} = (values %$action);
$req_storage->{method} = $req_storage->{name};


# main processing pipeline
my $f = $c->before_forward($req_storage)->transform(
my $f = $c->dispatch($args)->then(sub {
my ($action) = @_;
unless ($action) {
my $err = $c->wsp_error('error', UnrecognisedRequest => 'Unrecognised request');
$c->send({json => $err}, $req_storage);
return $c->_run_hooks($config->{after_dispatch} || [])->then(sub {
return Future::Mojo->fail('UNRECOGNISED_REQUEST');
});
}

@{$req_storage}{keys %$action} = (values %$action);
$req_storage->{method} = $req_storage->{name};
return Future::Mojo->done;
})->then(sub {
return $c->before_forward($req_storage);
})->transform(
done => sub {
# Note that we completely ignore the return value of ->before_forward here.
return $req_storage->{instead_of_forward}->($c, $req_storage) if $req_storage->{instead_of_forward};
Expand Down Expand Up @@ -170,6 +176,11 @@ sub on_message {
}
)->on_fail(
sub {
my ($err) = @_;

# we don't want to flood logs if clients sent incorrect requests.
return if $err and $err eq 'UNRECOGNISED_REQUEST';

$c->app->log->error("An error occurred handling on_message. Error @_");
})->retain;
}
Expand Down Expand Up @@ -221,12 +232,16 @@ sub dispatch {
my $log = $c->app->log;
$log->debug("websocket got json " . $c->dumper($args));

if ($c->wsp_config->{dispatcher}) {
return Future::Mojo->wrap($c->wsp_config->{dispatcher}->dipatch($actions));
}

my ($action) =
sort { $a->{order} <=> $b->{order} }
grep { defined }
map { $c->wsp_config->{actions}->{$_} } keys %$args;

return $action;
return Future::Mojo->done($action);
}

sub forward {
Expand Down
9 changes: 7 additions & 2 deletions lib/Mojolicious/Plugin/WebSocketProxy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package Mojolicious::Plugin::WebSocketProxy;
use strict;
use warnings;

use Scalar::Util qw(blessed);

use Mojo::Base 'Mojolicious::Plugin';
use Mojo::WebSocketProxy::Backend;
use Mojo::WebSocketProxy::Config;
Expand Down Expand Up @@ -55,11 +57,14 @@ sub register {
$_->websocket('/')->to('Dispatcher#open_connection', namespace => 'Mojo::WebSocketProxy');
}

my $actions = delete $config->{actions};
my $dispatcher_config = Mojo::WebSocketProxy::Config->new;
$dispatcher_config->init($config);
# if custom dispatcher is provided, we will use it
if (my $dispatcher = delete $config->{dispatcher}) {
$dispatcher_config->add_dispatcher($dispatcher);
} elsif (my $actions = delete $config->{actions}) {
die 'Invalid format of actions parameter' unless ref $actions eq 'ARRAY';

if (ref $actions eq 'ARRAY') {
for (my $i = 0; $i < @$actions; $i++) {
$dispatcher_config->add_action($actions->[$i], $i);
}
Expand Down