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_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;
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 events are invoked, either using subclass methods or CODE references in parameters:
$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.
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.
$continue
This is useful for filtering on origin, for example:
on_handshake => sub { my ( $self, $client, $hs, $continue ) = @_; $continue->( $hs->req->origin eq "http://localhost" ); }
The following named parameters may be passed to new or configure:
new
configure
CODE references for event handlers.
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.