The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::Async::WebSocket::Server - serve WebSocket clients using IO::Async

SYNOPSIS

 use IO::Async::Loop;
 use Net::Async::WebSocket::Server;
 
 my $server = Net::Async::WebSocket::Server->new(
    on_client => sub {
       my ( undef, $client ) = @_;
 
       $client->configure(
          on_frame => sub {
             my ( $self, $frame ) = @_;
             $self->send_frame( $frame );
          },
       );
    }
 );
 
 my $loop = IO::Async::Loop->new;
 $loop->add( $server );
 
 $server->listen(
    service => 3000,
 
    on_listen_error => sub { die "Cannot listen - $_[-1]" },
    on_resolve_error => sub { die "Cannot resolve - $_[-1]" },
 );
 
 $loop->loop_forever;

DESCRIPTION

This subclass of IO::Async::Listener accepts WebSocket connections. When a new connection arrives it will perform an initial handshake, and then pass the connection on to the continuation callback or method.

PARAMETERS

The following named parameters may be passed to new or configure:

on_client => CODE

A callback that is invoked whenever a new client connects and completes its inital handshake.

 $on_client->( $self, $client )

It will be passed a new instance of a Net::Async::WebSocket::Protocol object, wrapping the client connection.

on_handshake => CODE

A callback that is invoked when a handshake has been requested.

 $on_handshake->( $self, $client, $hs, $continuation )

Calling $continuation with a true value will complete the handshake, false will drop the connection.

This is useful for filtering on origin, for example:

 on_handshake => sub {
    my ( $self, $client, $hs, $continuation ) = @_;

    $continuation->( $hs->req->origin eq "http://localhost" );
 }

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>