| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2017 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package Net::Async::WebSocket::JSON::Server; | ||
| 7 | |||
| 8 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use base qw( Net::Async::WebSocket::Server ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | Net::Async::WebSocket::Server->VERSION( '0.11' ); # respects subclasses changing handle_class | |
| 12 | |||
| 13 | 1 | our $VERSION = '0.01'; | |
| 14 | |||
| 15 | 1 | ▶ | use Net::Async::WebSocket::JSON::Protocol; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | C<Net::Async::WebSocket::JSON::Server> - server WebSocket clients using JSON and C<IO::Async> | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use IO::Async::Loop; | ||
| 24 | use Net::Async::WebSocket::JSON::Server; | ||
| 25 | |||
| 26 | my $server = Net::Async::WebSocket::JSON::Server->new( | ||
| 27 | on_client => sub { | ||
| 28 | my ( undef, $client ) = @_; | ||
| 29 | |||
| 30 | $client->configure( | ||
| 31 | on_json => sub { | ||
| 32 | my ( $self, $frame ) = @_; | ||
| 33 | $self->send_json( $frame ); | ||
| 34 | }, | ||
| 35 | ); | ||
| 36 | } | ||
| 37 | ); | ||
| 38 | |||
| 39 | my $loop = IO::Async::Loop->new; | ||
| 40 | $loop->add( $server ); | ||
| 41 | |||
| 42 | $server->listen( | ||
| 43 | service => 3000, | ||
| 44 | )->get; | ||
| 45 | |||
| 46 | $loop->run; | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | This subclass of L<Net::Async::WebSocket::Server> provides conveniences for | ||
| 51 | using JSON-encoded data sent over text frames. | ||
| 52 | |||
| 53 | It should be used identically to C<Net::Async::WebSocket::Server>, except that | ||
| 54 | connected client instances will be instances of | ||
| 55 | L<Net::Async::WebSocket::JSON::Protocol>, and have the new C<send_json> method | ||
| 56 | and C<on_json> event available. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub new | ||
| 61 | 1 | ▶ | { |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 62 | 1 | my $class = shift; | |
| 63 | 1 | return $class->SUPER::new( | |
| 64 | handle_class => "Net::Async::WebSocket::JSON::Protocol", | ||
| 65 | @_, | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | |||
| 69 | =head1 AUTHOR | ||
| 70 | |||
| 71 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | 1 | 0x55AA; | |