| 1 | package PAGI::App::WebSocket::Echo; | ||||||||||||||
| 2 | 1 | $PAGI::App::WebSocket::Echo::VERSION = '0.002002'; | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 NAME | ||||||||||||||
| 8 | |||||||||||||||
| 9 | PAGI::App::WebSocket::Echo - Echo WebSocket messages back to sender | ||||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||
| 12 | |||||||||||||||
| 13 | use PAGI::App::WebSocket::Echo; | ||||||||||||||
| 14 | |||||||||||||||
| 15 | my $app = PAGI::App::WebSocket::Echo->new->to_app; | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =cut | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 4 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 20 | 4 | my ($class, %args) = @_; | |||||||||||||
| 21 | |||||||||||||||
| 22 | return bless { | ||||||||||||||
| 23 | on_connect => $args{on_connect}, | ||||||||||||||
| 24 | on_disconnect => $args{on_disconnect}, | ||||||||||||||
| 25 | 4 | }, $class; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 4 | ▶ | sub to_app { | ||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 29 | 4 | my ($self) = @_; | |||||||||||||
| 30 | |||||||||||||||
| 31 | 4 | my $on_connect = $self->{on_connect}; | |||||||||||||
| 32 | 4 | my $on_disconnect = $self->{on_disconnect}; | |||||||||||||
| 33 | |||||||||||||||
| 34 | 4 | ▶ | return async sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.9
| |||||||||||||||
| 35 | 4 | my ($scope, $receive, $send) = @_; | |||||||||||||
| 36 | 4 | ▶ | die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'websocket'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | # Accept the connection | ||||||||||||||
| 39 | 4 | await $send->({ type => 'websocket.accept' }); | |||||||||||||
| 40 | |||||||||||||||
| 41 | 4 | ▶ | $on_connect->($scope) if $on_connect; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | # Echo loop | ||||||||||||||
| 44 | 4 | while (1) { | |||||||||||||
| 45 | 6 | my $event = await $receive->(); | |||||||||||||
| 46 | |||||||||||||||
| 47 | 6 | ▶ | if ($event->{type} eq 'websocket.receive') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 48 | # Echo back - preserve text vs binary | ||||||||||||||
| 49 | 2 | ▶ | if (exists $event->{text}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 50 | await $send->({ | ||||||||||||||
| 51 | type => 'websocket.send', | ||||||||||||||
| 52 | text => $event->{text}, | ||||||||||||||
| 53 | 1 | }); | |||||||||||||
| 54 | } elsif (exists $event->{bytes}) { | ||||||||||||||
| 55 | await $send->({ | ||||||||||||||
| 56 | type => 'websocket.send', | ||||||||||||||
| 57 | bytes => $event->{bytes}, | ||||||||||||||
| 58 | 1 | }); | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | } elsif ($event->{type} eq 'websocket.disconnect') { | ||||||||||||||
| 61 | 4 | ▶ | $on_disconnect->($scope, $event->{code}) if $on_disconnect; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 4 | last; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | 4 | }; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 1 | 1; | |||||||||||||
| 69 | |||||||||||||||
| 70 | __END__ | ||||||||||||||