Skip to content
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

Pass handshake's @other_results to on_establish #5

Merged
merged 3 commits into from Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions lib/Plack/App/WebSocket.pm
Expand Up @@ -72,18 +72,19 @@ sub call {
my $responder = shift;
$cv_conn->cb(sub {
my ($cv_conn) = @_;
my ($conn, $error) = try {
(scalar($cv_conn->recv), undef);
my ($conn, @handshake_results, $error);
try {
($conn, @handshake_results) = $cv_conn->recv;
}catch {
(undef, $_[0]);
$error = $_[0];
};
if(!$conn) {
$env->{$ERROR_ENV} = "invalid request";
$env->{"plack.app.websocket.error.handshake"} = $error;
_respond_via($responder, $self->{on_error}->($env));
return;
}
$self->{on_establish}->(Plack::App::WebSocket::Connection->new($conn, $responder), $env);
$self->{on_establish}->(Plack::App::WebSocket::Connection->new($conn, $responder), $env, \@handshake_results);
});
};
}
Expand Down Expand Up @@ -112,8 +113,9 @@ Plack::App::WebSocket - WebSocket server as a PSGI application
["Error: " . $env->{"plack.app.websocket.error"}]];
},
on_establish => sub {
my $conn = shift; ## Plack::App::WebSocket::Connection object
my $env = shift; ## PSGI env
my $conn = shift; ## Plack::App::WebSocket::Connection object
my $env = shift; ## PSGI env
my $hs_res = shift; ## extra results from the handshake callback
$conn->on(
message => sub {
my ($conn, $msg) = @_;
Expand Down Expand Up @@ -172,10 +174,14 @@ WebSocket connection to a client.

The code is called like

$code->($connection, $psgi_env)
$code->($connection, $psgi_env, \@handshake_results)

where C<$connection> is a L<Plack::App::WebSocket::Connection> object
and C<$psgi_env> is the PSGI environment object for the connection request.
where C<$connection> is a L<Plack::App::WebSocket::Connection> object,
C<$psgi_env> is the PSGI environment object for the connection request,
and C<\@handshake_results> are extra results from the backend
L<AnyEvent::WebSocket::Server> instance's handshake callback (which can
be defined by passing a configured L<AnyEvent::WebSocket::Server> to
the C<websocket_server> constructor parameter).
You can use the C<$connection> to communicate with the client.

Make sure you keep C<$connection> object as long as you need it.
Expand Down
33 changes: 33 additions & 0 deletions t/testlib/CustomServer.pm
Expand Up @@ -6,6 +6,7 @@ use testlib::Util qw(set_timeout run_server);

use Plack::App::WebSocket;
use AnyEvent::WebSocket::Server;
use AnyEvent::WebSocket::Client;
use AnyEvent;
use AnyEvent::Handle;
use Protocol::WebSocket::Handshake::Client;
Expand Down Expand Up @@ -39,6 +40,37 @@ sub _get_raw_handshake_response {
return $response_cv;
}

sub test_handshake_other_results {
my ($server_runner) = @_;
my @got_others = ();
my $app = Plack::App::WebSocket->new(
websocket_server => AnyEvent::WebSocket::Server->new(
handshake => sub {
my ($req, $res) = @_;
return ($res, 1, 2, 3);
}
),
on_establish => sub {
my ($conn, $env, $others) = @_;
push @got_others, $others;
undef $conn;
},
on_error => sub {
fail("unexpected error happened.");
}
);
my ($port, $server_guard) = run_server($server_runner, $app->to_app);
my $client = AnyEvent::WebSocket::Client->new;
my $conn = $client->connect("ws://127.0.0.1:$port/")->recv;
my $finish_cv = AnyEvent->condvar;
$conn->on(finish => sub {
note("client finished");
$finish_cv->send;
});
$finish_cv->recv;
is_deeply(\@got_others, [[1,2,3]], 'handshake other_results should be obtained from on_establish callback');
}

sub run_tests {
my ($server_runner) = @_;
set_timeout;
Expand Down Expand Up @@ -90,6 +122,7 @@ sub run_tests {
is $got_error_env[0]{"plack.app.websocket.error"}, "invalid request";
like $got_error_env[0]{"plack.app.websocket.error.handshake"}, qr/This is user-defined exception/;
}
test_handshake_other_results($server_runner);
}

1;