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::Frame

SYNOPSIS

#Never instantiate Net::WebSocket::Frame directly;
#always call new() on a subclass:
my $frame = Net::WebSocket::Frame::text->new(
    fin => 0,                   #to start a fragmented message
    rsv => 0b11,                #RSV2 and RSV3 are on
    mask => "\x01\x02\x03\x04   #clients MUST include; servers MUST NOT
    payload => \'Woot!',
);

$frame->get_fin();
$frame->get_mask_bytes();
$frame->get_payload();

$frame->set_rsv();
$frame->get_rsv();

$frame->to_bytes();     #for sending over the wire

DESCRIPTION

This is the base class for all frame objects. The interface as described above should be fairly straightforward.

EXPERIMENTAL: CUSTOM FRAME CLASSES

You can have custom frame classes, e.g., to support WebSocket extensions that use custom frame opcodes. RFC 6455 allocates opcodes 3-7 for data frames and 11-15 (0xb - 0xf) for control frames.

The best way to do this is to subclass either Net::WebSocket::Base::DataFrame or Net::WebSocket::Base::ControlFrame, depending on what kind of frame you’re dealing with.

An example of such a class is below:

package My::Custom::Frame::booya;

use strict;
use warnings;

use parent qw( Net::WebSocket::Base::DataFrame );

use constant get_opcode => 3;

use constant get_type => 'booya';

Note that Net::WebSocket::Parser still won’t know how to handle such a custom frame, so if you intend to receive custom frames as part of messages, you’ll also need to create a custom base class of this class, then also subclass Net::WebSocket::Parser. You may additionally want to subclass Net::WebSocket::Streamer::Server (or -::Client) if you do streaming.

NOTE: THIS IS LARGELY UNTESTED. I’m not familiar with any application that actually requires this feature. The permessage-deflate extension seems to be the only one that has much widespread web browser support.