The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::WebSocket::Parser - Parse WebSocket from a filehandle

SYNOPSIS

    my $iof = IO::Framed::Read->new($fh);

    my $parse = Net::WebSocket::Parser->new($iof);

    #See below for error responses
    my $frame = $parse->get_next_frame();

$iof should normally be an instance of IO::Framed::Read. You’re free to pass in anything with a read() method, but that method must implement the same behavior as IO::Framed::Read::read().

METHODS

OBJ->get_next_frame()

A call to this method yields one of the following:

  • If a frame can be read, it will be returned.

  • If only a partial frame is ready, undef is returned.

I/O DETAILS

IO::Framed was born out of work on this module; see that module’s documentation for the particulars of working with it. In particular, note the exceptions IO::Framed::X::EmptyRead and IO::Framed::X::ReadError. (As described in Net::WebSocket’s documentation, you can use an equivalent interface for frame chunking if you wish.)

CUSTOM FRAMES SUPPORT

To support reception of custom frame types you’ll probably want to subclass this module and define a specific custom constant for each supported opcode, e.g.:

    package My::WebSocket::Parser;

    use parent qw( Net::WebSocket::Parser );

    use constant OPCODE_CLASS_3 => 'My::WebSocket::Frame::booya';

… where My::WebSocket::Frame::booya is itself a subclass of Net::WebSocket::Base::DataFrame.

You can also use this to override the default classes for built-in frame types; e.g., OPCODE_CLASS_10() will override Net::WebSocket::Frame::pong as the class will be used for pong frames that this module receives. That could be useful, e.g., for compression extensions, where you might want the get_payload() method to decompress so that that detail is abstracted away.