The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Net::Async::WebSocket::Client - connect to a WebSocket server using IO::Async

SYNOPSIS

use Future::AsyncAwait;

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

my $client = Net::Async::WebSocket::Client->new(
   on_text_frame => sub {
      my ( $self, $frame ) = @_;
      print $frame;
   },
);

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

await $client->connect( url => "ws://$HOST:$PORT/" );

await $client->send_text_frame( "Hello, world!\n" );

$loop->run;

DESCRIPTION

This subclass of Net::Async::WebSocket::Protocol connects to a WebSocket server to establish a WebSocket connection for passing frames.

METHODS

The following methods documented in an await expression return Future instances.

connect

await $self->connect( %params );

Connect to a WebSocket server. Takes the following named parameters:

url => STRING

URL to provide to WebSocket handshake. This is also used to infer the host and service name (port number) if not otherwise supplied.

req => Protocol::WebSocket::Request

Optional. If provided, gives the Protocol::WebSocket::Request instance used for performing the handshake.

The returned Future returns the client instance itself, making it useful in chaining constructors.

connect (void)

$self->connect( %params );

When not returning a Future, the following additional parameters provide continuations:

on_connected => CODE

CODE reference to invoke when the handshaking is complete.

connect_handle

await $client->connect_handle( $handle, %params );

Sets the read and write handles to the IO reference given, then performs the initial handshake using the parameters given. These are as for connect.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>