Subject: | Support for raw frame access |
There are various cases where the raw frame data would be useful.
This patch provides on_raw_frame, for direct access to this. It should also resolve the issues around ping/pong handling which currently require manual patching of the module.
There's also a bit of cleanup around the URI::wss? method/@ISA handling.
cheers,
Tom
Subject: | 2017-01-03-websockets.patch |
diff --git a/lib/Net/Async/WebSocket/Client.pm b/lib/Net/Async/WebSocket/Client.pm
index 5b97964..f480090 100644
--- a/lib/Net/Async/WebSocket/Client.pm
+++ b/lib/Net/Async/WebSocket/Client.pm
@@ -17,10 +17,23 @@ use Scalar::Util qw( blessed );
use URI;
-# In case URI doesn't know that ws:// and wss:// URIs use host/port
-require URI::_server;
-@URI::ws::ISA = ("URI::_server");
-@URI::wss::ISA = ("URI::_server");
+BEGIN {
+ eval {
+ require URI::wss;
+ } or do {
+ # In case URI doesn't know that ws:// and wss:// URIs use host/port
+ require URI::_server;
+ @URI::ws::ISA = ("URI::_server") unless URI::ws->isa('URI::_server');
+ @URI::wss::ISA = ("URI::_server") unless URI::ws->isa('URI::_server');
+ };
+
+ # We also need to support ->resource_name, which the CPAN module does not
+ # understand as of 2017-01-01
+ no warnings 'once';
+ *URI::wss::resource_name = sub {
+ shift->path
+ } unless URI::wss->can('resource_name')
+}
our $VERSION = '0.10';
diff --git a/lib/Net/Async/WebSocket/Protocol.pm b/lib/Net/Async/WebSocket/Protocol.pm
index 7520b2b..bf8adbd 100644
--- a/lib/Net/Async/WebSocket/Protocol.pm
+++ b/lib/Net/Async/WebSocket/Protocol.pm
@@ -49,12 +49,20 @@ The following named parameters may be passed to C<new> or C<configure>:
=item on_frame => CODE
-A CODE reference for when a frame is received
+A CODE reference for when a text frame is received
- $on_frame->( $self, $frame )
+ $on_frame->( $self, $frame_content )
+
+=item on_raw_frame => CODE
+
+A CODE reference for when a frame of any type is received, including PING/PONG/binary/close
+
+ $on_raw_frame->( $self, $frame, $bytes )
=back
+For more details on the C<$frame> instance, see L<Protocol::WebSocket::Frame>.
+
=cut
sub configure
@@ -62,8 +70,8 @@ sub configure
my $self = shift;
my %params = @_;
- foreach (qw( on_frame )) {
- $self->{$_} = delete $params{$_} if exists $params{on_frame};
+ foreach (qw( on_frame on_raw_frame )) {
+ $self->{$_} = delete $params{$_} if exists $params{$_};
}
$self->SUPER::configure( %params );
@@ -86,10 +94,11 @@ sub on_read
$framebuffer->append( $$buffref ); # modifies $$buffref
- while( defined( my $frame = $framebuffer->next ) ) {
+ while( defined( my $bytes = $framebuffer->next_bytes ) ) {
$self->debug_printf( "FRAME " . $FRAMETYPES{$framebuffer->opcode} );
- $self->invoke_event( on_frame => $frame );
+ $self->maybe_invoke_event( on_raw_frame => $framebuffer, $bytes );
+ $self->maybe_invoke_event( on_frame => Encode::decode_utf8( $bytes ) ) if $framebuffer->is_text;
}
return 0;