line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::EventSource; |
2
|
1
|
|
|
1
|
|
43698
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = 0.3; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub register { |
7
|
|
|
|
|
|
|
my $self = shift; |
8
|
|
|
|
|
|
|
my $app = shift; |
9
|
|
|
|
|
|
|
my $conf = shift; |
10
|
|
|
|
|
|
|
$conf->{ timeout } ||= 300; |
11
|
|
|
|
|
|
|
$app->routes->add_shortcut('event_source' => sub { |
12
|
|
|
|
|
|
|
my $self = shift; |
13
|
|
|
|
|
|
|
my @pars = map { |
14
|
|
|
|
|
|
|
if(ref $_ eq "CODE") { |
15
|
|
|
|
|
|
|
my $copy = $_; |
16
|
|
|
|
|
|
|
$_ = sub { |
17
|
|
|
|
|
|
|
my $self = shift; |
18
|
|
|
|
|
|
|
Mojo::IOLoop->stream($self->tx->connection)->timeout($conf->{ timeout }); |
19
|
|
|
|
|
|
|
$self->res->headers->content_type('text/event-stream'); |
20
|
|
|
|
|
|
|
$self->$copy(@_); |
21
|
|
|
|
|
|
|
}; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
$_; |
24
|
|
|
|
|
|
|
} @_; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$app->routes->get( @_ ); |
27
|
|
|
|
|
|
|
}); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
*{ main::event_source } = sub { $app->routes->event_source( @_ ) }; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$app->helper( 'emit' => sub { |
32
|
|
|
|
|
|
|
my $self = shift; |
33
|
|
|
|
|
|
|
my $event = shift; |
34
|
|
|
|
|
|
|
my $data = shift; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$self->write("event:$event\ndata: $data\n\n"); |
37
|
|
|
|
|
|
|
} ); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
42 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
__END__ |