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_text_frame => sub {
             my ( $self, $frame ) = @_;
             $self->send_text_frame( $frame );
          },
       );
    }
 );

 my $loop = IO::Async::Loop->new;
 $loop->add( $server );

 $server->listen(
    service => 3000,
 )->get;

 $loop->run;

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.

EVENTS

The following events are invoked, either using subclass methods or CODE references in parameters:

on_client

   $self->on_client( $client )
   $on_client->( $self, $client )

Invoked when a new client connects and completes its initial handshake.

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

on_handshake

Invoked when a handshake has been requested.

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

Calling $continue 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, $continue ) = @_;

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

PARAMETERS

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

on_client => CODE
on_handshake => CODE

CODE references for event handlers.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>