Net::WebSocket::Streamer - Send a stream easily over WebSocket
Here’s the gist of it:
my $streamer = Net::WebSocket::Streamer->new('binary'); my $frame = $streamer->create_chunk($buf); my $last_frame = $streamer->create_final($buf);
… but a more complete example might be this: streaming a file of arbitrary size in 64-KiB chunks:
my $size = -s $rfh; while ( read $rfh, my $buf, 65536 ) { my $frame; if (tell($rfh) == $size) { $frame = $streamer->create_final($buf); } else { $frame = $streamer->create_chunk($buf); } syswrite $wfh, $frame->to_bytes(); }
You can, of course, create/send an empty final frame for cases where you’re not sure how much data will actually be sent.
To stream custom frame types (or overridden classes), you can subclass this module and define frame_class_* constants, where * is the frame type, e.g., text, binary.
frame_class_*
*
text
binary
To install Net::WebSocket, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::WebSocket
CPAN shell
perl -MCPAN -e shell install Net::WebSocket
For more information on module installation, please visit the detailed CPAN module installation guide.