AnyEvent-WebSocket-Server
view release on metacpan or search on metacpan
lib/AnyEvent/WebSocket/Server.pm view on Meta::CPAN
The constructor.
Fields in C<%args> are:
=over
=item C<handshake> => CODE (optional)
A subroutine reference to customize the WebSocket handshake process.
You can use this option to validate and preprocess the handshake request and customize the handshake response.
For each request, the handshake code is called like
($response, @other_results) = $handshake->($request, $default_response)
where C<$request> is a L<Protocol::WebSocket::Request> object,
and C<$default_response> is a L<Protocol::WebSocket::Response> object.
The C<$handshake> code must return C<$response>. C<@other_results> are optional.
The return value C<$response> is the handshake response returned to the client.
lib/AnyEvent/WebSocket/Server.pm view on Meta::CPAN
Those C<@other_results> can be obtained later from the condition variable of C<establish()> method.
If you throw an exception from C<$handshake> code, we think you reject the C<$request>.
In this case, the condition variable of C<establish()> method croaks.
=item C<validator> => CODE (optional)
B<< This option is only for backward compatibility. Use C<handshake> option instead. If C<handshake> option is specified, this option is ignored. >>
A subroutine reference to validate the incoming WebSocket request.
If omitted, it accepts the request.
The validator is called like
@other_results = $validator->($request)
where C<$request> is a L<Protocol::WebSocket::Request> object.
If you reject the C<$request>, throw an exception.
t/testlib/PSGI.pm view on Meta::CPAN
sub _make_app {
my ($ws_server) = @_;
return sub {
my ($env) = @_;
return sub {
my $responder = shift;
note("server enters streaming callback");
$cv_server_finish->begin;
$ws_server->establish_psgi($env)->cb(sub {
my $cv = shift;
my ($conn, $validate_str) = try { $cv->recv };
if(!$conn) {
note("server connection error");
$responder->([400, ['Content-Type' => 'text/plain', 'Connection' => 'close'], ['invalid request']]);
$cv_server_finish->end;
return;
}
note("server websocket established");
is($validate_str, "validated", "validator should be called");
$conn->on(each_message => sub {
my ($conn, $message) = @_;
$conn->send($message);
});
$conn->on(finish => sub {
undef $conn;
$cv_server_finish->end;
## release the session held by the PSGI server.
$responder->([200, ['Content-Type' => 'text/plain', 'Connection' => 'close'], ['dummy response']]);
});
t/testlib/PSGI.pm view on Meta::CPAN
set_timeout;
testlib::ConnConfig->for_all_ok_conn_configs(sub {
my ($cconfig) = @_;
if(!$cconfig->is_plain_socket_transport($cconfig)) {
my $label = $cconfig->label;
plan skip_all => "Case '$label' does not use a plain socket. Too unrealistic to test. Skipped.";
}
my $client = AnyEvent::WebSocket::Client->new($cconfig->client_args);
my $server = AnyEvent::WebSocket::Server->new(
$cconfig->server_args,
validator => sub { return "validated" }
);
my $port = empty_port();
note("empty port: $port");
my $server_guard = $server_runner->($port, _make_app($server));
_test_case "normal echo", sub {
my $conn = $client->connect($cconfig->connect_url($port, "/"))->recv;
note("client connection established");
my @received = ();
$cv_server_finish->begin;
( run in 0.863 second using v1.01-cache-2.11-cpan-a5abf4f5562 )