Plack::App::Net::Async::WebSocket
- serve WebSocket clients using IO::Async
over PSGI
# app.psgi
use Plack::App::Net::Async::WebSocket;
my $app = Plack::App::Net::Async::WebSocket->new(
on_handshake => sub {
my ( $websocket ) = @_;
$websocket->sent_frame( 'Hello' );
},
on_frame => sub {
my ( $websocket, $frame ) = @_;
$websocket->send_frame( $frame ); # echo
},
on_closed => sub {
my ( $websocket ) = @_;
}
)->to_app;
# using Plack::Builder
use Plack::Builder;
use Plack::App::Net::Async::WebSocket;
builder {
mount '/websocket' => Plack::App::Net::Async::WebSocket->new(
on_frame => sub {
my ( $websocket, $frame ) = @_;
$websocket->send_frame( $frame ); # echo
},
);
};
This subclass of Plack::Component accepts WebSocket connections. When a new connection arrives it will perform an initial handshake and take control over existing connection.
The following named parameters may be passed to new
:
- on_handshake => CODE
-
A CODE reference for when a new connection has been handshaked. This parameter is optional
- on_frame => CODE
-
A CODE reference for when a frame is received
- on_close => CODE
-
This parameter is optional
- on_error => CODE
-
This parameter is optional
Sends a frame to the peer containing the given string. The arguments are passed to Protocol::WebSocket::Frame's new
method.
Net::Async::WebSocket - WebSocket server using IO::Async
Plack::Handler::Net::Async::HTTP::Server - HTTP handler for Plack using Net::Async::HTTP::Server
Paweł Feruś <null@mindc.net>