File Coverage

blib/lib/SockJS/Transport/EventSource.pm
Criterion Covered Total %
statement 37 43 86.0
branch 5 8 62.5
condition 3 5 60.0
subroutine 6 6 100.0
pod 0 2 0.0
total 51 64 79.6


line stmt bran cond sub pod time code
1             package SockJS::Transport::EventSource;
2              
3 1     1   506 use strict;
  1         3  
  1         29  
4 1     1   5 use warnings;
  1         2  
  1         26  
5              
6 1     1   5 use base 'SockJS::Transport::Base';
  1         2  
  1         413  
7              
8             sub new {
9 2     2 0 4079 my $self = shift->SUPER::new(@_);
10 2         5 my (%params) = @_;
11              
12 2   50     8 $self->{response_limit} = $params{response_limit} || 128 * 1024;
13              
14 2         13 push @{$self->{allowed_methods}}, 'GET';
  2         14  
15              
16 2         4 return $self;
17             }
18              
19             sub dispatch_GET {
20 2     2 0 4 my $self = shift;
21 2         4 my ($env, $conn) = @_;
22              
23 2         3 my $limit = $self->{response_limit};
24              
25             return sub {
26 2     2   298 my $respond = shift;
27              
28 2         8 my $writer =
29             $respond->( [ 200, [ 'Content-Type' => 'text/event-stream', ] ] );
30              
31 2 100 66     12 if ($conn->is_connected && !$conn->is_reconnecting) {
32 1         24 $writer->write("\x0d\x0a");
33 1         72 $writer->write(
34             qq{data: c[2010,"Another connection still open"]\x0d\x0a\x0d\x0a\n}
35             );
36 1         41 $writer->close;
37 1         37 return;
38             }
39              
40             $conn->write_cb(
41             sub {
42 1         2 my $conn = shift;
43 1         3 my ($message) = @_;
44              
45 1         3 $limit -= length($message) - 1;
46              
47 1         7 $writer->write("data: $message\x0d\x0a\x0d\x0a");
48              
49 1 50       53 if ($limit <= 0) {
50 0         0 $writer->close;
51              
52 0         0 $conn->reconnecting;
53             }
54             }
55 1         10 );
56              
57 1         4 $conn->on(close => sub { $writer->close });
  0         0  
58              
59 1         11 $writer->write("\x0d\x0a");
60 1         63 $conn->write('o');
61              
62 1 50       3 if ($conn->is_closed) {
    50          
63 0         0 $conn->connected;
64 0         0 $conn->close;
65             }
66             elsif ($conn->is_connected) {
67 0         0 $conn->reconnected;
68             }
69             else {
70 1         9 $conn->connected;
71             }
72 2         19 };
73             }
74              
75             1;