NAME

WebSocket::Frame - WebSocket Frame

SYNOPSIS

use WebSocket::Frame;
# Create frame
my $frame = WebSocket::Frame->new( '123' );
$frame->to_bytes;

# Parse frames
my $frame = WebSocket::Frame->new;
$frame->append( $some_data );
$f->next; # get next message
$f->next; # get another next message

VERSION

v0.1.0

DESCRIPTION

Construct or parse a WebSocket frame.

CONSTRUCTOR

new

# same as (buffer => 'data')
WebSocket::Frame->new( 'data' );
WebSocket::Frame->new( buffer => 'data', type => 'close' );

Create a new WebSocket::Frame instance. Automatically detect if the passed data is a Perl string (UTF-8 flag) or bytes.

When called with more than one arguments, it takes the following named arguments (all of them are optional).

buffer

The payload of the frame. It can also be provided as the first argument of the "new" method.

fin

Boolean default to 1. Indicate whether this frame is the last frame of the entire message body

fin flag of the frame. fin flag must be 1 in the ending frame of fragments.

masked

Boolean default to 0.

If set to true, the frame will be masked.

opcode

Default to 1. Operation bit, which defines the type of this frame

The opcode of the frame. If type field is set to a valid string, this field is ignored.

rsv

Reserved bit, must be 0, if it is not 0, it is marked as connection failure

type

Default to text

The type of the frame. Accepted values are: continuation, text, binary, ping, pong, close

version

String. Default to draft-ietf-hybi-17

WebSocket protocol version string. See WebSocket for valid version strings.

METHODS

append

$frame->append( $chunk );

Append a frame chunk.

Beware that this method is destructive. It makes $chunk empty unless $chunk is read-only.

fin

Indicate whether this frame is the last frame of the entire message body

fragments

Sets or gets the array object of payload fragments

is_binary

Returns true if frame is of binary type, false otherwise.

is_close

Returns true if frame is of close type, false otherwise.

is_continuation

Returns true if frame is of continuation type, false otherwise.

is_ping

Returns true if frame is a ping request, false otherwise.

is_pong

Returns true if frame is a pong response, false otherwise.

is_text

Returns true if frame is of text type, false otherwise.

mask

Indicate whether the carried content needs to be XORed with a mask

masked

$masked = $frame->masked;
$frame->masked(1);

Get or set masking of the frame.

max_fragments_amount

The maximum fragments allowed.

max_payload_size

The maximum size of the payload. You may set this to 0 (but not undef) to disable checking the payload size.

next

$frame->append( $some_data );

$frame->next; # next message

Return the next message as a Perl string (UTF-8 decoded).

next_bytes

Return the next message as is.

opcode

$opcode = $frame->opcode;
$frame->opcode(8);

Get or set opcode of the frame. Operation bit, which defines the type of this frame.

rsv

Reserved bit, must be 0, if it is not 0, it is marked as connection failure

supported_types

Provided a type and this returns true if it is supported, false otherwise. This is case insensitive.

Without any argument, this returns an array object of supported frame types.

to_bytes

Construct a WebSocket message.

CREDITS

Viacheslav Tykhanovskyi for code borrowed.

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

WebSocket::Client, WebSocket::Connection, WebSocket::Server

COPYRIGHT & LICENSE

Copyright(c) 2021-2023 DEGUEST Pte. Ltd.

You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.