-
-
Save kraih/ba9fad6b5676d440a14ccbf0f4e20658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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