lib/PAGI/App/WebSocket/Echo.pm

filter: Statementstmt 100% 30 / 30 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 90.3% 47 / 52 CC 8 SCAR 16.8
CC
8
Cov
93%
__ANON__
8.1
to_app
1.0
BEGIN
1.0
CRAP 5.4
« blib/lib/PAGI/Middleware/CSRF.pm ↑ Summary blib/lib/PAGI/Endpoint/HTTP.pm »
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
BranchTrueFalseTotal
if $scope->{"type"} ne "websocket" 0 4 4
37  
38 # Accept the connection
39 4 await $send->({ type => 'websocket.accept' });
40  
41 4 ▶ $on_connect->($scope) if $on_connect;
Branch: 1 branch
BranchTrueFalseTotal
if $on_connect 1 3 4
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
BranchTrueFalseTotal
if ($event->{'type'} eq 'websocket.receive') { } 2 4 6
elsif ($event->{'type'} eq 'websocket.disconnect') { } 4 0 4
48 # Echo back - preserve text vs binary
49 2 ▶ if (exists $event->{text}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $event->{'text'}) { } 1 1 2
elsif (exists $event->{'bytes'}) { } 1 0 1
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
BranchTrueFalseTotal
if $on_disconnect 1 3 4
62 4 last;
63 }
64 }
65 4 };
66 }
67  
68 1 1;
69  
70 __END__
« blib/lib/PAGI/Middleware/CSRF.pm ↑ Summary blib/lib/PAGI/Endpoint/HTTP.pm »