| 1 | package Web::Async::WebSocket::Server; | ||
| 2 | 0 | ▶ | use Full::Class qw(:v1), extends => 'IO::Async::Notifier'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | |||
| 4 | our $VERSION = '0.006'; ## VERSION | ||
| 5 | ## AUTHORITY | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Web::Async::WebSocket::Server - L<Future>-based web+HTTP handling | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Provides basic websocket server implementation. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | use Ryu::Async; | ||
| 18 | use IO::Async::Listener; | ||
| 19 | |||
| 20 | use Web::Async::WebSocket::Server::Connection; | ||
| 21 | |||
| 22 | field $srv; | ||
| 23 | field $ryu : reader : param = undef; | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | =head2 port | ||
| 28 | |||
| 29 | Returns the current listening port. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | field $port : reader : param = undef; | ||
| 34 | |||
| 35 | =head2 incoming_client | ||
| 36 | |||
| 37 | A L<Ryu::Source> which emits an event every time a client connects. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | field $incoming_client : reader : param = undef; | ||
| 42 | |||
| 43 | =head2 disconnecting_client | ||
| 44 | |||
| 45 | A L<Ryu::Source> which emits an event every time a client disconnects. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | field $disconnecting_client : reader : param = undef; | ||
| 50 | |||
| 51 | =head2 closing_client | ||
| 52 | |||
| 53 | A L<Ryu::Source> which emits an event every time a client closes normally. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | field $closing_client : reader : param = undef; | ||
| 58 | field $active_client : reader { +{ } } | ||
| 59 | |||
| 60 | field $handshake : reader : param = undef; | ||
| 61 | field $on_handshake_failure : reader : param = undef; | ||
| 62 | |||
| 63 | field $listening : reader = undef; | ||
| 64 | |||
| 65 | method configure (%args) { | ||
| 66 | $port = delete $args{port} if exists $args{port}; | ||
| 67 | $on_handshake_failure = delete $args{on_handshake_failure} if exists $args{on_handshake_failure}; | ||
| 68 | $handshake = delete $args{handshake} if exists $args{handshake}; | ||
| 69 | $incoming_client = delete $args{incoming_client} if exists $args{incoming_client}; | ||
| 70 | $closing_client = delete $args{closing_client} if exists $args{closing_client}; | ||
| 71 | $disconnecting_client = delete $args{disconnecting_client} if exists $args{disconnecting_client}; | ||
| 72 | return $self->next::method(%args); | ||
| 73 | } | ||
| 74 | |||
| 75 | method _add_to_loop ($loop) { | ||
| 76 | $self->add_child( | ||
| 77 | $ryu = Ryu::Async->new | ||
| 78 | ) unless $ryu; | ||
| 79 | $incoming_client //= $self->ryu->source; | ||
| 80 | $closing_client //= $self->ryu->source; | ||
| 81 | $disconnecting_client //= $self->ryu->source; | ||
| 82 | $self->add_child( | ||
| 83 | $srv = IO::Async::Listener->new( | ||
| 84 | on_stream => $self->curry::weak::on_stream, | ||
| 85 | ) | ||
| 86 | ); | ||
| 87 | $self->adopt_future( | ||
| 88 | $listening = $srv->listen( | ||
| 89 | service => $port, | ||
| 90 | socktype => 'stream', | ||
| 91 | )->on_ready(sub { undef $listening }) | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | |||
| 95 | method on_stream ($listener, $stream, @) { | ||
| 96 | $log->tracef('Connection %s for listener %s', "$stream", "$listener"); | ||
| 97 | $stream->configure( | ||
| 98 | on_read => sub { 0 } | ||
| 99 | ); | ||
| 100 | my $client = Web::Async::WebSocket::Server::Connection->new( | ||
| 101 | server => $self, | ||
| 102 | stream => $stream, | ||
| 103 | ryu => $ryu, | ||
| 104 | handshake => $handshake, | ||
| 105 | on_handshake_failure => $on_handshake_failure, | ||
| 106 | ); | ||
| 107 | $active_client->{$client} = $client; | ||
| 108 | $log->infof('Client %s recorded', "$client"); | ||
| 109 | $self->add_child($client); | ||
| 110 | $incoming_client->emit($client); | ||
| 111 | $self->adopt_future( | ||
| 112 | $client->handle_connection | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | method on_client_close ($client, %args) { | ||
| 117 | $closing_client->emit({ | ||
| 118 | client => $client, | ||
| 119 | %args, | ||
| 120 | }); | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | |||
| 124 | method on_client_disconnect ($client, @) { | ||
| 125 | $disconnecting_client->emit({ | ||
| 126 | client => $client | ||
| 127 | }); | ||
| 128 | delete $active_client->{$client} or $log->errorf('Client %s was not recorded', "$client"); | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | |||
| 132 | 1; | ||
| 133 | |||
| 134 | =head1 AUTHOR | ||
| 135 | |||
| 136 | Tom Molesworth C<< <TEAM@cpan.org> >> | ||
| 137 | |||
| 138 | =head1 LICENSE | ||
| 139 | |||
| 140 | Copyright Tom Molesworth 2024. Licensed under the same terms as Perl itself. | ||
| 141 | |||