Skip to content

Instantly share code, notes, and snippets.

/ws.pl Secret

Created September 16, 2014 23:04
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/2e570644b9e4e3f9fed0 to your computer and use it in GitHub Desktop.
Save anonymous/2e570644b9e4e3f9fed0 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
# Route associating "/" with template in DATA section
get '/' => 'title';
# WebSocket service scraping information from remote site
websocket '/scrape' => sub {
my $c = shift;
$c->on(message => sub {
my ($c, $url) = @_;
my $title = $c->ua->get($url)->res->dom->at('title')->text;
$c->send($title);
});
};
app->start;
__DATA__
@@ title.html.ep
% my $websocket = url_for 'scrape';
<script>
var ws = new WebSocket('<%= $websocket->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