Well, that would explain, now I put scripts into public directory and everything is working like a charm. Thank you
Now I am trying to build an application that will let's say every second check the DB or file and if there is some new value, will send it via websocket to thebrowsers. So, I wrote a script that send a message ( date and time). I am able to send it once, but how to put it into the loop? How to check the DB every seconds or every few seconds and send that data via WS?
here is the code that sends single message:
#!/usr/bin/env perl use warnings; use strict; use Mojolicious::Lite; get '/' => 'index'; websocket '/echo' => sub { my $c = shift; $c->app->log->debug('WebSocket opened'); $c->inactivity_timeout(300); my $msg1 = `date`; $c->send($msg1); $c->on(finish => sub { my ($c, $code, $reason) = @_; $c->app->log->debug("WebSocket closed with status $code"); }); }; app->start; __DATA__ @@ index.html.ep <!DOCTYPE html> <html> <head><title>Test</title></head> <body> <script> var ws = new WebSocket('<%= url_for('echo')->to_abs %>'); ws.onmessage = function(event) { document.getElementById("dateTime").innerHTML = event.data; }; </script> <h1 id="dateTime"></h1> </body> </html>
Thanks like always.
Robert
In reply to Re^4: Mojolicious Websocket example
by rkrasowski
in thread Mojolicious Websocket example
by rkrasowski
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |