How to do things after the app->start; in Mojolicious websocket for Perl

362 Views Asked by At

I have a server side websocket running on Linux and at the end the websocket is running but there need to be executed more after the app->start; Like in the code bellow I put a print hello world to try it but it doens't work. Somebody knows how to handle this?

    #!/usr/bin/perl
use utf8;
use Mojolicious::Lite;
use DateTime;
use Mojo::JSON;
use Mojo::Transaction::WebSocket;
use Data::Dumper;
no strict "refs";

get '/' => 'index';
my $clients = {};
# Arrays voor het ordenen van gasten
my @hoofdArray =();
my $teller = 0;

websocket '/echo' => sub {
    my $self = shift;
    $self->inactivity_timeout(0);
    app->log->debug(sprintf 'Client connected: %s', $self->tx);
    # Toevoegen origin op array positie 
    $teller = $teller + 1;      
    # later renderen van de websocket

    # Pushen van alle gasten in een array
    my $id = sprintf "%s", $self->tx;
    $clients->{$id} = $self->tx;
    $self->on(message =>
        sub {

            my ($self, $msg) = @_;

            if (index($msg, "naam:") != -1){
                my $ori = $self->tx->handshake->connection;
                my $naam = substr $msg,5;
                print $naam."\n";

                my @gasten = ();
                push(@gasten, $ori);
                push(@gasten, $naam);
                push(@hoofdArray, \@gasten);
            }
            else {   
                my $json = Mojo::JSON->new;
                my $dt   = DateTime->now( time_zone => 'Europe/Amsterdam');
                my $currentNaam = "undefinid";
                for (my $i = 0; $i < @hoofdArray; $i++){

                    if($hoofdArray[$i]->[0] eq $self->tx->handshake->connection){
                        $currentNaam = $hoofdArray[$i]->[1];
                        last;
                    }
                }

                for (keys %$clients) {
                    $clients->{$_}->send(
                        $json->encode({
                            hms  => $currentNaam,
                            text => $msg,
                        })
                    );
                    #print $_[0]->tx->handshake->req->content->headers->origin."\n";
                    #print $_[0]->tx->handshake->connection."\n";

                }
                print Dumper $hoofdArray[0];
                print Dumper $hoofdArray[1];
            }
        }
    );

    $self->on(finish =>
        sub {
            app->log->debug('Client with hash: '.$clients->{$id}.' disconnected');
            delete $clients->{$id};
        }
    );
};

app->start;
print "Hello World! \n";
1

There are 1 best solutions below

0
On

The short answer is that app->start never returns. The code preceding the app->start creates handlers for the routes you have defined. When the server gets a request for one one of those routes, it uses one of the handlers you provided to generate the content returned by the server. You might find it helpful to do some reading about event-driven programming. The routes can be thought of as the events that your program is handling. app->start more-or-less means "start the event loop".