Skip to content

Commit

Permalink
test WebSocket ping/pong
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 25, 2011
1 parent 4875ed2 commit bcf436b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -272,13 +272,15 @@ sub server_read {
$self->emit(frame => $frame);
my $op = $frame->[1] || CONTINUATION;

# Ping
# Ping/Pong
$self->send_frame(1, PONG, $frame->[2]) and next if $op == PING;
next if $op == PONG;

# Close
$self->finish and next if $op == CLOSE;

# Append chunk and check message size
next unless $self->has_subscribers('message');
$self->{op} = $op unless exists $self->{op};
$self->{message} .= $frame->[2];
$self->finish and last
Expand Down
20 changes: 19 additions & 1 deletion t/mojo/websocket.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 44;
use Test::More tests => 45;

# "I can't believe it! Reading and writing actually paid off!"
use IO::Socket::INET;
Expand Down Expand Up @@ -483,3 +483,21 @@ $ua->websocket(
);
$loop->start;
is $result, 'hi' x 200000, 'right result';

# WebSocket /echo (ping/pong)
my $pong;
$ua->websocket(
'/echo' => sub {
my $tx = pop;
$tx->on(
frame => sub {
my ($tx, $frame) = @_;
$pong = $frame->[2] if $frame->[1] == 10;
Mojo::IOLoop->stop;
}
);
$tx->send_frame(1, 9, 'test');
}
);
Mojo::IOLoop->start;
is $pong, 'test', 'received pong with payload';

0 comments on commit bcf436b

Please sign in to comment.