line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::WebSocket::Connection; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Role tying Plack::App::WebSocket::Connection with the Dancer serializer |
4
|
|
|
|
|
|
|
$Dancer2::Plugin::WebSocket::Connection::VERSION = '0.3.1'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
975
|
use Scalar::Util qw/ refaddr /; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
82
|
|
8
|
1
|
|
|
1
|
|
584
|
use Set::Tiny; |
|
1
|
|
|
|
|
1607
|
|
|
1
|
|
|
|
|
71
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
673
|
use Dancer2::Plugin::WebSocket::Group; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
48
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
9
|
use Moo::Role; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has manager => ( |
15
|
|
|
|
|
|
|
is => 'rw', |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has serializer => ( |
19
|
|
|
|
|
|
|
is => 'rw', |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has id => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
lazy => 1, |
25
|
|
|
|
|
|
|
default => sub { refaddr shift }, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has channels => ( |
29
|
|
|
|
|
|
|
is => 'rw', |
30
|
|
|
|
|
|
|
lazy => 1, |
31
|
|
|
|
|
|
|
clearer => 1, |
32
|
|
|
|
|
|
|
default => sub { Set::Tiny->new( '*', $_[0]->id ) }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub set_channels { |
37
|
0
|
|
|
0
|
1
|
|
my ( $self, @channels ) = @_; |
38
|
0
|
|
|
|
|
|
$self->clear_channels; |
39
|
0
|
|
|
|
|
|
$self->add_channels(@channels); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub add_channels { |
44
|
0
|
|
|
0
|
1
|
|
my ( $self, @channels ) = @_; |
45
|
0
|
|
|
|
|
|
$self->channels->insert(@channels); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
around send => sub { |
49
|
|
|
|
|
|
|
my( $orig, $self, $message ) = @_; |
50
|
|
|
|
|
|
|
if( my $s = $self->serializer and ref $message ne 'AnyEvent::WebSocket::Message' ) { |
51
|
|
|
|
|
|
|
$message = $s->encode($message); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
$orig->($self,$message); |
54
|
|
|
|
|
|
|
}; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub in_channel { |
58
|
0
|
|
|
0
|
1
|
|
my ( $self, @channels ) = @_; |
59
|
0
|
|
|
|
|
|
my $target_set; |
60
|
0
|
0
|
0
|
|
|
|
if ( @channels == 1 and ref $channels[0] eq 'Set::Tiny' ) { |
61
|
0
|
|
|
|
|
|
$target_set = shift @channels; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
else { |
64
|
0
|
|
|
|
|
|
$target_set = Set::Tiny->new(@channels); |
65
|
|
|
|
|
|
|
} |
66
|
0
|
|
|
|
|
|
return not $self->channels->is_disjoint($target_set); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub to { |
71
|
0
|
|
|
0
|
1
|
|
my ( $self, @channels ) = @_; |
72
|
0
|
|
|
|
|
|
return Dancer2::Plugin::WebSocket::Group->new( |
73
|
|
|
|
|
|
|
source => $self, |
74
|
|
|
|
|
|
|
channels => \@channels, |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
__END__ |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=pod |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=encoding UTF-8 |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 NAME |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Dancer2::Plugin::WebSocket::Connection - Role tying Plack::App::WebSocket::Connection with the Dancer serializer |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 VERSION |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
version 0.3.1 |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 DESCRIPTION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
The connection objects used by L<Dancer2::Plugin::WebSocket> are |
97
|
|
|
|
|
|
|
L<Plack::App::WebSocket::Connection> objects augmented with this role. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 Attributes |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=over 4 |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=item serializer |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Serializer object used to serialize/deserialize messages. If it's not |
106
|
|
|
|
|
|
|
C<undef>, all messages that are not L<AnyEvent::WebSocket::Message> objects |
107
|
|
|
|
|
|
|
are assumed to be JSON and will be deserialized |
108
|
|
|
|
|
|
|
before being passed to the handlers, and will be serialized after being |
109
|
|
|
|
|
|
|
give to C<send>. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item id |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
A numerical value that is the id of the connection. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=back |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 Methods |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=over |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item set_channels( @channels ) |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Set the channels this connection belongs to. In addition to the C<@channels> provided, the |
124
|
|
|
|
|
|
|
connection is always associated to its id channel (which is always numerical) |
125
|
|
|
|
|
|
|
as well as the global channel C<*>. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item add_channels( @channels ) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Add C<@channels> to the list of channels the connection belongs to. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item in_channel( @channels ) |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Returns C<true> if the connection belongs to at least one of the |
134
|
|
|
|
|
|
|
given C<@channels>. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item to( @channels ) |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Returns a L<Dancer2::Plugin::WebSocket::Group> that will emit messages |
139
|
|
|
|
|
|
|
to all connections belonging to the given C<@channels>. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$conn->to( 'players' )->send( "game about to begin" ); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 AUTHOR |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Yanick Champoux <yanick@cpan.org> |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This software is copyright (c) 2021, 2019, 2017 by Yanick Champoux. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
154
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |