| 1 | package HTTP::ServerEvent; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | $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<event> - 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<data> - 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<id> - the event id. If you send this, a client will send the | ||||||||||||||||||||||||||||
| 48 | C<Last-Event-Id> 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<retry> - 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 | 13 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 13 | my ($self, %options) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Better be on the safe side | ||||||||||||||||||||||||||||
| 68 | 13 | for my $key (qw( event id retry )) { | |||||||||||||||||||||||||||
| 69 | croak "Newline or null detected in event type '$options{ $key }'. Possible event injection." | ||||||||||||||||||||||||||||
| 70 | 30 | ▶ | if defined $options{ $key } and $options{ $key } =~ /[\x0D\x0A\x00]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options{$key} and $options{$key} =~ /[\x0D\x0A\x00]/
MC/DC100%2/2
defined $options{$key} and $options{$key} =~ /[\x0D\x0A\x00]/
defined $options{$key}
$options{$key} =~ /[\x0D\x0A\x00]/
Truth table100%3/3
A and B
A= defined $options{$key}B= $options{$key} =~ /[\x0D\x0A\x00]/
| |||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | if( !$options{ data }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $options{ data }= []; | |||||||||||||||||||||||||||
| 75 | }; | ||||||||||||||||||||||||||||
| 76 | $options{ data } = [ $options{ data }] | ||||||||||||||||||||||||||||
| 77 | 4 | ▶ | unless 'ARRAY' eq ref $options{ data }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 4 | my @result; | |||||||||||||||||||||||||||
| 80 | 4 | ▶ | if( defined $options{ event }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | push @result, "event: $options{ event }"; | |||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | 4 | ▶ | if(defined $options{ id }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1 | push @result, "id: $options{ id }"; | |||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | ▶ | if( defined $options{ retry }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | push @result, "retry: $options{ retry }"; | |||||||||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 7 | push @result, map {"data: $_" } | |||||||||||||||||||||||||||
| 92 | 4 | map { split /(?:\x0D\x0A?|\x0A)/ } | |||||||||||||||||||||||||||
| 93 | 4 | ▶ | @{ $options{ data } || [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options{'data'} || []
MC/DC0%0/1
$options{'data'} || []
$options{'data'}
Truth table50%1/2
A
A= $options{'data'}
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 4 | return ((join "\x0D\x0A", @result) . "\x0D\x0A\x0D\x0A") | |||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | 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<EventSource> 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<EventSource> 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<https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events> | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | L<https://hacks.mozilla.org/2011/06/a-wall-powered-by-eventsource-and-server-sent-events/> | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | L<http://www.html5rocks.com/en/tutorials/eventsource/basics/?ModPagespeed=noscript> | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | The public repository of this module is | ||||||||||||||||||||||||||||
| 132 | L<http://github.com/Corion/http-serverevent>. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | The public support forum of this module is | ||||||||||||||||||||||||||||
| 137 | L<http://perlmonks.org/>. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 BUG TRACKER | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||||||||||||||||||||
| 142 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-ServerEvent> | ||||||||||||||||||||||||||||
| 143 | or via mail to L<http-serverevent-Bugs@rt.cpan.org>. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Max Maischein C<corion@cpan.org> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 COPYRIGHT (c) | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Copyright 2013-2013 by Max Maischein C<corion@cpan.org>. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||