Skip to content

Instantly share code, notes, and snippets.

@kraih

kraih/example.pl Secret

Last active December 27, 2019 14:54
Show Gist options
  • Save kraih/ba9fad6b5676d440a14ccbf0f4e20658 to your computer and use it in GitHub Desktop.
Save kraih/ba9fad6b5676d440a14ccbf0f4e20658 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite -signatures, -async;
# Render template "index.html.ep" from the DATA section
get '/' => sub ($c) {
$c->render(template => 'index');
};
# WebSocket service used by the template to extract the title from a web site
websocket '/title' => sub ($c) {
$c->on(message => async sub ($c, $msg) {
my $tx = await $c->ua->get_p($msg);
my $title = $tx->result->dom->at('title')->text;
$c->send($title);
});
};
app->start;
__DATA__
@@ index.html.ep
% my $url = url_for 'title';
<script>
var ws = new WebSocket('<%= $url->to_abs %>');
ws.onmessage = function (event) { document.body.innerHTML += event.data };
ws.onopen = function (event) { ws.send('https://mojolicious.org') };
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment