Skip to content

Commit

Permalink
use a working example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 16, 2014
1 parent 19f19d4 commit 39e6d51
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
41 changes: 19 additions & 22 deletions README.md
Expand Up @@ -46,7 +46,7 @@
```perl
use Mojolicious::Lite;

get '/' => {text => 'Hello World!'};
get '/' => {text => 'I ♥ Mojolicious!'};

app->start;
```
Expand All @@ -67,36 +67,33 @@ app->start;
```perl
use Mojolicious::Lite;

# Simple plain text response
get '/' => {text => 'I ♥ Mojolicious!'};

# Route associating "/time" with template in DATA section
get '/time' => 'clock';

# Scrape information from remote sites
post '/title' => sub {
my $c = shift;
my $url = $c->param('url') || 'http://mojolicio.us';
my $title = $c->ua->get($url)->res->dom->at('title')->text;
$c->render(json => {url => $url, title => $title});
};
# Route associating "/" with template in DATA section
get '/' => 'fetch';

# WebSocket echo service
websocket '/echo' => sub {
# WebSocket service scraping information from remote site
websocket '/title' => sub {
my $c = shift;
$c->on(message => sub {
my ($c, $msg) = @_;
$c->send("echo: $msg");
my ($c, $url) = @_;
my $title = $c->ua->get($url)->res->dom->at('title')->text;
$c->send($title);
});
};

app->start;
__DATA__
@@ clock.html.ep
% use Time::Piece;
% my $now = localtime;
The time is <%= $now->hms %>.
@@ fetch.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>
```
Single file prototypes like this one can easily grow into well-structured
Expand Down
2 changes: 1 addition & 1 deletion examples/websocket.pl
Expand Up @@ -31,7 +31,7 @@
ws.onmessage = function (event) {
document.body.innerHTML += JSON.parse(event.data).test;
};
ws.onopen = function (event) {
ws.onopen = function () {
ws.send(JSON.stringify({test: 'WebSocket support works! ♥'}));
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -943,7 +943,7 @@ and return them with L<Mojolicious::Controller/"send">.
ws.onmessage = function (event) {
document.body.innerHTML += JSON.parse(event.data).msg;
};
ws.onopen = function (event) {
ws.onopen = function () {
ws.send(JSON.stringify({msg: 'I ♥ Mojolicious!'}));
};
</script>
Expand Down

0 comments on commit 39e6d51

Please sign in to comment.