line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
use strict; |
2
|
2
|
|
|
2
|
|
917
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
61
|
|
3
|
2
|
|
|
2
|
|
9
|
use utf8; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
48
|
|
4
|
2
|
|
|
2
|
|
9
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
10
|
|
5
|
|
|
|
|
|
|
use Amon2::Util; |
6
|
2
|
|
|
2
|
|
43
|
use Amon2::Web::Response::Callback; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
103
|
|
7
|
2
|
|
|
2
|
|
810
|
|
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
789
|
|
8
|
|
|
|
|
|
|
my ($class, $c, $conf) = @_; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
0
|
7
|
Amon2::Util::add_method(ref $c || $c, 'streaming', \&_streaming); |
11
|
|
|
|
|
|
|
Amon2::Util::add_method(ref $c || $c, 'streaming_json', \&_streaming_json); |
12
|
2
|
|
33
|
|
|
21
|
} |
13
|
2
|
|
33
|
|
|
14
|
|
14
|
|
|
|
|
|
|
my ($self, $code) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
return Amon2::Web::Response::Callback->new( |
17
|
1
|
|
|
1
|
|
15
|
code => sub { |
18
|
|
|
|
|
|
|
$code->(@_); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
); |
21
|
1
|
|
|
1
|
|
12
|
} |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
|
|
11
|
my ($self, $code) = @_; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
return Amon2::Web::Response::Callback->new( |
26
|
|
|
|
|
|
|
code => sub { |
27
|
0
|
|
|
0
|
|
|
my $respond = shift; |
28
|
|
|
|
|
|
|
my $writer = $respond->([200, ['Content-Type' => 'application/json;charset=utf-8']]); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $longpoll_ctx = Amon2::Plugin::Web::Streaming::Writer->new( |
31
|
0
|
|
|
0
|
|
|
$self, |
32
|
0
|
|
|
|
|
|
$writer |
33
|
|
|
|
|
|
|
); |
34
|
0
|
|
|
|
|
|
$code->($longpoll_ctx); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
} |
38
|
0
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
my ($class, $c, $writer) = @_; |
41
|
|
|
|
|
|
|
bless {ctx => $c, writer => $writer}, $class; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my ($self, $data) = @_; |
45
|
|
|
|
|
|
|
my $json = $self->{ctx}->render_json($data)->content; |
46
|
0
|
|
|
0
|
|
|
$self->{writer}->write($json); |
47
|
0
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my ($self) = @_; |
50
|
|
|
|
|
|
|
$self->{writer}->close(); |
51
|
0
|
|
|
0
|
|
|
} |
52
|
0
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
1; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 NAME |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
0
|
|
|
Amon2::Plugin::Web::Streaming - streaming support for Amon2 |
58
|
0
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 SYNOPSIS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
use Amon2::Lite; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
__PACKAGE__->load_plugin(qw/Web::Streaming/); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
any '/poll' => sub { |
66
|
|
|
|
|
|
|
my $c = shift; |
67
|
|
|
|
|
|
|
return $c->streaming(sub { |
68
|
|
|
|
|
|
|
my $respond = shift; |
69
|
|
|
|
|
|
|
...; |
70
|
|
|
|
|
|
|
$respond->write([200, [], ['OK']]); |
71
|
|
|
|
|
|
|
}); |
72
|
|
|
|
|
|
|
}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
any '/poll_json' => sub { |
75
|
|
|
|
|
|
|
my $c = shift; |
76
|
|
|
|
|
|
|
return $c->streaming_json(sub { |
77
|
|
|
|
|
|
|
my $writer = shift; |
78
|
|
|
|
|
|
|
...; |
79
|
|
|
|
|
|
|
$writer->write_json(+{ }); |
80
|
|
|
|
|
|
|
$writer->close; |
81
|
|
|
|
|
|
|
}); |
82
|
|
|
|
|
|
|
}; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 DESCRIPTION |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
This is an Amon2 plugin to support streaming. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
You MUST use the HTTP server supporting psgi.streaming. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 EXPORTED METHODS |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=over 4 |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item $c->streaming($code); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
You can return delayed response for PSGI spec. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Argument for $code is C<< $respond >>. It's same as a argument for PSGI callback. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item $c->streaming_json($code); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
It's a short hand utility to publish streaming JSON. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
The argument is instance of Amon2::Plugin::Web::Streaming::Writer. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=back |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 Amon2::Plugin::Streaming::Writer METHODS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=over 4 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item new |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Do not create the instance directly. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item $writer->write_json($data) |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Write a $data as JSON for the socket. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item $writer->close() |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Close the socket. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=back |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 SEE ALSO |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L<PSGI> |
131
|
|
|
|
|
|
|
|