| 1 | package Plack::Example::EventChat; | ||
| 2 | use strict; | ||
| 3 | use AnyEvent; | ||
| 4 | use HTTP::ServerEvent; | ||
| 5 | use Plack::Request; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Plack::Example::EventChat - Sample Server-Sent Events chat server | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | plackup -MPlack::Example::EventChat -e "Plack::Example::EventChat->chat_server" | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | use vars qw($VERSION); | ||
| 18 | $VERSION = '0.02'; | ||
| 19 | |||
| 20 | my $html= join "", <DATA>; | ||
| 21 | |||
| 22 | sub broadcast{ | ||
| 23 | my ($type, $payload, $listeners) = @_; | ||
| 24 | my $event= HTTP::ServerEvent->as_string( | ||
| 25 | data => $payload, | ||
| 26 | event => $type, | ||
| 27 | id => time(), | ||
| 28 | ); | ||
| 29 | |||
| 30 | for (@$listeners) { | ||
| 31 | eval { | ||
| 32 | $_->write($event); | ||
| 33 | 1; | ||
| 34 | } or undef $_; | ||
| 35 | }; | ||
| 36 | @$listeners= grep { $_ } @$listeners; | ||
| 37 | }; | ||
| 38 | |||
| 39 | # Creates a PSGI responder | ||
| 40 | sub chat_server { | ||
| 41 | |||
| 42 | my (%users); | ||
| 43 | my @chat; | ||
| 44 | my @listeners; | ||
| 45 | |||
| 46 | my $app= sub { | ||
| 47 | my $env= shift; | ||
| 48 | my $req= Plack::Request->new( $env ); | ||
| 49 | my $path= $req->path_info; | ||
| 50 | |||
| 51 | if( '/chat' eq $path) { | ||
| 52 | if((my $msg) = $req->body_parameters->{'msg'} ) { | ||
| 53 | push @chat, [time, $msg]; | ||
| 54 | broadcast( 'chat', $msg, \@listeners ); | ||
| 55 | }; | ||
| 56 | |||
| 57 | # prune the chat store: | ||
| 58 | my $cutoff= time() - 5*60; | ||
| 59 | @chat= grep { $_->[0] > $cutoff } @chat; | ||
| 60 | |||
| 61 | # Keep only up to 500 lines, ever | ||
| 62 | if( @chat > 500 ) { | ||
| 63 | splice @chat, 0, @chat-500; | ||
| 64 | }; | ||
| 65 | |||
| 66 | return [ 200, [ 'Location' => '/chat' ], [<<'CHAT']]; | ||
| 67 | <html> | ||
| 68 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
| 69 | <form action="/chat" method="POST" enctype="application/x-www-form-urlencoded"> | ||
| 70 | <input name="msg" type="text"> | ||
| 71 | <button name="send">Chat</button> | ||
| 72 | </form> | ||
| 73 | </html> | ||
| 74 | CHAT | ||
| 75 | }; | ||
| 76 | |||
| 77 | if( '/events' ne $path ) { | ||
| 78 | # Send the JS+HTML | ||
| 79 | return [ 200, ['Content-Type', 'text/html'], [$html] ] | ||
| 80 | }; | ||
| 81 | |||
| 82 | if( ! $env->{"psgi.streaming"}) { | ||
| 83 | my $err= "Server does not support streaming responses"; | ||
| 84 | warn $err; | ||
| 85 | return [ 500, ['Content-Type', 'text/plain'], [$err] ] | ||
| 86 | }; | ||
| 87 | |||
| 88 | # immediately starts the response and stream the content | ||
| 89 | return sub { | ||
| 90 | my $responder = shift; | ||
| 91 | my $writer = $responder->( | ||
| 92 | [ 200, [ 'Content-Type', 'text/event-stream' ]]); | ||
| 93 | |||
| 94 | if (my $last= $env->{HTTP_LAST_EVENT_ID}) { | ||
| 95 | # bring client up to date with the current chat | ||
| 96 | for( grep { $_->[0] > $last} @chat ) { | ||
| 97 | $writer->write( | ||
| 98 | event => 'chat', | ||
| 99 | id => $_->[0], | ||
| 100 | data => $_->[1], | ||
| 101 | ); | ||
| 102 | }; | ||
| 103 | }; | ||
| 104 | |||
| 105 | warn 0+@listeners; | ||
| 106 | push @listeners, $writer; | ||
| 107 | broadcast( 'count', 0+@listeners, \@listeners ); | ||
| 108 | }; | ||
| 109 | }; | ||
| 110 | }; | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | =head1 SEE ALSO | ||
| 115 | |||
| 116 | The source code of this module | ||
| 117 | |||
| 118 | L<HTTP::ServerEvent> | ||
| 119 | |||
| 120 | =head1 AUTHOR | ||
| 121 | |||
| 122 | Max Maischein C<corion@cpan.org> | ||
| 123 | |||
| 124 | =head1 COPYRIGHT (c) | ||
| 125 | |||
| 126 | Copyright 2013-2013 by Max Maischein C<corion@cpan.org>. | ||
| 127 | |||
| 128 | =head1 LICENSE | ||
| 129 | |||
| 130 | This module is released under the same terms as Perl itself. | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | __DATA__ |