diff --git a/eg/chat/app.psgi b/eg/chat/app.psgi index 42ce3fb..ff07895 100644 --- a/eg/chat/app.psgi +++ b/eg/chat/app.psgi @@ -3,13 +3,39 @@ use warnings; use Tatsumaki; use Tatsumaki::Error; use Tatsumaki::Application; +use Tatsumaki::MessageQueue; use Time::HiRes; -package ChatPollHandler; +package ChatServerSentEventsHandler; use base qw(Tatsumaki::Handler); __PACKAGE__->asynchronous(1); -use Tatsumaki::MessageQueue; +sub get { + my($self, $channel) = @_; + + my $client_id = $self->request->param('client_id') || rand(1); + my $mq = Tatsumaki::MessageQueue->instance($channel); + + $self->server_sent_events_push(1); + + $mq->poll($client_id, sub { + my @events = @_; + for my $event (@events) { + if ($event->{type} eq 'message') { + $self->stream_write({ + event => $event->{type}, + data => $event + }); + } else { + $self->stream_write({ data => $event }); + } + } + }); +} + +package ChatPollHandler; +use base qw(Tatsumaki::Handler); +__PACKAGE__->asynchronous(1); sub get { my($self, $channel) = @_; @@ -87,6 +113,7 @@ use File::Basename; my $chat_re = '[\w\.\-]+'; my $app = Tatsumaki::Application->new([ + "/chat/($chat_re)/sse" => 'ChatServerSentEventsHandler', "/chat/($chat_re)/poll" => 'ChatPollHandler', "/chat/($chat_re)/mxhrpoll" => 'ChatMultipartPollHandler', "/chat/($chat_re)/post" => 'ChatPostHandler', diff --git a/eg/chat/templates/chat.html b/eg/chat/templates/chat.html index 99dd0cd..b5b9b30 100644 --- a/eg/chat/templates/chat.html +++ b/eg/chat/templates/chat.html @@ -1,5 +1,6 @@ % my $channel = $_[0]->{handler}->args->[0]; % my $mxhr = $_[0]->{handler}->request->param('mxhr'); +% my $sse = $_[0]->{handler}->request->param('sse');