Advertisement
sestegra

Mojolicious vs MIME::64

Feb 2nd, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.21 KB | None | 0 0
  1. #!/usr/bin/perl                                      
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use MIME::Base64;
  7. use Mojolicious::Lite;
  8.  
  9. # Template with browser-side code
  10. get '/' => 'index';              
  11.  
  12. # WebSocket echo service
  13. websocket '/echo' => sub {
  14.   my $self = shift;      
  15.  
  16.   # Connected
  17.   $self->app->log->debug('WebSocket connected.');
  18.  
  19.   # Incoming message
  20.   $self->on('message' => sub {
  21.     my ($self, $message) = @_;
  22.     $self->send_message("echo: $message");
  23.   });
  24.  
  25.   # Disconnected
  26.   $self->on('finish' => sub {
  27.     my $self = shift;
  28.     $self->app->log->debug('WebSocket disconnected.');
  29.   });
  30. };
  31.  
  32. app->start;
  33. __DATA__
  34.  
  35. @@ index.html.ep
  36. <!DOCTYPE html>
  37. <html>
  38.   <head><title>Echo</title></head>
  39.   <body>
  40.     <script>
  41.       var ws = new WebSocket('<%= url_for('echo')->to_abs %>');
  42.  
  43.       // Incoming messages
  44.       ws.onmessage = function(event) {
  45.         document.body.innerHTML += event.data + '<br/>';
  46.       };
  47.  
  48.       // Close
  49.       ws.onclose = function() {
  50.         document.body.innerHTML += 'Disconected <br/>';
  51.       };
  52.  
  53.       // Outgoing messages
  54.       window.setInterval(function() {
  55.         ws.send('Hello Mojo!');
  56.       }, 1000);
  57.     </script>
  58.   </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement