Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^5: Mojolicious Websocket example

by Anonymous Monk
on Jun 21, 2015 at 09:16 UTC ( [id://1131323]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Mojolicious Websocket example
in thread Mojolicious Websocket example

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?

copy/paste the example I linked ?:)

oh right, its full of stuff

copy/paste the example you already had in the beginning?

Yeah, that sounds good, copy/paste the example you already had in Mojolicious Websocket example, the one trippledubs said worked

Also see localtime/gmtime

Replies are listed 'Best First'.
Re^6: Mojolicious Websocket example
by rkrasowski (Sexton) on Jun 22, 2015 at 01:39 UTC

    OK, I am learning that stuff. Here is a solution for my problem. Script runs every second and sends data ( current time in this example) via websocket.

    Here is the code:

    #!/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 $id = Mojo::IOLoop->recurring(1 => sub { my $msg1 = `date`; $c->send($msg1); }); $c->on(finish => sub { my ($c, $code, $reason) = @_; $c->on(finish => sub { Mojo::IOLoop->remove($id) }); $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 for help.

    Robert

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-25 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found