I want to output my log via websocket using File::Tail.

Below is my code:

use Mojolicious::Lite;
use Mojo::EventEmitter;
use File::Tail;

helper events => sub { state $events = Mojo::EventEmitter->new };

get '/' => 'log';

websocket '/ws' => sub {
  my $c = shift;
  app->log->info(sprintf 'Client connected: %s', $c->tx);
  $c->inactivity_timeout(3600);
  my $file = File::Tail->new(name => '/home/pavel/test.txt', maxinterval => 
1, adjustafter => 7);
  while (defined(my $line=$file->read)) {
    $c->send({json => $line});
  }
};

app->start;

__DATA__

@@ log.html.ep
<div id="log"></div>
<script>
  var ws  = new WebSocket('<%= url_for('ws')->to_abs %>');
  ws.onmessage = function (e) {
    document.getElementById('log').innerHTML += '<p>' + e.data + '</p>';
  };
  function sendChat(input) { ws.send(input.value); input.value = '' }
</script>

When I do 

*echo "test" >> /home/pavel/test.txt*

Nothing happened, "test" message isn't showing on webpage. Moreover I can't 
refresh the page. I don't know why but I think that it's because of    while
 (defined(my $line=$file->read)) {  act like an infinite loop and can't 
properly interact with non-blocking mojolicious nature.

How to make app working?

-- 
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 https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to