Hi everybody, I started looking into websocket as a way to push data from server to browser. I found example for Mojolicious written by Joel Berger. I am trying to make it to work, but no luck so far. here is the code:

websocket.pl

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 '/jquery-1.9.1.min.js' %= javascript '/jquery.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

While starting the script with morbo websocket.pl commands everything seems to be fine, but when I connect from the browser to the server I am getting following messages:

debug Your secret passphrase needs to be changed

debug GET "/"

debug Rendering template "index.html.ep" from DATA section

debug Template "layouts/basic.html.ep" not found

debug 200 OK (0.043215s, 23.140/s)

debug GET "/jquery-1.9.1.min.js"

debug Template "not_found.development.html.ep" not found

debug Template "not_found.html.ep" not found

debug Rendering inline template "fd403ab55a4c875e35b42428816134c7"

debug Rendering inline template "dd85456cf9f44e1e43bebae130a7f9df"

debug 404 Not Found (0.201262s, 4.969/s)

debug GET "/jquery.flot.js"

debug Template "not_found.development.html.ep" not found

debug Template "not_found.html.ep" not found

debug Rendering cached inline template "fd403ab55a4c875e35b42428816134c7"

debug Rendering cached inline template "dd85456cf9f44e1e43bebae130a7f9df"

debug 404 Not Found (0.051227s, 19.521/s)

Any idea what's wrong?

Thanks like always

Robert


In reply to Mojolicious Websocket example by rkrasowski

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.