File Coverage

blib/lib/Kelp/Module/WebSocket/AnyEvent/Connection.pm
Criterion Covered Total %
statement 9 27 33.3
branch 0 4 0.0
condition 0 12 0.0
subroutine 3 6 50.0
pod 0 3 0.0
total 12 52 23.0


line stmt bran cond sub pod time code
1             package Kelp::Module::WebSocket::AnyEvent::Connection;
2              
3             our $VERSION = '1.03';
4              
5 1     1   8 use Kelp::Base;
  1         3  
  1         10  
6 1     1   247 use Carp;
  1         2  
  1         61  
7 1     1   6 use Scalar::Util qw(weaken blessed);
  1         3  
  1         413  
8              
9             attr "-id";
10             attr "-manager";
11             attr "-connection";
12             attr "data" => sub { {} };
13              
14             sub new
15             {
16 0     0 0   my $class = shift;
17 0           my $self = $class->SUPER::new(@_);
18 0           $self->manager->connections->{$self->id} = $self;
19 0           weaken($self->{connection});
20 0           return $self;
21             }
22              
23             sub send
24             {
25 0     0 0   my ($self, $message) = @_;
26 0           my $serializer = $self->manager->get_serializer;
27 0   0       my $is_inst = blessed $message && $message->isa("AnyEvent::WebSocket::Message");
28              
29 0 0 0       if ($serializer && (!blessed $message || !$is_inst)) {
      0        
30 0           $message = $serializer->encode($message);
31             }
32              
33 0 0 0       if (ref $message && !$is_inst) {
34 0           carp "invalid data sent to websocket peer, disconnecting";
35 0           $self->connection->close;
36 0           return;
37             }
38              
39 0           $self->connection->send($message);
40             }
41              
42             sub close
43             {
44 0     0 0   my ($self) = @_;
45 0           delete $self->manager->connections->{$self->id};
46 0           $self->connection->close;
47             }
48              
49             1;
50             __END__