Skip to content

Commit

Permalink
just document the current behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 8, 2015
1 parent 04f92e7 commit 2b6dabd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 0 additions & 2 deletions Changes
@@ -1,7 +1,5 @@

6.08 2015-04-08
- Improved finish method in Mojolicious::Controller to work before the
WebSocket connection has actually been established.

6.07 2015-04-07
- Fixed Windows bug in "daemon.t".
Expand Down
8 changes: 7 additions & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -107,7 +107,7 @@ sub finish {

# WebSocket
my $tx = $self->tx;
$tx->finish(@_) and return $self->rendered(101) if $tx->is_websocket;
$tx->finish(@_) and return $self if $tx->is_websocket;

# Chunked stream
return @_ ? $self->write_chunk(@_)->write_chunk('') : $self->write_chunk('')
Expand Down Expand Up @@ -509,6 +509,9 @@ sharing the same name as an array reference.
Close WebSocket connection or long poll stream gracefully.
# Accept WebSocket handshake and close WebSocket connection immediately
$c->rendered(101)->finish(1001);
=head2 flash
my $foo = $c->flash('foo');
Expand Down Expand Up @@ -811,6 +814,9 @@ will be invoked once all data has been written.
$c->send('Second message!');
});
# Accept WebSocket handshake, send a message and close connection immediately
$c->rendered(101)->send('I ♥ Mojolicious!')->finish;
For mostly idle WebSockets you might also want to increase the inactivity
timeout with L<Mojolicious::Plugin::DefaultHelpers/"inactivity_timeout">, which
usually defaults to C<15> seconds.
Expand Down
9 changes: 8 additions & 1 deletion t/mojolicious/websocket_lite_app.t
Expand Up @@ -84,7 +84,10 @@ websocket '/once' => sub {
);
};

websocket '/close' => sub { shift->finish(1001) };
websocket '/close' => sub { shift->rendered(101)->finish(1001) };

websocket '/one_sided' =>
sub { shift->rendered(101)->send('I ♥ Mojolicious!')->finish };

under '/nested';

Expand Down Expand Up @@ -295,6 +298,10 @@ $t->websocket_ok('/once')->send_ok('hello')
# WebSocket connection gets closed right away
$t->websocket_ok('/close')->finished_ok(1001);

# WebSocket connection gets closed after one message
$t->websocket_ok('/one_sided')->message_ok->message_is('I ♥ Mojolicious!')
->finished_ok(1005);

# Nested WebSocket
$t->websocket_ok('/nested')->send_ok('hello')
->message_ok->message_is('nested echo: hello')->finished_ok(1000);
Expand Down

0 comments on commit 2b6dabd

Please sign in to comment.