line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::ServerEvent; |
2
|
2
|
|
|
2
|
|
28550
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
88
|
|
3
|
2
|
|
|
2
|
|
11
|
use Carp qw( croak ); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
135
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
11
|
use vars qw($VERSION); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
1113
|
|
6
|
|
|
|
|
|
|
$VERSION = '0.02'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
HTTP::ServerEvent - create strings for HTTP Server Sent Events |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=cut |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head2 C<< ->as_string( %options ) >> |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
return HTTP::ServerEvent->as_string( |
17
|
|
|
|
|
|
|
event => "ping", |
18
|
|
|
|
|
|
|
data => time(), |
19
|
|
|
|
|
|
|
retry => 5000, # retry in 5 seconds if disconnected |
20
|
|
|
|
|
|
|
id => $counter++, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Returns a string that can be sent as a server-sent event |
24
|
|
|
|
|
|
|
through the socket. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
The allowed options are: |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=over 4 |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item * |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
C - the type of event (optional). This is the |
33
|
|
|
|
|
|
|
event type you will want to listen to on the other side. |
34
|
|
|
|
|
|
|
Newlines or null characters in the event type are |
35
|
|
|
|
|
|
|
treated as a fatal error. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item * |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
C - the data to be sent. This can be either a string |
40
|
|
|
|
|
|
|
or an array reference of strings. Note that embedded newlines |
41
|
|
|
|
|
|
|
(either C<\x{0d}> , C<\x{0a}> or C<\x{0d}\x{0a}> ) will |
42
|
|
|
|
|
|
|
be interpreted as newlines and be normalized to the C<\x{0d}\x{0a}> |
43
|
|
|
|
|
|
|
pairs that are sent over the wire. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=item * |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
C - the event id. If you send this, a client will send the |
48
|
|
|
|
|
|
|
C header when reconnecting, allowing you to send |
49
|
|
|
|
|
|
|
the events missed while offline. |
50
|
|
|
|
|
|
|
Newlines or null characters in the event id are |
51
|
|
|
|
|
|
|
treated as a fatal error. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item * |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
C - the amount of miliseconds to wait before reconnecting |
56
|
|
|
|
|
|
|
if the connection is lost. |
57
|
|
|
|
|
|
|
Newlines or null characters in the retry interval are |
58
|
|
|
|
|
|
|
treated as a fatal error. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=back |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub as_string { |
65
|
13
|
|
|
13
|
1
|
4655
|
my ($self, %options) = @_; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Better be on the safe side |
68
|
13
|
|
|
|
|
26
|
for my $key (qw( event id retry )) { |
69
|
30
|
100
|
100
|
|
|
1394
|
croak "Newline or null detected in event type '$options{ $key }'. Possible event injection." |
70
|
|
|
|
|
|
|
if defined $options{ $key } and $options{ $key } =~ /[\x0D\x0A\x00]/; |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
|
73
|
4
|
50
|
|
|
|
12
|
if( !$options{ data }) { |
74
|
0
|
|
|
|
|
0
|
$options{ data }= []; |
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
$options{ data } = [ $options{ data }] |
77
|
4
|
100
|
|
|
|
14
|
unless 'ARRAY' eq ref $options{ data }; |
78
|
|
|
|
|
|
|
|
79
|
4
|
|
|
|
|
6
|
my @result; |
80
|
4
|
100
|
|
|
|
11
|
if( defined $options{ event }) { |
81
|
1
|
|
|
|
|
4
|
push @result, "event: $options{ event }"; |
82
|
|
|
|
|
|
|
}; |
83
|
4
|
100
|
|
|
|
10
|
if(defined $options{ id }) { |
84
|
1
|
|
|
|
|
3
|
push @result, "id: $options{ id }"; |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
|
87
|
4
|
100
|
|
|
|
9
|
if( defined $options{ retry }) { |
88
|
1
|
|
|
|
|
4
|
push @result, "retry: $options{ retry }"; |
89
|
|
|
|
|
|
|
}; |
90
|
|
|
|
|
|
|
|
91
|
7
|
|
|
|
|
16
|
push @result, map {"data: $_" } |
|
4
|
|
|
|
|
28
|
|
92
|
|
|
|
|
|
|
map { split /(?:\x0D\x0A?|\x0A)/ } |
93
|
4
|
50
|
|
|
|
6
|
@{ $options{ data } || [] }; |
|
4
|
|
|
|
|
25
|
|
94
|
|
|
|
|
|
|
|
95
|
4
|
|
|
|
|
22
|
return ((join "\x0D\x0A", @result) . "\x0D\x0A\x0D\x0A") |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 Javascript EventSource object |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
To receive events on the other side, usually in a browser, |
103
|
|
|
|
|
|
|
you will want to instantiate an C object. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
var events = new EventSource('/events'); |
106
|
|
|
|
|
|
|
// Subscribe to "tick" event |
107
|
|
|
|
|
|
|
events.addEventListener('tick', function(event) { |
108
|
|
|
|
|
|
|
var out= document.getElementById("my_console"); |
109
|
|
|
|
|
|
|
out.appendChild(document.createTextNode(event.data)); |
110
|
|
|
|
|
|
|
}, false); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 Last-Event-Id Header |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
If you're sending events, you may want to look at the C<< Last-Event-Id >> |
116
|
|
|
|
|
|
|
HTTP header. This header is sent by the C object when |
117
|
|
|
|
|
|
|
reestablishing a connection that was intermittently lost. You can use this |
118
|
|
|
|
|
|
|
to bring the reconnecting client up to date with the current state |
119
|
|
|
|
|
|
|
instead of transmitting the complete state. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 SEE ALSO |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
L |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
L |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
L |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 REPOSITORY |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
The public repository of this module is |
132
|
|
|
|
|
|
|
L. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 SUPPORT |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
The public support forum of this module is |
137
|
|
|
|
|
|
|
L. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 BUG TRACKER |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Please report bugs in this module via the RT CPAN bug queue at |
142
|
|
|
|
|
|
|
L |
143
|
|
|
|
|
|
|
or via mail to L. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 AUTHOR |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Max Maischein C |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 COPYRIGHT (c) |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Copyright 2013-2013 by Max Maischein C. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 LICENSE |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
This module is released under the same terms as Perl itself. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |