Skip to content

Instantly share code, notes, and snippets.

Created September 20, 2014 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3a012d544df830e92de8 to your computer and use it in GitHub Desktop.
Save anonymous/3a012d544df830e92de8 to your computer and use it in GitHub Desktop.
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