Skip to content
This repository was archived by the owner on Apr 30, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/Protocol/WebSocket/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use Protocol::WebSocket::URL;
use Protocol::WebSocket::Handshake::Client;
use Protocol::WebSocket::Frame;

use Encode ();

sub new {
my $class = shift;
$class = ref $class if ref $class;
Expand Down Expand Up @@ -103,9 +105,15 @@ sub read {
while (defined (my $bytes = $frame_buffer->next)) {
if ($frame_buffer->is_close) {
# Remote WebSocket close (TCP socket may stay open for a bit)
$self->disconnect if ($self->is_ready);
# TODO: see message in disconnect() about error code / reason
$self->{on_eof}->($self) if $self->{on_eof};
# Decode the error code and message, if it exists
my $code = length $bytes > 1 ? unpack('n', substr($bytes, 0, 2)) : undef;
my $message = length $bytes > 3 ? Encode::decode('UTF-8', substr($bytes, 2)) : undef;

# Spec says to send our own close frame (and echo the errno.)
$self->disconnect($code) if ($self->is_ready);

# Call user callback
$self->{on_eof}->($self, $code, $message) if $self->{on_eof};
} elsif ($frame_buffer->is_pong) {
# Server responded to our ping.
$self->{on_pong}->($self, $bytes) if $self->{on_pong};
Expand Down Expand Up @@ -162,11 +170,13 @@ sub connect {
# also sets state to -1 when called
sub disconnect {
my $self = shift;
my ($code, $message) = @_;

my $buffer;
if (defined $code) { $buffer = pack 'n', $code }
if (defined $message) { $buffer .= Encode::encode('UTF-8', $message) }

# TODO: Spec states 'close' messages may contain a uint16 error code, and a utf-8 reason.
# Clients are supposed to echo back the error code when receiving close from server.
# For now, we just send an empty message.
$self->write( $self->_build_frame(type => 'close', masked => 1) );
$self->write( $self->_build_frame(type => 'close', masked => 1, buffer => $buffer) );

$self->{state} = -1;

Expand Down
6 changes: 5 additions & 1 deletion lib/Protocol/WebSocket/Frame.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ sub next {
my $bytes = $self->next_bytes;
return unless defined $bytes;

return Encode::decode('UTF-8', $bytes);
if ($self->{version} eq 'draft-ietf-hybi-00' || $self->is_text) {
return Encode::decode('UTF-8', $bytes);
} else {
return $bytes;
}
}

sub fin {
Expand Down