Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Mojolicious Websocket example

by trippledubs (Deacon)
on Jun 18, 2015 at 03:08 UTC ( [id://1130928]=note: print w/replies, xml ) Need Help??


in reply to Mojolicious Websocket example

Replies are listed 'Best First'.
Re^2: Mojolicious Websocket example
by rkrasowski (Sexton) on Jun 20, 2015 at 12:40 UTC

    Hmmm, I see, so the problem was that java scripts were not "seen". Interesting, both scripts were in the same directories as example script.

    Now I have the script working with correction that trippledubs did. Thank you very much. Still can not understand why scripts that were in the same directory were not recognized.

    Thanks like always, well, I am ready to play more with Mojolicious.....

    Robert

      Now I have the script working with correction that trippledubs did. Thank you very much. Still can not understand why scripts that were in the same directory were not recognized.

      Why do you expect them to be recognized?

      $ mojo generate app Goner [mkdir] ...goner\script [write] ...goner\script\goner [chmod] ...goner\script\goner 744 [mkdir] ...goner\lib [write] ...goner\lib\Goner.pm [mkdir] ...goner\lib\Goner\Controller [write] ...goner\lib\Goner\Controller\Example.pm [mkdir] ...goner\t [write] ...goner\t\basic.t [mkdir] ...goner\log [mkdir] ...goner\public [write] ...goner\public\index.html [mkdir] ...goner\templates\layouts [write] ...goner\templates\layouts\default.html.ep [mkdir] ...goner\templates\example [write] ...goner\templates\example\welcome.html.ep

      In this example same directory might be ...goner\ or ...goner\lib\ or ...goner\script\ but neither of those are ...goner\public\

        For comparison a lite app also won't serve from same directory, only from public
        $ mojo generate lite_app [exist] ...\goner [write] ...\goner\myapp.pl [chmod] ...\goner\myapp.pl 744 $ md public $ echo in public > public\hi.txt $ echo in same > no.txt $ perl myapp.pl get /hi.txt [Sat Jun 20 11:30:28 2015] [debug] Your secret passphrase needs to be +changed in public $ perl myapp.pl get /no.txt |wc --lines [Sat Jun 20 11:30:36 2015] [debug] Your secret passphrase needs to be +changed [Sat Jun 20 11:30:36 2015] [debug] GET "/no.txt" [Sat Jun 20 11:30:36 2015] [debug] Template "not_found.development.htm +l.ep" not found [Sat Jun 20 11:30:36 2015] [debug] Template "not_found.html.ep" not fo +und [Sat Jun 20 11:30:36 2015] [debug] Rendering inline template "fd403ab5 +5a4c875e35b42428816134c7" [Sat Jun 20 11:30:36 2015] [debug] Rendering inline template "dd85456c +f9f44e1e43bebae130a7f9df" [Sat Jun 20 11:30:36 2015] [debug] 404 Not Found (0.050898s, 19.647/s) 540

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1130928]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found