File Coverage

blib/lib/Kelp/Module/WebSocket/AnyEvent/Connection.pm
Criterion Covered Total %
statement 9 28 32.1
branch 0 4 0.0
condition 0 12 0.0
subroutine 3 6 50.0
pod 3 3 100.0
total 15 53 28.3


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