Hello,

I am just starting out with Mojolicious.  I am trying to create an 
application that provides a Websocket interface to a child process managed 
by the application.  I started with the great example of a Websocket 
program here:

    Joel's example 
<http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&cad=rja&uact=8&ved=0CEIQFjAF&url=http%3A%2F%2Fblogs.perl.org%2Fusers%2Fjoel_berger%2F2014%2F09%2Fmojolicious-do-it-for-the-candy.html&ei=vLuZVLqAEsGVNr-_gYgI&usg=AFQjCNFDjyc9tu0Sl9K4Z2U5oKO41MqOvw&bvm=bv.82001339,d.eXY>

I started to modify it by stripping uneeded functions and adding code to 
manage the child process.  Here is that version (minus the html/javascript 
part which is currently unchanged from Joel's):

#!/usr/bin/env 
perl                                                                            
                       


use Mojolicious::Lite;
use Mojo::IOLoop;

use IO::Async::Process;
use IO::Async::Loop::Mojo;

my $loop = IO::Async::Loop::Mojo->new;

my $process = IO::Async::Process->new(
    command => [ "/bin/ping", "-c4", "localhost" ],

    stdout => {
        on_read => sub {
            my ( $stream, $buffref, $eof ) = @_;
            while ( $$buffref =~ s/^(.*)\n// ) {
                print "PING wrote: $1\n";
            }
            return 0;
        },
    },

    on_finish => sub {
        my ( $pid, $exitcode ) = @_;
        my $status = ( $exitcode >> 8 );

        $loop->stop;
    },
);

$loop->add($process);

# setup base 
route                                                                           
                         

any '/' => sub {
    my $self = shift;
    my $rows;
    push @$rows, [ 'col', 1 ];

    $self->render( 'index', rows => $rows );
};

# setup websocket message 
handler                                                                         
            

websocket '/monitor' => sub {
    my $self = shift;
    $self->on(
        json => sub {
            my ( $ws, $row ) = @_;

            my $html = $ws->render_to_string( 'table', rows => [$row] );
            $ws->send( { json => { row => $html } } );

            # Start recurring 
timer                                                                           
        

            my $i  = 1;
            my $id = Mojo::IOLoop->recurring(
                1 => sub {

                    $html = $ws->render_to_string( 'table', rows => [$row] 
);
                    $ws->send( { json => { row => $html } } );
                    $self->finish if $i++ == 5;
                }
            );

            # Stop recurring 
timer                                                                           
         

            $self->on( finish => sub { Mojo::IOLoop->remove($id) } );

        }
    );
};

app->start;

The problem is if I leave the "$loop->add($process);" line in code the 
Websocket part doesn't work.  This is probably due to how I setup the 
IO::Async stuff.

Also, thinking ahead getting past this I would like the Websocket clients 
to interact with the child process (see the output of the single child and 
to restart it).  However, it is not clear how to do that in Mojolicious.

Yes, these are probably very basic things and any assistance is appreciated.

Thanks,

John

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to