Skip Menu |

This queue is for tickets about the Net-Async-WebSocket CPAN distribution.

Report information
The Basics
Id: 119654
Status: resolved
Priority: 0/
Queue: Net-Async-WebSocket

People
Owner: Nobody in particular
Requestors: TEAM [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 0.10
Fixed in: 0.11



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;
Tiny update to include query parameters as well in ->resource_name. Technically Protocol::WebSocket doesn't use URI, it expects its own URI class, but I much prefer to use URI everywhere as a semi-de-facto-except-in-Mojolicious standard =) On 2017-01-02 19:06:22, TEAM wrote: Show quoted text
> 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-05-websockets.patch
diff --git a/lib/Net/Async/WebSocket/Client.pm b/lib/Net/Async/WebSocket/Client.pm index 5b97964..8d6acdb 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_query + } 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;
I've applied the first half (the URI::ws hackery) I'm unsure quite about the second half, because it changes the behaviour of the current 'on_frame' - now it will only receive text frames. I wonder if instead, we want an entire collection of on_text_frame on_binary_frame ... in addition to the 'on_raw_frame' -- Paul Evans
On Sat Jan 07 18:22:18 2017, PEVANS wrote: Show quoted text
> I'm unsure quite about the second half, because it changes the > behaviour of the current 'on_frame' - now it will only receive text > frames. > > I wonder if instead, we want an entire collection of > > on_text_frame > on_binary_frame > ... > > in addition to the 'on_raw_frame'
This was in fact added in 0.11. -- Paul Evans