Protoc...:Frame(3pm)
π Protocol::WebSocket::Frame β WebSocket Frame (Perl 3pm page) π¦
okay so this is the Protocol::WebSocket::Frame page from the User Contributed Perl Documentation, and bestie, it's literally the whole toolkit for building and reading WebSocket frames. let's get into it β¨
πΈ SYNOPSIS πΈ
no cap, here's the vibe:
# Create frame
my $frame = Protocol::WebSocket::Frame->new('123');
$frame->to_bytes;
# Parse frames
my $frame = Protocol::WebSocket::Frame->new;
$frame->append(...);
$f->next; # get next message
$f->next; # get another next message
see how you can go either way? make a frame, or slurp bytes in and pull messages out one at a time π«Ά (and yeah it's $f there, not $frame β that's literally how the original has it, iykyk, not my typo π)
π DESCRIPTION π
Construct or parse a WebSocket frame. that's it, that's the whole thing β it's giving main character energy for the entire WebSocket protocol tbh π
π₯ RANDOM MASK GENERATION π₯
so by default it uses the built-in rand for random mask generation, and bestieβ¦ that is not secure. literally not secure at all π
but good news: if Math::Random::Secure is installed, it gets used instead. so installing it is lowkey the slay move πβ¨
π METHODS π
β new
Protocol::WebSocket::Frame->new('data'); # same as (buffer => 'data')
Protocol::WebSocket::Frame->new(buffer => 'data', type => 'close');
Create a new Protocol::WebSocket::Frame instance. it automatically detects whether the data you passed is a Perl string (UTF-8 flag) or bytes β it just knows ππ
when you call it with more than one argument, it takes these named arguments. all of them are optional, no pressure:
buffer=> STR (default:"") β the payload of the frame. πtype=> TYPE_STR (default:"text") β the type of the frame. accepted values are:
continuation
text
binary
ping
pong
close
opcode=> INT (default:1) β the opcode of the frame. heads up: if thetypefield is set to a valid string, this field is ignored. sorry, opcode, type wins πfin=> BOOL (default:1) β thefinflag of the frame.finflag must be1in the ending frame of fragments. not optional, that's the rule πmasked=> BOOL (default:0) β if set to true, the frame will be masked. πversion=> VERSION_STR (default:'draft-ietf-hybi-17') β WebSocket protocol version string. checkProtocol::WebSocketfor valid version strings, iykyk π
β is_continuation
Check if frame is of continuation type. π
β is_text
Check if frame is of text type. π
β is_binary
Check if frame is of binary type. πΎ
β is_ping
Check if frame is a ping request. π
β is_pong
Check if frame is a pong response. π back atcha
β is_close
Check if frame is of close type. πͺ
β opcode
$opcode = $frame->opcode;
$frame->opcode(8);
Get/set opcode of the frame. dual-purpose icon β¨
β masked
$masked = $frame->masked;
$frame->masked(1);
Get/set masking of the frame. π
β append
$frame->append($chunk);
Append a frame chunk.
β οΈ beware bestie β this method is destructive. it makes$chunkempty unless$chunkis read-only. like it literally eats your chunk π don't say i didn't warn you.
β next
$frame->append(...);
$frame->next; # next message
Return the next message as a Perl string (UTF-8 decoded). π
β next_bytes
Return the next message as is. no decoding, raw bytes, exactly as delivered π§
β to_bytes
Construct a WebSocket message. π¨
β max_payload_size
The maximum size of the payload. you may set this to 0 or undef to disable checking the payload size. so like, set it to nothing if you want no limit β your call, live your truth π¦
π sign-off π
that's the whole page, bestie! π«Ά this was perl v5.42.0, dated 2019-03-13, straight from the User Contributed Perl Documentation, and the title up top is Protoc...:Frame(3pm) in case anyone asks. now go parse some frames and slay π
πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Protocol-WebSocket 0.26-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms