-
-
Save anonymous/9869edbdf244a6c69a99 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; | |
helper title => sub { shift->ua->get(shift)->res->dom->at('title')->text }; | |
# ...insert awesome comment here... | |
get '/' => sub { | |
my $c = shift; | |
return $c->render('index') unless my $url = $c->param('url'); | |
$c->render(text => $c->title($url)); | |
}; | |
# WebSocket service scraping information from a web site | |
websocket '/title' => sub { | |
my $c = shift; | |
$c->on(message => sub { | |
my ($c, $url) = @_; | |
$c->send($c->title($url)); | |
}); | |
}; | |
app->start; | |
__DATA__ | |
@@ index.html.ep | |
% my $websocket = url_for 'title'; | |
<script> | |
var ws = new WebSocket('<%= $websocket->to_abs %>'); | |
ws.onmessage = function (event) { | |
document.body.innerHTML += event.data; | |
}; | |
ws.onopen = function () { | |
ws.send('http://mojolicio.us'); | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment