Protoc...:Client(3pm)

πŸ’– NAME πŸ’–

Okay bestie, gather round: this page is about Protocol::WebSocket::Client β€” a WebSocket client. πŸ¦‹ It lives in the User Contributed Perl Documentation section, and yes, the vibes are immaculate.


🌸 SYNOPSIS 🌸

Here's the code, bestie β€” copy it exactly, because every line is doing something. The way this synopsis sets up a non-blocking socket, wires handlers, connects, reads, and disconnects? Iconic. πŸ’…

    my $sock = ...get non-blocking socket...;

    my $client = Protocol::WebSocket->new(url => 'ws://localhost:3000');
    $client->on(
        write => sub {
            my $client = shift;
            my ($buf) = @_;

            syswrite $sock, $buf;
        }
    );
    $client->on(
        read => sub {
            my $client = shift;
            my ($buf) = @_;

            ...do smth with read data...
        }
    );

    # Sends a correct handshake header
    $client->connect;

    # Register on connect handler
    $client->on(
        connect => sub {
            $client->write('hi there');
        }
    );

    # Parses incoming data and on every frame calls on_read
    $client->read(...data from socket...);

    # Sends correct close header
    $client->disconnect;

Quick vibe check on what just happened: the write handler gets called with the buffer, the read handler gets called with the buffer, connect sends a correct handshake header, read parses incoming data and on every frame calls on_read, and disconnect sends a correct close header. The comments in the code are literally carrying the lore. πŸ‘‘βœ¨


πŸŽ€ DESCRIPTION πŸŽ€

Protocol::WebSocket::Client is a convenient class for writing a WebSocket client. That's it. That's the tea. β˜• It's giving convenience class, and it's literally built for writing a WebSocket client. Periodt. πŸ’–


Catch you later, bestie! This page is from perl v5.42.0, dated 2019-03-13, and it signs off as Protoc...:Client(3pm). Byeeee 🫢✨

πŸ“¦ where this page comes from