Skip to content

Commit

Permalink
added experimental binary support to Mojo::Transaction::WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 17, 2011
1 parent 80c2b3b commit f95097e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -4,6 +4,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Deprecated direct hash access to the flash in
Mojolicious::Controller.
- Added EXPERIMENTAL profile helper.
- Added EXPERIMENTAL binary support to Mojo::Transaction::WebSocket.
- Updated WebSocket implementation to ietf-14.
- Improved documentation.
- Fixed small redirect_to bug. (judofyr, sri)
Expand Down
10 changes: 9 additions & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -92,8 +92,13 @@ sub resume {

sub send_message {
my ($self, $message, $cb) = @_;

# Binary
$self->{drain} = $cb if $cb;
$message = '' unless defined $message;
return $self->_send_frame(BINARY, $message->[0]) if ref $message;

# Text
encode 'UTF-8', $message;
$self->_send_frame(TEXT, $message);
}
Expand Down Expand Up @@ -148,6 +153,7 @@ sub server_read {
}

# Append chunk and check message size
$self->{op} = $op unless exists $self->{op};
$self->{message} .= $frame->[2];
$self->finish and last
if length $self->{message} > $self->max_websocket_size;
Expand All @@ -158,7 +164,7 @@ sub server_read {
# Callback
my $message = $self->{message};
$self->{message} = '';
decode 'UTF-8', $message if $message;
decode 'UTF-8', $message if $message && delete $self->{op} == TEXT;
return $self->finish unless my $cb = $self->on_message;
$self->$cb($message);
}
Expand Down Expand Up @@ -518,6 +524,8 @@ Resume transaction.
$ws->send_message('Hi there!');
$ws->send_message('Hi there!', sub {...});
$ws->send_message([$bytes]);
$ws->send_message([$bytes], sub {...});
Send a message over the WebSocket, encoding and framing will be handled
transparently.
Expand Down
4 changes: 4 additions & 0 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -782,6 +782,7 @@ Callback to be invoked when the transaction has been finished.
$c = $c->on_message(sub {...});
Callback to be invoked when new WebSocket messages arrive.
Note that this method is EXPERIMENTAL and might change without warning!
$c->on_message(sub {
my ($c, $message) = @_;
Expand Down Expand Up @@ -959,9 +960,12 @@ Note that this method is EXPERIMENTAL and might change without warning!
$c = $c->send_message('Hi there!');
$c = $c->send_message('Hi there!', sub {...});
$c = $c->send_message([$bytes]);
$c = $c->send_message([$bytes], sub {...});
Send a message via WebSocket, only works if there is currently a WebSocket
connection in progress.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<session>
Expand Down
45 changes: 44 additions & 1 deletion t/mojolicious/websocket_lite_app.t
@@ -1,6 +1,8 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use utf8;

# Disable Bonjour, IPv6 and libev
BEGIN {
$ENV{MOJO_NO_BONJOUR} = $ENV{MOJO_NO_IPV6} = 1;
Expand All @@ -9,9 +11,10 @@ BEGIN {

# "Oh, dear. She’s stuck in an infinite loop and he’s an idiot.
# Well, that’s love for you."
use Test::More tests => 41;
use Test::More tests => 59;

# "Your mistletoe is no match for my *tow* missile."
use Mojo::ByteStream 'b';
use Mojolicious::Lite;
use Test::Mojo;

Expand All @@ -37,6 +40,28 @@ websocket '/push' => sub {
$self->on_finish(sub { Mojo::IOLoop->drop($id) });
};

# WebSocket /unicode
websocket '/unicode' => sub {
my $self = shift;
$self->on_message(
sub {
my ($self, $message) = @_;
$self->send_message("♥: $message");
}
);
};

# WebSocket /bytes
websocket '/bytes' => sub {
my $self = shift;
$self->on_message(
sub {
my ($self, $message) = @_;
$self->send_message([$message]);
}
);
};

# "I was a hero to broken robots 'cause I was one of them, but how can I sing
# about being damaged if I'm not?
# That's like Christina Aguilera singing Spanish.
Expand All @@ -52,6 +77,10 @@ $t->websocket_ok('/echo')->send_message_ok('hello again')
->message_is('echo: hello again')->send_message_ok('and one more time')
->message_is('echo: and one more time')->finish_ok;

# WebSocket /echo (zero)
$t->websocket_ok('/echo')->send_message_ok(0)->message_is('echo: 0')
->finish_ok;

# GET /plain
$t->get_ok('/plain')->status_is(200)->content_is('Nothing to see here!');

Expand All @@ -77,3 +106,17 @@ $t->websocket_ok('/echo')->send_message_ok('this')->send_message_ok('just')

# GET /plain (and again)
$t->get_ok('/plain')->status_is(200)->content_is('Nothing to see here!');

# WebSocket /unicode
$t->websocket_ok('/unicode')->send_message_ok('hello')
->message_is('♥: hello')->finish_ok;

# WebSocket /unicode (multiple times)
$t->websocket_ok('/unicode')->send_message_ok('hello again')
->message_is('♥: hello again')->send_message_ok('and one ☃ more time')
->message_is('♥: and one ☃ more time')->finish_ok;

# WebSocket /bytes
my $bytes = b("I ♥ Mojolicious")->encode('UTF-16LE')->to_string;
$t->websocket_ok('/bytes')->send_message_ok([$bytes])->message_is($bytes)
->finish_ok;

0 comments on commit f95097e

Please sign in to comment.