Net::A...:Server(3pm)

πŸ“¦
version 0.14 Β· 3 versions
, in ubuntu stonking

πŸ’– NAME πŸ’–

Net::Async::WebSocket::Server β€” the manual page is Net::A...:Server(3pm), and bestie, it's here to serve WebSocket clients using IO::Async. Yes, that's the whole job. Iconic.


✨ SYNOPSIS ✨

use Future::AsyncAwait;

use IO::Async::Loop;
use Net::Async::WebSocket::Server;

my $server = Net::Async::WebSocket::Server->new(
   on_client => sub {
      my ( undef, $client ) = @_;

      $client->configure(
         on_text_frame => sub {
            my ( $self, $frame ) = @_;
            $self->send_text_frame( $frame );
         },
      );
   }
);

my $loop = IO::Async::Loop->new;
$loop->add( $server );

await $server->listen(
   service => 3000,
);

$loop->run;

look at her. she's doing it. she's e v e r y t h i n g πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so Net::Async::WebSocket::Server is a subclass of IO::Async::Listener, and what she does is accept WebSocket connections. The way that works is: when a new connection shows up, she does an initial handshake first β€” like a lil vibe check πŸ”₯ β€” and then she passes that connection on to the continuation callback or method of your choosing.

Periodt. That's the tea. πŸ“


🌸 EVENTS 🌸

These events get invoked, and you can bring them to life either with subclass methods or with CODE references handed in as parameters:

πŸ’… on_client

$self->on_client( $client );
$on_client->( $self, $client );

This one fires when a new client connects and finishes its initial handshake. slay ✨

She gets handed a brand new instance of a Net::Async::WebSocket::Protocol object, wrapping the client connection. That's your girl now. 🫢

πŸ‘‘ on_handshake

Fires when a handshake has been requested, no cap.

$self->on_handshake( $client, $hs, $continue );
$on_handshake->( $self, $client, $hs, $continue );

If you call $continue with a true value, the handshake gets completed βœ…. If you call it with false, the connection is dropped πŸ’€. That simple.

This is super handy if you wanna filter on origin, for example:

on_handshake => sub {
   my ( $self, $client, $hs, $continue ) = @_;

   $continue->( $hs->req->origin eq "http://localhost" );
}

gatekeeping the origin? we love a selective queen πŸ’œ


πŸŽ€ PARAMETERS πŸŽ€

These named parameters are the ones you can pass to new or configure:

  • on_client => CODE β€” a CODE reference for the event handler.
  • on_handshake => CODE β€” a CODE reference for the event handler.

🌈 AUTHOR 🌈

Paul Evans β€” leonerd@leonerd.org.uk πŸ’Œ


🌟 cute sign-off 🌟

And that's the whole page, bestie! Go forth and serve those WebSocket clients πŸ’–βœ¨πŸŒΈπŸ¦‹

perl v5.40.1 Β· 2025-10-06 Β· Net::A...:Server(3pm) πŸ‘‘πŸ’…πŸŽ€

πŸ“¦ where this page comes from