Plack:...nection(3pm)

πŸ“¦ version 0.08, in opensuse-leap 16.0

🌸 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