File Coverage

blib/lib/PocketIO/Transport/WebSocket.pm
Criterion Covered Total %
statement 21 54 38.8
branch 0 4 0.0
condition n/a
subroutine 7 10 70.0
pod 1 1 100.0
total 29 69 42.0


line stmt bran cond sub pod time code
1             package PocketIO::Transport::WebSocket;
2              
3 6     6   59 use strict;
  6         12  
  6         240  
4 6     6   33 use warnings;
  6         16  
  6         2573  
5              
6 6     6   37 use base 'PocketIO::Transport::Base';
  6         32  
  6         535  
7              
8 6     6   6389 use Protocol::WebSocket::Frame;
  6         139261  
  6         234  
9 6     6   6905 use Protocol::WebSocket::Handshake::Server;
  6         203817  
  6         203  
10              
11 6     6   67 use PocketIO::Exception;
  6         14  
  6         146  
12 6     6   5173 use PocketIO::Handle;
  6         29  
  6         3523  
13              
14             sub dispatch {
15 0     0 1   my $self = shift;
16              
17 0           my $handle = $self->{handle};
18              
19 0           my $hs =
20             Protocol::WebSocket::Handshake::Server->new_from_psgi($self->{env});
21 0 0         PocketIO::Exception->throw(500, 'WebSocket failed: ' . $hs->error)
22             unless $hs->parse($handle->fh);
23              
24 0 0         return unless $hs->is_done;
25              
26 0           my $version = $hs->version;
27              
28 0           my $frame = Protocol::WebSocket::Frame->new(version => $version);
29              
30             return sub {
31 0     0     my $respond = shift;
32              
33             $handle->write(
34             $hs->to_string => sub {
35 0           my $handle = shift;
36              
37 0           my $conn = $self->conn;
38              
39             my $close_cb = sub {
40 0           $handle->close;
41 0           $self->client_disconnected($conn);
42 0           };
43 0           $handle->on_eof($close_cb);
44 0           $handle->on_error($close_cb);
45              
46 0           $handle->on_heartbeat(sub { $conn->send_heartbeat });
  0            
47              
48             $handle->on_read(
49             sub {
50 0           $frame->append($_[1]);
51              
52 0           while (my $message = $frame->next_bytes) {
53 0           $conn->parse_message($message);
54             }
55             }
56 0           );
57              
58             $conn->on(
59             close => sub {
60 0           my $conn = shift;
61              
62             # $handle->write(); TODO write WebSocket EOF
63              
64 0           $handle->close;
65 0           $self->client_disconnected($conn);
66             }
67 0           );
68              
69             $conn->on(
70             write => sub {
71 0           my $bytes = $self->_build_frame(
72             buffer => $_[1],
73             version => $version
74             );
75              
76 0           $handle->write($bytes);
77             }
78 0           );
79              
80 0           $self->client_connected($conn);
81             }
82 0           );
83 0           };
84             }
85              
86             sub _build_frame {
87 0     0     my $self = shift;
88              
89 0           return Protocol::WebSocket::Frame->new(@_)->to_bytes;
90             }
91              
92             1;
93             __END__