NAME
Plack::App::WebSocket::Connection - WebSocket connection for Plack::App::WebSocket
SYNOPSIS
my $app = Plack::App::WebSocket->new(on_establish => sub {
my $connection = shift;
$connection->on(message => sub {
my ($connection, $message) = @_;
warn "Received: $message\n";
if($message eq "quit") {
$connection->close();
}
});
$connection->on(finish => sub {
warn "Closed\n";
undef $connection;
});
$connection->send("Message to the client");
});
DESCRIPTION
Plack::App::WebSocket::Connection is an object representing a WebSocket connection to a client. It is created by Plack::App::WebSocket internally and given to you in on_establish callback function.
OBJECT METHODS
@unregister = $connection->on($event => $handler, $event2 => $handler2, ...)
Register a callback function to a particular event. You can register multiple callbacks to the same event. You can register multiple callbacks by a single call to on method.
$event is a string and $handler is a subroutine reference.
Possible value for $event is:
"message"-
$handler->($connection, $message, $unregister)$handleris called for each message received via the$connection. Argument$connectionis the Plack::App::WebSocket::Connection object, and$messageis a non-decoded byte string of the received message.$unregisteris a subroutine reference. When you invoke it, it removes the handler from the$connection, so that it'll be never called again. See below for an example. "finish"(alias:"close")-
$handler->($connection)$handleris called when the$connectionis closed. Argument$connectionis the Plack::App::WebSocket::Connection object.
on method returns list of subroutine references that unregister the $handlers just registered.
In scalar context, it returns the first subroutine reference.
Example of unregistering:
my $unregister;
$unregister = $connection->on( message => sub {
my( $conn, $message ) = @_;
$conn->send( "This will only be sent once" );
$unregister->();
});
# could also be written as
$connection->on( message => sub {
my( $conn, $message, $unregister ) = @_;
$conn->send( "This will only be sent once" );
$unregister->();
});
$connection->send($message)
Send a message via $connection.
$message should be a UTF-8 encoded string.
$connection->close()
Close the WebSocket $connection.
AUTHOR
Toshio Ito, <toshioito at cpan.org>