Plack:...nection(3pm)
πΈ NAME πΈ
okay bestie, this is the User Contributed Perl Documentation page for Plack::App::WebSocket::Connection β the one filed under Plack:...nection(3pm) β and it's literally the WebSocket connection for Plack::App::WebSocket. no cap π
β¨ 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's created by Plack::App::WebSocket internally and handed to you in the on_establish callback function. so that's where you meet it β it gets delivered to your callback like a little gift π
π 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 the on method. one call, multiple event/handler pairs β icon behavior β¨
$event is a string and $handler is a subroutine reference.
Possible value for $event is:
"message"β signature:$handler->($connection, $message, $unregister)
$handler is called for each message received via the $connection. Argument $connection is the Plack::App::WebSocket::Connection object, and $message is a non-decoded byte string of the received message. non-decoded byte string, okay? not a decoded text string β keep that in mind π«Ά
$unregister is 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") β signature:$handler->($connection)
$handler is called when the $connection is closed. Argument $connection is the Plack::App::WebSocket::Connection object.
The on method returns a 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. UTF-8 encoded, bestie β not raw vibes π
π $connection->close() π
Close the WebSocket $connection. that's it, that's the whole close β dramatic exit optional π
π«Ά AUTHOR π«Ά
Toshio Ito, "<toshioito at cpan.org>" author icon behavior π
mwah π that's the whole page, bestie! This one ships with perl v5.42.0, dated 2020-05-10, and it's the Plack:...nection(3pm) manual. bye π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Plack-App-WebSocket 0.08-bp160.1.10 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms