Skip to content

Commit

Permalink
removed no_compression method and added with_compression method
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 19, 2014
1 parent 701d26e commit 6f543e3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Changes
Expand Up @@ -10,7 +10,7 @@
- Removed format method from Mojo::Log.
- Removed check_file method from Mojo::Server::Morbo.
- Added format attribute to Mojo::Log.
- Added no_compression method to Mojo::Transaction::WebSocket.
- Added with_compression method to Mojo::Transaction::WebSocket.
- Added append method to Mojo::Log.
- Added check method to Mojo::Server::Morbo.
- Updated jQuery to version 2.1.1.
Expand Down
32 changes: 16 additions & 16 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -156,10 +156,6 @@ sub new {
return $self;
}

sub no_compression {
shift->compressed(0)->res->headers->remove('Sec-WebSocket-Extensions');
}

sub parse_frame {
my ($self, $buffer) = @_;

Expand Down Expand Up @@ -260,11 +256,6 @@ sub server_handshake {
and $res_headers->sec_websocket_protocol($1);
$res_headers->sec_websocket_accept(
_challenge($req_headers->sec_websocket_key));

# "permessage-deflate" extension
$self->compressed(1)
and $res_headers->sec_websocket_extensions('permessage-deflate')
if ($req_headers->sec_websocket_extensions // '') =~ /permessage-deflate/;
}

sub server_read {
Expand All @@ -289,6 +280,16 @@ sub server_write {
return delete $self->{write} // '';
}

sub with_compression {
my $self = shift;

# "permessage-deflate" extension
$self->compressed(1)
and $self->res->headers->sec_websocket_extensions('permessage-deflate')
if ($self->req->headers->sec_websocket_extensions // '')
=~ /permessage-deflate/;
}

sub _challenge { b64_encode(sha1_bytes(($_[0] || '') . GUID), '') }

sub _message {
Expand Down Expand Up @@ -619,13 +620,6 @@ Construct a new L<Mojo::Transaction::WebSocket> object and subscribe to
L</"frame"> event with default message parser, which also handles C<PING> and
C<CLOSE> frames automatically.
=head2 no_compression
$ws->no_compression;
Make sure C<permessage-deflate> extension is deactivated for this WebSocket
connection.
=head2 parse_frame
my $frame = $ws->parse_frame(\$bytes);
Expand Down Expand Up @@ -710,6 +704,12 @@ Read data server-side, used to implement web servers.
Write data server-side, used to implement web servers.
=head2 with_compression
$ws->with_compression;
Negotiate C<permessage-deflate> extension for this WebSocket connection.
=head1 DEBUGGING
You can set the C<MOJO_WEBSOCKET_DEBUG> environment variable to get some
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -570,12 +570,12 @@ L<Mojolicious::Controller/"send">.
The event L<Mojo::Transaction::WebSocket/"finish"> will be emitted right after
the WebSocket connection has been closed.

$self->tx->no_compression;
$self->tx->with_compression;

By default C<permessage-deflate> compression will be used if the client
supports it, this can result in much better performance, but also increases
memory usage by up to 300KB per connection. It can be disabled with
L<Mojo::Transaction::WebSocket/"no_compression">.
You can activate C<permessage-deflate> compression with
L<Mojo::Transaction::WebSocket/"with_compression">, this can result in much
better performance, but also increases memory usage by up to 300KB per
connection.

=head2 Testing WebSocket web services

Expand Down
3 changes: 1 addition & 2 deletions t/mojolicious/websocket_lite_app.t
Expand Up @@ -13,7 +13,7 @@ use Test::Mojo;

websocket '/echo' => sub {
my $self = shift;
$self->tx->max_websocket_size(262145);
$self->tx->max_websocket_size(262145)->with_compression;
$self->on(binary => sub { shift->send({binary => shift}) });
$self->on(
text => sub {
Expand All @@ -27,7 +27,6 @@ get '/echo' => {text => 'plain echo!'};

websocket '/no_compression' => sub {
my $self = shift;
$self->tx->no_compression;
$self->on(binary => sub { shift->send({binary => shift}) });
};

Expand Down

0 comments on commit 6f543e3

Please sign in to comment.