Skip to content

Commit

Permalink
added kept_alive method to Mojo::Transaction::WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 3, 2012
1 parent d579715 commit f553a5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.72 2012-04-03
- Added kept_alive method to Mojo::Transaction::WebSocket.
- Improved documentation.

2.71 2012-04-03
Expand Down
7 changes: 7 additions & 0 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -119,6 +119,7 @@ sub finish {

sub is_websocket {1}

sub kept_alive { shift->handshake->kept_alive }
sub local_address { shift->handshake->local_address }
sub local_port { shift->handshake->local_port }

Expand Down Expand Up @@ -479,6 +480,12 @@ Finish the WebSocket connection gracefully.
True.
=head2 C<kept_alive>
my $kept_alive = $ws->kept_alive;
Alias for L<Mojo::Transaction/"kept_alive">.
=head2 C<local_address>
my $local_address = $ws->local_address;
Expand Down
13 changes: 4 additions & 9 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -496,20 +496,15 @@ sub _start {
sub _upgrade {
my ($self, $id) = @_;

# No upgrade request
# Check if connection needs to be upgraded
my $c = $self->{connections}->{$id};
my $old = $c->{tx};
return unless $old->req->headers->upgrade;
return unless ($old->res->code || '') eq '101';

# Handshake failed
my $res = $old->res;
return unless ($res->code || '') eq '101';

# Upgrade to WebSocket transaction
# Check challenge and upgrade to WebSocket transaction
my $new = Mojo::Transaction::WebSocket->new(handshake => $old, masked => 1);
$new->kept_alive($old->kept_alive);
$res->error('WebSocket challenge failed.') and return
unless $new->client_challenge;
return unless $new->client_challenge;
$c->{tx} = $new;
weaken $self;
$new->on(resume => sub { $self->_write($id) });
Expand Down

0 comments on commit f553a5c

Please sign in to comment.