Net::WebSocket::Streamer - Stream a WebSocket message easily
Here’s the gist of it:
#Use the ::Client or ::Server subclass as needed. my $streamer = Net::WebSocket::Streamer::Client->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.
Note that the receiving application won’t necessarily have access to the individual message fragments (i.e., frames) that you send. Web browsers, for example, only expose messages, not frames. You may thus be better off sending full messages rather than frames.
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.