Hi everyone,
I need a simple app which got POST request and send its content through
websocket to all connected browser clients.
I wrote a code,
#!/usr/bin/env perl
use Mojolicious::Lite;
use feature 'say';
my $clients = {};
my $curr_msg = {};
post '/' => sub {
my $self = shift;
my $params = $self->req->params->names;
for (@$params) {
$curr_msg->{$_} = $self->param($_);
};
$self->render(json => { status => 'post ok', data => $curr_msg});
};
websocket '/ws' => sub {
my $self = shift;
app->log->debug(sprintf 'Client connected: %s', $self->tx);
my $id = sprintf "%s", $self->tx;
$clients->{$id} = $self->tx;
if (%$curr_msg) {
for (keys %$clients) {
$clients->{$_}->send(json => $curr_msg);
};
$curr_msg = {};
};
$self->on(finish => sub {
app->log->debug('Client disconnected');
delete $clients->{$id};
});
};
app->start;
But for some reason it doesn't work - not sending json to clients. I have
no idea why and how to debug.
Does anyone see any bad things in my code?
Any suggestion is much appreciated.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.