| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Dancer2::Plugin::WebSocket::Group; |
|
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
|
3
|
|
|
|
|
|
|
# ABSTACT: Grouping of connections to send messages to |
|
4
|
|
|
|
|
|
|
$Dancer2::Plugin::WebSocket::Group::VERSION = '0.3.1'; |
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
8
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
39
|
|
|
7
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
13
|
|
|
|
1
|
|
|
|
|
49
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
7
|
use Moo; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has source => ( |
|
13
|
|
|
|
|
|
|
is => 'ro', |
|
14
|
|
|
|
|
|
|
required => 1, |
|
15
|
|
|
|
|
|
|
); |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has channels => ( |
|
18
|
|
|
|
|
|
|
is => 'ro', |
|
19
|
|
|
|
|
|
|
required => 1, |
|
20
|
|
|
|
|
|
|
); |
|
21
|
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
|
478
|
use Set::Tiny; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
293
|
|
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub targets { |
|
25
|
0
|
|
|
0
|
0
|
|
my ( $self, $omit_self ) = @_; |
|
26
|
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my $channels = Set::Tiny->new( @{$self->channels} ); |
|
|
0
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
return grep { |
|
30
|
0
|
0
|
0
|
|
|
|
$_->in_channel($channels) and |
|
31
|
|
|
|
|
|
|
( !$omit_self or $self->source->id != $_->id ) |
|
32
|
0
|
|
|
|
|
|
} values %{ $self->source->manager->connections }; |
|
|
0
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub send { |
|
37
|
0
|
|
|
0
|
1
|
|
my ( $self, @args ) = @_; |
|
38
|
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
$_->send(@args) for $self->targets; |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub broadcast { |
|
44
|
0
|
|
|
0
|
1
|
|
my ( $self, @args ) = @_; |
|
45
|
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
$_->send(@args) for $self->targets(1); |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
__END__ |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=pod |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=encoding UTF-8 |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 NAME |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Dancer2::Plugin::WebSocket::Group |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 VERSION |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
version 0.3.1 |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
websocket_on_message sub { |
|
69
|
|
|
|
|
|
|
my( $conn, $message ) = @_; |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
if ( $message eq 'tell to everybody' ) { |
|
72
|
|
|
|
|
|
|
$conn->to( '* ' )->send( "HEY, Y'ALL!" ); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
}; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 DESC |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Those objects are generated via the C<to> method of the L<Dancer2::Plugin::WebSocket::Connection> |
|
79
|
|
|
|
|
|
|
objects, and allow to easily send to groups of connections. |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
In addition to any channels one might fancy creating, each connection also has a private |
|
82
|
|
|
|
|
|
|
channel that is associated to its numerical id, and a global channel C<*> also exist |
|
83
|
|
|
|
|
|
|
to send messages to all connections. |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 Methods |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=over |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=item send( $message ) |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Send the message to all connections of the group. |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
$conn->to( 'players' )->send( "Hi!" ); |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item broadcast( $message ) |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Send the message to all connections of the group, except the original connection. |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
websocket_on_message sub { |
|
100
|
|
|
|
|
|
|
my( $conn, $msg ) = @_; |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
if ( $msg eq ='resign' ) { |
|
103
|
|
|
|
|
|
|
$conn->broadcast( "player ", $conn->idm " resigned" ); |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=back |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 AUTHOR |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Yanick Champoux <yanick@cpan.org> |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This software is copyright (c) 2021, 2019, 2017 by Yanick Champoux. |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
118
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |