Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The Controller for the websocket communication channel that all clients communicate with (worked perfectly! :-) ):
- package XXXXXX::Actions::Websocket;
- use Modern::Perl;
- use utf8;
- use Mojo::Base 'Mojolicious::Controller';
- use Mojo::IOLoop;
- use Mojo::Redis;
- sub controlchannel {
- my $self = shift;
- my $tx = $self->tx;
- $tx->kept_alive(1);
- my $redis = Mojo::Redis->new;
- $redis->on(
- message => 'mychannel',
- sub {
- my ( $redis, $err, $message, $channel ) = @_;
- $tx->send( $message || $err );
- $self->app->log->debug("Redis message '$message'");
- }
- );
- my $client_id = sprintf '%s', $tx;
- $self->app->log->debug("WebSocket opened, client: $client_id");
- Mojo::IOLoop->stream( $self->tx->connection )->timeout(300);
- # Incoming message
- $self->on(
- message => sub {
- my ( $self, $message ) = @_;
- $self->app->log->debug("WebSocket message: '$message'");
- $redis->publish( mychannel => $message );
- }
- );
- $self->stash( redis => $redis );
- # Closed
- $self->on(
- finish => sub {
- my ( $self, $code, $reason ) = @_;
- if ( !defined $reason ) {
- $reason = q{[Unknown Reason]},;
- }
- $self->app->log->debug(
- "Sitelog WebSocket ($client_id} closed with status $code. Reason: $reason."
- );
- delete $self->stash->{redis};
- undef $tx;
- }
- );
- return 1;
- }
- 1;
- ====
- What Did Not Work:
- In startup() in the main application module:
- ...
- use Mojo::Redis;
- ...
- $self->helper(
- update_clients => sub {
- my $self = shift;
- my $redis = Mojo::Redis->new;
- $redis->publish(mychannel => 'reload');
- return 1;
- }
- );
- And then for certain actions, like in a controller later on:
- $self->update_clients;
- ====
- What Did Work:
- In startup() in the main application module:
- ...
- use Redis;
- ...
- $self->helper(
- update_clients => sub {
- my $self = shift;
- my $redis = Redis->new;
- $redis->publish('mychannel', 'reloadlogs');
- $redis->quit;
- return 1;
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement