blib/lib/PAGI/App/WebSocket/Broadcast.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 121 CC 13 SCAR 36.1
CC
13
Cov
0%
to_app
72.0
broadcast
20.0
client_count
12.0
CRAP 36.8
1 package PAGI::App::WebSocket::Broadcast;
2 $PAGI::App::WebSocket::Broadcast::VERSION = '0.002001';
3 use strict;
4 use warnings;
5 use Future::AsyncAwait;
6  
7 =head1 NAME
8  
9 PAGI::App::WebSocket::Broadcast - Pub/sub WebSocket broadcast
10  
11 =head1 SYNOPSIS
12  
13 use PAGI::App::WebSocket::Broadcast;
14  
15 my $app = PAGI::App::WebSocket::Broadcast->new->to_app;
16  
17 =cut
18  
19 # Shared state for all connections
20 my %channels; # channel => { clients => { id => send_cb } }
21 my $next_id = 1;
22  
23 sub new {
24 my ($class, %args) = @_;
25  
26 return bless {
27 default_channel => $args{channel} // 'default',
28 echo_self => $args{echo_self} // 0,
29 }, $class;
30 }
31  
32 sub to_app {
33 my ($self) = @_;
34  
35 my $default_channel = $self->{default_channel};
36 my $echo_self = $self->{echo_self};
37  
38 return async sub {
39 my ($scope, $receive, $send) = @_;
40 die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'websocket';
41  
42 # Accept the connection
43 await $send->({ type => 'websocket.accept' });
44  
45 my $client_id = $next_id++;
46 my $channel = $default_channel;
47  
48 # Register client
49 $channels{$channel} //= { clients => {} };
50 $channels{$channel}{clients}{$client_id} = $send;
51  
52 eval {
53 while (1) {
54 my $event = await $receive->();
55  
56 if ($event->{type} eq 'websocket.receive') {
57 my $message = $event->{text} // $event->{bytes};
58 my $is_text = exists $event->{text};
59  
60 # Broadcast to all clients in the channel
61 my $clients = $channels{$channel}{clients};
62 for my $id (keys %$clients) {
63 next if $id eq $client_id && !$echo_self;
64 my $client_send = $clients->{$id};
65 eval {
66 if ($is_text) {
67 await $client_send->({
68 type => 'websocket.send',
69 text => $message,
70 });
71 } else {
72 await $client_send->({
73 type => 'websocket.send',
74 bytes => $message,
75 });
76 }
77 };
78 # Remove dead clients
79 if ($@) {
80 delete $clients->{$id};
81 }
82 }
83 } elsif ($event->{type} eq 'websocket.disconnect') {
84 last;
85 }
86 }
87 };
88  
89 # Cleanup
90 delete $channels{$channel}{clients}{$client_id};
91 delete $channels{$channel} if !keys %{$channels{$channel}{clients}};
92 };
93 }
94  
95 # Class method to broadcast to a channel
96 sub broadcast {
97 my ($class, $channel, $message, %opts) = @_;
98  
99 return unless $channels{$channel};
100  
101 my $is_text = !$opts{binary};
102 my $clients = $channels{$channel}{clients};
103  
104 for my $id (keys %$clients) {
105 my $send = $clients->{$id};
106 eval {
107 if ($is_text) {
108 $send->({ type => 'websocket.send', text => $message });
109 } else {
110 $send->({ type => 'websocket.send', bytes => $message });
111 }
112 };
113 delete $clients->{$id} if $@;
114 }
115 }
116  
117 # Get connected client count
118 sub client_count {
119 my ($class, $channel) = @_;
120 $channel //= undef;
121  
122 if ($channel) {
123 return 0 unless $channels{$channel};
124 return scalar keys %{$channels{$channel}{clients}};
125 }
126 my $total = 0;
127 $total += scalar keys %{$_->{clients}} for values %channels;
128 return $total;
129 }
130  
131 1;
132  
133 __END__