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::WebSocket::Handshake::Client

SYNOPSIS

my $hsk = Net::WebSocket::Handshake::Client->new(

    #required
    uri => 'ws://haha.test',

    #optional, to imitate a web client
    origin => ..,

    #optional, base 64 .. auto-created if not given
    key => '..',

    #optional
    subprotocols => [ 'echo', 'haha' ],

    #optional
    extensions => \@extension_objects,
);

print $hsk->to_string();

$hsk->consume_headers( NAME1 => VALUE1, .. );

DESCRIPTION

This class implements WebSocket handshake logic for a client. It handles the basics of handshaking and, optionally, subprotocol and extension negotiation.

It is a subclass of Net::WebSocket::Handshake.

METHODS

OBJ->new( %OPTS )

Returns an instance of the class; %OPTS includes the options from Net::WebSocket::Handshake as well as:

  • uri - (required) The full URI you’re connecting to.

  • origin - (optional) The HTTP Origin header’s value. Useful for imitating a web browser.

OBJ->valid_status_or_die( CODE, REASON )

Throws an exception if the given CODE isn’t the HTTP status code (101) that WebSocket requires in response to all requests. (REASON is included with the exception on error; otherwise it’s unused.)

You only need this if if you’re not using a request-parsing interface that’s compatible with HTTP::Response; otherwise, Net::WebSocket::HTTP_R’s handshake_consume_response() function will do this (and other niceties) for you.

LEGACY INTERFACE: SYNOPSIS

my $hsk = Net::WebSocket::Handshake::Client->new(

    #..same as the newer interface, except:

    #optional
    extensions => \@extension_objects,
);

print $hsk->create_header_text() . "\x0d\x0a";

#...Parse the response’s headers yourself...

#Validates the value of the “Sec-WebSocket-Accept” header;
#throws Net::WebSocket::X::BadAccept if not.
$hsk->validate_accept_or_die($accept_value);