-
-
Save anonymous/3a012d544df830e92de8 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; | |
# Render template "index.html.ep" from the DATA section | |
get '/' => {template => 'index'}; | |
# WebSocket service used by the template to extract the title from a web site | |
websocket '/title' => sub ($c) { | |
$c->on(message => sub ($c, $msg) { | |
my $title = $c->ua->get($msg)->res->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('http://mojolicio.us') }; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment