我有一个在 Linux 上运行的服务器端 websocket,最后 websocket 正在运行,但在 app->start 之后需要执行更多;就像在下面的代码中一样,我放了一个 print hello world 来尝试它,但它不起作用。有人知道如何处理这个吗?
#!/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";