Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add full JSON roundtrip to WebSocket example
  • Loading branch information
kraih committed Jan 14, 2013
1 parent c2f218e commit 0cedac1
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions examples/websocket.pl
Expand Up @@ -5,8 +5,14 @@

any '/' => sub {
my $self = shift;
$self->on(text => sub { shift->send(j(shift)->{test}) })
if $self->tx->is_websocket;
$self->on(
text => sub {
my ($self, $message) = @_;
my $hash = j($message);
$hash->{test} = "$hash->{test}";
$self->send({text => j($hash)});
}
) if $self->tx->is_websocket;
} => 'websocket';

# Minimal WebSocket application for browser testing
Expand All @@ -26,11 +32,10 @@
}
if(typeof(ws) !== 'undefined') {
function wsmessage(event) {
data = event.data;
alert(data);
alert(JSON.parse(event.data).test);
}
function wsopen(event) {
ws.send(JSON.stringify({test: "WebSocket support works! ♥"}));
ws.send(JSON.stringify({test: "WebSocket support works! ♥"}));
}
ws.onmessage = wsmessage;
ws.onopen = wsopen;
Expand Down

0 comments on commit 0cedac1

Please sign in to comment.