daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Jan 19th, 2018 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env perl
  2. use Mojolicious::Lite;
  3. use EV;
  4. use AnyEvent::IRC::Client;
  5.  
  6. # Join #mojo on irc.perl.org
  7. my $irc = AnyEvent::IRC::Client->new;
  8. $irc->connect('irc.perl.org', 6667, {nick => "mojobot$$"});
  9. $irc->send_srv(JOIN => '#mojo');
  10.  
  11. get '/' => 'index';
  12.  
  13. get '/events' => sub {
  14.   my $self = shift;
  15.  
  16.   # Emit "msg" event for every new IRC message
  17.   $self->res->headers->content_type('text/event-stream');
  18.   my $g = $irc->reg_cb(publicmsg => sub {
  19.     my $message = pop->{params}->[1];
  20.     $self->write("event:msg\ndata: $message\n\n");
  21.   });
  22.   $self->on_finish(sub { undef $g });
  23. };
  24.  
  25. app->start;
  26. __DATA__
  27.  
  28. @@ index.html.ep
  29. <!doctype html><html>
  30.   <head><title>The Mojolicious IRC channel</title></head>
  31.   <body>
  32.     <script>
  33.       var events = new EventSource('<%= url_for 'events' %>');
  34.  
  35.       // Subscribe to "msg" event
  36.       events.addEventListener('msg', function(event) {
  37.         document.body.innerHTML += event.data + '<br/>';
  38.       }, false);
  39.     </script>
  40.   </body>
  41. </html>
RAW Paste Data
Top