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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|