Net::Async::WebSocket::Server - serve WebSocket clients using IO::Async
Net::Async::WebSocket::Server
IO::Async
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;
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.
The following named parameters may be passed to new or configure:
new
configure
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.
A callback that is invoked when a handshake has been requested.
$on_handshake->( $self, $hs, $continuation )
Calling $continuation with a true value will complete the handshake, false will drop the connection.
$continuation
This is useful for filtering on origin, for example:
on_handshake => sub { my ( $self, $hs, $continuation ) = @_; $continuation->( $hs->req->origin eq "http://localhost" ); }
Paul Evans <leonerd@leonerd.org.uk>
To install Net::Async::WebSocket, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::Async::WebSocket
CPAN shell
perl -MCPAN -e shell install Net::Async::WebSocket
For more information on module installation, please visit the detailed CPAN module installation guide.