Skip to content

Commit

Permalink
added "websocket.pl" to example scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 28, 2011
1 parent fb59801 commit 7ab4592
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,7 +1,9 @@
This file documents the revision history for Perl extension Mojolicious.

2.34 2011-11-28 00:00:00
- Added "websocket.pl" to example scripts.
- Improved documentation.
- Fixed small bugs in example scripts.

2.33 2011-11-28 00:00:00
- Improved Mojo::EventEmitter performance slightly.
Expand Down
2 changes: 1 addition & 1 deletion examples/connect-proxy.pl
@@ -1,9 +1,9 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

# Use bundled libraries
use FindBin;
use lib "$FindBin::Bin/../lib";
use Mojo::Base -strict;

# "Cheating in a fake fight. That's low."
use Mojo::IOLoop;
Expand Down
2 changes: 1 addition & 1 deletion examples/flash-policy-server.pl
@@ -1,9 +1,9 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

# Use bundled libraries
use FindBin;
use lib "$FindBin::Bin/../lib";
use Mojo::Base -strict;

# "After all this time, somebody else with one eye... who ISN'T a clumsy
# carpenter or a kid with a BB gun."
Expand Down
2 changes: 1 addition & 1 deletion examples/microhttpd.pl
@@ -1,9 +1,9 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

# Use bundled libraries
use FindBin;
use lib "$FindBin::Bin/../lib";
use Mojo::Base -strict;

# "Kif, I'm feeling the Captain's Itch.
# I'll get the powder, sir."
Expand Down
53 changes: 53 additions & 0 deletions examples/websocket.pl
@@ -0,0 +1,53 @@
#!/usr/bin/env perl

# Use bundled libraries
use FindBin;
use lib "$FindBin::Bin/../lib";
use Mojolicious::Lite;

# "Stop being such a spineless jellyfish!
# You know full well I'm more closely related to the sea cucumber.
# Not where it counts."
any '/' => sub {
my $self = shift;
$self->on(message => sub { shift->send_message(shift) })
if $self->tx->is_websocket;
} => 'websocket';

app->start;
__DATA__
@@ websocket.html.ep
<!DOCTYPE html>
<html>
<head>
<title>WebSocket</title>
% my $url = url_for->to_abs->scheme('ws');
%= javascript begin
var ws;
if ("MozWebSocket" in window) {
ws = new MozWebSocket('<%= $url %>');
}
else if ("WebSocket" in window) {
ws = new WebSocket('<%= $url %>');
}
if(typeof(ws) !== 'undefined') {
function wsmessage(event) {
data = event.data;
alert(data);
}
function wsopen(event) {
ws.send("WebSocket support works!");
}
ws.onmessage = wsmessage;
ws.onopen = wsopen;
}
else {
alert("Sorry, your browser does not support WebSockets.");
}
% end
</head>
<body>
Testing WebSockets, please make sure you have JavaScript enabled.
</body>
</html>
48 changes: 1 addition & 47 deletions lib/Mojo/HelloWorld.pm
@@ -1,59 +1,13 @@
package Mojo::HelloWorld;
use Mojolicious::Lite;

# "Don't worry, son.
# I'm sure he's up in heaven right now laughing it up with all the other
# celebrities: John Dilinger, Ty Cobb, Joseph Stalin."
# "Does whisky count as beer?"
app->log->level('error');
app->log->path(undef);

any '/websocket' => sub {
my $self = shift;
$self->on(message => sub { shift->send_message(shift) })
if $self->tx->is_websocket;
};

# "Does whisky count as beer?"
any '/*whatever' => {whatever => '', text => 'Your Mojo is working!'};

1;
__DATA__
@@ websocket.html.ep
<!DOCTYPE html>
<html>
<head>
<title>WebSocket</title>
% my $url = url_for->to_abs->scheme('ws');
%= javascript begin
var ws;
if ("MozWebSocket" in window) {
ws = new MozWebSocket('<%= $url %>');
}
else if ("WebSocket" in window) {
ws = new WebSocket('<%= $url %>');
}
if(typeof(ws) !== 'undefined') {
function wsmessage(event) {
data = event.data;
alert(data);
}
function wsopen(event) {
ws.send("WebSocket support works!");
}
ws.onmessage = wsmessage;
ws.onopen = wsopen;
}
else {
alert("Sorry, your browser does not support WebSockets.");
}
% end
</head>
<body>
Testing WebSockets, please make sure you have JavaScript enabled.
</body>
</html>
__END__
=head1 NAME
Expand Down
3 changes: 3 additions & 0 deletions lib/Mojo/Home.pm
Expand Up @@ -103,6 +103,9 @@ sub list_files {

sub mojo_lib_dir { File::Spec->catdir(dirname(__FILE__), '..') }

# "Don't worry, son.
# I'm sure he's up in heaven right now laughing it up with all the other
# celebrities: John Dilinger, Ty Cobb, Joseph Stalin."
sub parse {
my ($self, $path) = @_;
return $self unless defined $path;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -314,7 +314,7 @@ sub server_write {
return $write;
}

sub _challenge { b64_encode(sha1_bytes(pop() . GUID), '') }
sub _challenge { b64_encode(sha1_bytes((pop() || '') . GUID), '') }

sub _xor_mask {
my ($input, $mask) = @_;
Expand Down
3 changes: 0 additions & 3 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -532,9 +532,6 @@ sub _generate_route {
return $route;
}

# "Stop being such a spineless jellyfish!
# You know full well I'm more closely related to the sea cucumber.
# Not where it counts."
sub _walk_stack {
my ($self, $c) = @_;

Expand Down

0 comments on commit 7ab4592

Please sign in to comment.