I have this code snippet, I'm trying to push a string to the server every X seconds like:
#!/usr/bin/env perl
use Mojolicious::Lite;
use EV;
use AnyEvent;
use POSIX qw(strftime);
get '/' => sub {
my $self = shift;
$self->render('main');
};
websocket '/echo' => sub {
my $self = shift;
my $w;
$w = AE::timer 3, 1, sub {
$self->send('Got it');
};
# $self->send(strftime("Server $$: %a %b %e %H:%M:%S %Y", localtime));
};
app->start();
__DATA__
@@ main.html.ep
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<table id="tableID">
<tbody>
</tbody>
</table>
<script type="text/javascript">
var ws = new WebSocket('ws://192.168.1.104:3000/echo');
ws.onopen = function () {
alert('Connection opened');
};
ws.onerror = function() { alert("Error"); };
ws.onmessage = function (msg) {
$("#tableID").find('tbody')
.append($('<tr>')
.append($('<td>')
.text(msg.data)
)
);
};
</script>
</body>
</html>
AFAIK Mojo used IO::Loop
event loop, which I guess should be fine with AnyEvent
This doesn't work and I'm wondering why.
When I remove the AE part and uncomment the simple send
, I see the results in the browser.
P.S: Just experimenting with WebSockets and Mojo for some Project I will need to use Both Mojo and AnyEvent + WebSockets