line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
$Kelp::Module::WebSocket::AnyEvent::Connection::VERSION = '1.05'; |
2
|
|
|
|
|
|
|
use Kelp::Base; |
3
|
2
|
|
|
2
|
|
13
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
12
|
|
4
|
2
|
|
|
2
|
|
348
|
use Scalar::Util qw(weaken blessed); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
112
|
|
5
|
2
|
|
|
2
|
|
11
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
621
|
|
6
|
|
|
|
|
|
|
attr "-id"; |
7
|
|
|
|
|
|
|
attr "-manager"; |
8
|
|
|
|
|
|
|
attr "-connection"; |
9
|
|
|
|
|
|
|
attr "data" => sub { {} }; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
{ |
12
|
|
|
|
|
|
|
my $class = shift; |
13
|
|
|
|
|
|
|
my $self = $class->SUPER::new(@_); |
14
|
0
|
|
|
0
|
1
|
|
$self->manager->connections->{$self->id} = $self; |
15
|
0
|
|
|
|
|
|
weaken($self->{manager}); |
16
|
0
|
|
|
|
|
|
weaken($self->{connection}); |
17
|
0
|
|
|
|
|
|
return $self; |
18
|
0
|
|
|
|
|
|
} |
19
|
0
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
{ |
21
|
|
|
|
|
|
|
my ($self, $message) = @_; |
22
|
|
|
|
|
|
|
my $serializer = $self->manager->get_serializer; |
23
|
|
|
|
|
|
|
my $is_inst = blessed $message && $message->isa("AnyEvent::WebSocket::Message"); |
24
|
0
|
|
|
0
|
1
|
|
|
25
|
0
|
|
|
|
|
|
if ($serializer && (!blessed $message || !$is_inst)) { |
26
|
0
|
|
0
|
|
|
|
$message = $serializer->encode($message); |
27
|
|
|
|
|
|
|
} |
28
|
0
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
29
|
0
|
|
|
|
|
|
if (ref $message && !$is_inst) { |
30
|
|
|
|
|
|
|
carp "invalid data sent to websocket peer, disconnecting"; |
31
|
|
|
|
|
|
|
$self->connection->close; |
32
|
0
|
0
|
0
|
|
|
|
return; |
33
|
0
|
|
|
|
|
|
} |
34
|
0
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
$self->connection->send($message); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
{ |
39
|
|
|
|
|
|
|
my ($self) = @_; |
40
|
|
|
|
|
|
|
delete $self->manager->connections->{$self->id}; |
41
|
|
|
|
|
|
|
$self->connection->close; |
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
0
|
1
|
|
|
44
|
0
|
|
|
|
|
|
1; |
45
|
0
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=pod |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 NAME |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Kelp::Module::WebSocket::AnyEvent::Connection - Thin wrapper around Plack::App::WebSocket::Connection |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 SYNOPSIS |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $id = $connection->id; |
55
|
|
|
|
|
|
|
$connection->data->{test} = 'custom data'; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$connection->send('hello there'); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 DESCRIPTION |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Connection objects of this class fly around in L<Kelp::Module::WebSocket::AnyEvent>. Refer to its documentation for details |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 id |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
an autoincremented identifier. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 manager |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
an instance of L<Kelp::Module::WebSocket::AnyEvent> (weakened). |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 connection |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
an instance of L<Plack::App::WebSocket::Connection> (weakened). |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 data |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
custom data, a hash by default. Can be written by specifying the first argument. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 METHODS |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 new |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
a Kelp-style constructor. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 send |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sends data to the websocket peer. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 close |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
closes the connection gracefully. |
94
|
|
|
|
|
|
|
|