line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::App::EventSource; |
2
|
2
|
|
|
2
|
|
49933
|
use 5.008001; |
|
2
|
|
|
|
|
6
|
|
3
|
2
|
|
|
2
|
|
6
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
30
|
|
4
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
48
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
366
|
use parent 'Plack::Component'; |
|
2
|
|
|
|
|
211
|
|
|
2
|
|
|
|
|
7
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
9004
|
use Plack::Util::Accessor qw(handler_cb headers); |
|
2
|
|
|
|
|
185
|
|
|
2
|
|
|
|
|
9
|
|
11
|
2
|
|
|
2
|
|
778
|
use Plack::App::EventSource::Connection; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
537
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub call { |
14
|
3
|
|
|
3
|
1
|
22488
|
my $self = shift; |
15
|
3
|
|
|
|
|
6
|
my ($env) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
return [405, [], ['Method not allowed']] |
18
|
3
|
100
|
|
|
|
15
|
unless $env->{REQUEST_METHOD} eq 'GET'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
return sub { |
21
|
2
|
|
|
2
|
|
51
|
my $respond = shift; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $writer = $respond->( |
24
|
|
|
|
|
|
|
[ |
25
|
|
|
|
|
|
|
200, |
26
|
|
|
|
|
|
|
[ |
27
|
|
|
|
|
|
|
'Content-Type' => 'text/event-stream; charset=UTF-8', |
28
|
|
|
|
|
|
|
'Cache-Control' => |
29
|
|
|
|
|
|
|
'no-store, no-cache, must-revalidate, max-age=0', |
30
|
|
|
|
|
|
|
'Access-Control-Allow-Methods' => 'GET', |
31
|
2
|
50
|
|
|
|
3
|
@{$self->headers || []} |
|
2
|
|
|
|
|
6
|
|
32
|
|
|
|
|
|
|
] |
33
|
|
|
|
|
|
|
] |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
69
|
my $connection; $connection = Plack::App::EventSource::Connection->new( |
37
|
|
|
|
|
|
|
push_cb => sub { |
38
|
2
|
|
|
|
|
2
|
my (@messages) = @_; |
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
|
|
4
|
foreach my $msg (@messages) { |
41
|
2
|
100
|
|
|
|
5
|
if (ref $msg eq 'HASH') { |
42
|
|
|
|
|
|
|
my $event = join "\x0d\x0a", |
43
|
2
|
|
|
|
|
6
|
map { "$_: ".$msg->{$_} } |
44
|
1
|
|
|
|
|
18
|
grep { defined $msg->{$_} } |
|
4
|
|
|
|
|
6
|
|
45
|
|
|
|
|
|
|
qw(event id data retry); |
46
|
1
|
|
|
|
|
5
|
eval { $writer->write("$event\x0d\x0a"); 1 } |
|
1
|
|
|
|
|
12
|
|
47
|
1
|
50
|
|
|
|
2
|
or do { $connection->close; return }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
else { |
50
|
1
|
50
|
|
|
|
1
|
eval { $writer->write("data: $msg\x0d\x0a"); 1 } or do { $connection->close; return }; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
18
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
2
|
50
|
|
|
|
2
|
eval { $writer->write("\x0d\x0a"); 1 } or do { |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
19
|
|
55
|
0
|
|
|
|
|
0
|
$connection->close; |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
}, |
58
|
|
|
|
|
|
|
close_cb => sub { |
59
|
2
|
|
|
|
|
1
|
eval { $writer->close }; |
|
2
|
|
|
|
|
8
|
|
60
|
|
|
|
|
|
|
} |
61
|
2
|
|
|
|
|
16
|
); |
62
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
5
|
$self->{handler_cb}->($connection, $env); |
64
|
2
|
|
|
|
|
10
|
}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
__END__ |