Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Mojolicious Websocket example

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


in reply to Mojolicious Websocket example

Do you actually have those javascripts installed? It seems to work with the CDN versions
#!/usr/bin/env perl use Mojolicious::Lite; any '/' => 'index'; websocket '/data' => sub { my $self = shift; my $timer = Mojo::IOLoop->recurring( 1 => sub { state $i = 0; $self->send({ json => gen_data($i++) }); }); $self->on( finish => sub { Mojo::IOLoop->remove($timer); }); }; sub gen_data { my $x = shift; return [ $x, sin( $x + 2*rand() - 2*rand() ) ] } app->start; __DATA__ @@ index.html.ep % layout 'basic'; %= javascript 'https://code.jquery.com/jquery-2.1.4.min.js'; %= javascript 'https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquer +y.flot.js' <div id="plot" style="width:600px;height:300px"> </div> %= javascript begin var data = []; var plot = $.plot($('#plot'), [ data ]); var url = '<%= url_for('data')->to_abs %>'; var ws = new WebSocket( url ); ws.onmessage = function(e){ var point = JSON.parse(e.data); data.push(point); plot.setData([data]); plot.setupGrid(); plot.draw(); }; % end
Shell:
:~$ morbo t1.pl Server available at http://127.0.0.1:3000 [Wed Jun 17 21:54:11 2015] [debug] Your secret passphrase needs to be +changed [Wed Jun 17 21:54:11 2015] [debug] GET "/" [Wed Jun 17 21:54:11 2015] [debug] Rendering template "index.html.ep" +from DATA section [Wed Jun 17 21:54:11 2015] [debug] Template "layouts/basic.html.ep" no +t found [Wed Jun 17 21:54:11 2015] [debug] 200 OK (0.005384s, 185.736/s) [Wed Jun 17 21:54:11 2015] [debug] GET "/data" [Wed Jun 17 21:54:11 2015] [debug] Routing to a callback [Wed Jun 17 21:54:11 2015] [debug] 101 Switching Protocols (0.000710s, + 1408.451/s)
Browser shows oscillating lines and scaling horizontal axis. Where did you get this example from?

Log In?
Username:
Password:

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

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

    No recent polls found