Advertisement
Guest User

Mojo::Redis problems

a guest
Jun 10th, 2014
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.27 KB | None | 0 0
  1. The Controller for the websocket communication channel that all clients communicate with (worked perfectly! :-) ):
  2.  
  3. package XXXXXX::Actions::Websocket;
  4. use Modern::Perl;
  5. use utf8;
  6. use Mojo::Base 'Mojolicious::Controller';
  7. use Mojo::IOLoop;
  8. use Mojo::Redis;
  9.  
  10. sub controlchannel {
  11.     my $self = shift;
  12.  
  13.     my $tx = $self->tx;
  14.     $tx->kept_alive(1);
  15.     my $redis = Mojo::Redis->new;
  16.     $redis->on(
  17.         message => 'mychannel',
  18.         sub {
  19.             my ( $redis, $err, $message, $channel ) = @_;
  20.             $tx->send( $message || $err );
  21.             $self->app->log->debug("Redis message '$message'");
  22.         }
  23.     );
  24.  
  25.     my $client_id = sprintf '%s', $tx;
  26.     $self->app->log->debug("WebSocket opened, client: $client_id");
  27.     Mojo::IOLoop->stream( $self->tx->connection )->timeout(300);
  28.  
  29.     # Incoming message
  30.     $self->on(
  31.         message => sub {
  32.             my ( $self, $message ) = @_;
  33.             $self->app->log->debug("WebSocket message: '$message'");
  34.             $redis->publish( mychannel => $message );
  35.         }
  36.     );
  37.  
  38.     $self->stash( redis => $redis );
  39.  
  40.     # Closed
  41.     $self->on(
  42.         finish => sub {
  43.             my ( $self, $code, $reason ) = @_;
  44.             if ( !defined $reason ) {
  45.                 $reason = q{[Unknown Reason]},;
  46.             }
  47.             $self->app->log->debug(
  48. "Sitelog WebSocket ($client_id} closed with status $code. Reason: $reason."
  49.             );
  50.             delete $self->stash->{redis};
  51.             undef $tx;
  52.         }
  53.     );
  54.     return 1;
  55. }
  56.  
  57. 1;
  58. ====
  59. What Did Not Work:
  60.  
  61. In startup() in the main application module:
  62. ...
  63. use Mojo::Redis;
  64. ...
  65.     $self->helper(
  66.         update_clients => sub {
  67.             my $self = shift;
  68.             my $redis = Mojo::Redis->new;
  69.             $redis->publish(mychannel => 'reload');
  70.             return 1;
  71.         }
  72.     );
  73.  
  74. And then for certain actions, like in a controller later on:
  75.  
  76. $self->update_clients;
  77.  
  78. ====
  79. What Did Work:
  80. In startup() in the main application module:
  81.  
  82. ...
  83. use Redis;
  84. ...
  85.     $self->helper(
  86.         update_clients => sub {
  87.             my $self = shift;
  88.             my $redis = Redis->new;
  89.             $redis->publish('mychannel', 'reloadlogs');
  90.             $redis->quit;
  91.             return 1;
  92.         }
  93.     );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement