Skip to content

Commit

Permalink
renamed send_message methods to send in multiple classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 24, 2012
1 parent 72e3bbc commit 6bc5b15
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 124 deletions.
6 changes: 5 additions & 1 deletion Changes
@@ -1,8 +1,12 @@
This file documents the revision history for Perl extension Mojolicious.

2.52 2012-02-23 00:00:00
2.52 2012-02-24 00:00:00
- Removed experimental status from config method in Mojo.
- Renamed send_message method in Mojolicious::Controller to send.
- Renamed send_message_ok method in Test::mojo to send_ok.
- Renamed to_psgi method in Mojo::Server::PSGI to to_psgi_app.
- Combined send_message and send_frame methods in
Mojo::Transaction::WebSocket to send.
- Improved emit_chain method in Mojolicious::Plugins to allow
reforwarding. (jamadam)
- Improved documentation.
Expand Down
2 changes: 1 addition & 1 deletion README.pod
Expand Up @@ -116,7 +116,7 @@ Web development for humans, making hard things possible and everything fun.
my $self = shift;
$self->on(message => sub {
my ($self, $message) = @_;
$self->send_message("echo: $message");
$self->send("echo: $message");
});
};

Expand Down
3 changes: 1 addition & 2 deletions examples/websocket.pl
Expand Up @@ -7,8 +7,7 @@
# Not where it counts."
any '/' => sub {
my $self = shift;
$self->on(message => sub { shift->send_message(shift) })
if $self->tx->is_websocket;
$self->on(message => sub { shift->send(shift) }) if $self->tx->is_websocket;
} => 'websocket';

# Minimal WebSocket application for browser testing
Expand Down
62 changes: 25 additions & 37 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -120,7 +120,7 @@ sub connection { shift->handshake->connection(@_) }

sub finish {
my $self = shift;
$self->send_frame(1, 0, 0, 0, CLOSE, '');
$self->send([1, 0, 0, 0, CLOSE, '']);
$self->{finished} = 1;
return $self;
}
Expand Down Expand Up @@ -223,35 +223,30 @@ sub resume {
return $self;
}

sub send_frame {
my ($self, $fin, $rsv1, $rsv2, $rsv3, $op, $payload, $cb) = @_;
sub send {
my ($self, $frame, $cb) = @_;

# Binary or raw text
if (ref $frame && ref $frame eq 'HASH') {
$frame =
exists $frame->{text}
? [1, 0, 0, 0, TEXT, $frame->{text}]
: [1, 0, 0, 0, BINARY, $frame->{binary}];
}

# Text
elsif (!ref $frame) { $frame = [1, 0, 0, 0, TEXT, encode('UTF-8', $frame)] }

# Prepare frame
$self->once(drain => $cb) if $cb;
$self->{write} //= '';
$self->{write}
.= $self->build_frame($fin, $rsv1, $rsv2, $rsv3, $op, $payload);
$self->{write} .= $self->build_frame(@$frame);
$self->{state} = 'write';

# Resume
$self->emit('resume');
}

sub send_message {
my ($self, $m, $cb) = @_;
$m //= '';

# Binary or raw text
if (ref $m) {
return $self->send_frame(1, 0, 0, 0, TEXT, $m->[1], $cb)
if $m->[0] eq 'text';
return $self->send_frame(1, 0, 0, 0, BINARY, $m->[1], $cb);
}

# Text
$self->send_frame(1, 0, 0, 0, TEXT, encode('UTF-8', $m), $cb);
}

sub server_handshake {
my $self = shift;

Expand Down Expand Up @@ -283,7 +278,7 @@ sub server_read {
my $op = $frame->[4] || CONTINUATION;

# Ping/Pong
$self->send_frame(1, 0, 0, 0, PONG, $frame->[5]) and next if $op == PING;
$self->send([1, 0, 0, 0, PONG, $frame->[5]]) and next if $op == PING;
next if $op == PONG;

# Close
Expand Down Expand Up @@ -377,7 +372,7 @@ Emitted once all data has been sent.
$ws->on(drain => sub {
my $ws = shift;
$ws->send_message(time);
$ws->send(time);
});
=head2 C<frame>
Expand Down Expand Up @@ -546,23 +541,16 @@ Alias for L<Mojo::Transaction/"res">.
Alias for L<Mojo::Transaction/"resume">.
=head2 C<send_frame>
$ws->send_frame($fin, $rsv1, $rsv2, $rsv3, $op, $payload);
$ws->send_frame($fin, $rsv1, $rsv2, $rsv3, $op, $payload, sub {...});
Send a single frame non-blocking via WebSocket, the optional drain callback
will be invoked once all data has been written.
=head2 C<send_message>
=head2 C<send>
$ws->send_message([binary => $bytes]);
$ws->send_message([text => $bytes]);
$ws->send_message('Hi there!');
$ws->send_message('Hi there!', sub {...});
$ws->send({binary => $bytes});
$ws->send({text => $bytes});
$ws->send([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]);
$ws->send('Hi there!');
$ws->send('Hi there!' => sub {...});
Send a message non-blocking via WebSocket, the optional drain callback will
be invoked once all data has been written.
Send a message or single frame non-blocking via WebSocket, the optional drain
callback will be invoked once all data has been written.
=head2 C<server_handshake>
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -621,7 +621,7 @@ Mojo::UserAgent - Non-blocking I/O HTTP 1.1 and WebSocket user agent
say $message;
$tx->finish;
});
$tx->send_message('hi there!');
$tx->send('hi there!');
});
Mojo::IOLoop->start;
Expand Down Expand Up @@ -1002,7 +1002,7 @@ that this method is EXPERIMENTAL and might change without warning!
my ($tx, $message) = @_;
say "$message\n";
});
$tx->send_message('Hi!');
$tx->send('Hi!');
});
Mojo::IOLoop->start;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -73,7 +73,7 @@ sub new {
$r->hide(qw/param redirect_to render render_content render_data/);
$r->hide(qw/render_exception render_json render_not_found render_partial/);
$r->hide(qw/render_static render_text rendered req res respond_to/);
$r->hide(qw/send_message session signed_cookie stash tx ua url_for write/);
$r->hide(qw/send session signed_cookie stash tx ua url_for write/);
$r->hide('write_chunk');

# Prepare log
Expand Down
23 changes: 12 additions & 11 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -428,13 +428,13 @@ sub respond_to {
ref $target eq 'CODE' ? $target->($self) : $self->render($target);
}

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

my $tx = $self->tx;
Carp::croak('No WebSocket connection to send message to')
unless $tx->is_websocket;
$tx->send_message($message, sub { shift and $self->$cb(@_) if $cb });
$tx->send($message, sub { shift and $self->$cb(@_) if $cb });
$self->rendered(101);

return $self;
Expand Down Expand Up @@ -912,19 +912,20 @@ defaults to rendering an empty C<204> response.
any => {data => '', status => 204}
);
=head2 C<send_message>
=head2 C<send>
$c = $c->send_message([binary => $bytes]);
$c = $c->send_message([text => $bytes]);
$c = $c->send_message('Hi there!');
$c = $c->send_message('Hi there!', sub {...});
$c = $c->send({binary => $bytes});
$c = $c->send({text => $bytes});
$c = $c->send([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]);
$c = $c->send('Hi there!');
$c = $c->send('Hi there!', sub {...});
Send a message non-blocking via WebSocket, the optional drain callback will
be invoked once all data has been written. Note that this method is
EXPERIMENTAL and might change without warning!
Send a message or single frame non-blocking via WebSocket, the optional drain
callback will be invoked once all data has been written. Note that this
method is EXPERIMENTAL and might change without warning!
# Send JSON object as text frame
$c->send_message([text => Mojo::JSON->new->encode({hello => 'world'})]);
$c->send({text => Mojo::JSON->new->encode({hello => 'world'})});
=head2 C<session>
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -411,7 +411,7 @@ subscribing to the C<message> event of the transaction.
# Incoming message
$self->on(message => sub {
my ($self, $message) = @_;
$self->send_message("echo: $message");
$self->send("echo: $message");
});

# Disconnected
Expand Down Expand Up @@ -464,7 +464,7 @@ L<Test::Mojo> API to be used.
# Test echo web service
my $t = Test::Mojo->new;
$t->websocket_ok('/echo')
->send_message_ok('Hello Mojo!')
->send_ok('Hello Mojo!')
->message_is('echo: Hello Mojo!')
->finish_ok;

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -613,7 +613,7 @@ You can restrict access to WebSocket handshakes using the C<websocket> method.
my $self = shift;
$self->on(message => sub {
my ($self, $message) = @_;
$self->send_message("echo: $message");
$self->send("echo: $message");
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -713,7 +713,7 @@ WebSocket applications have never been this easy before.
my $self = shift;
$self->on(message => sub {
my ($self, $message) = @_;
$self->send_message("echo: $message");
$self->send("echo: $message");
});
};
Expand Down
19 changes: 11 additions & 8 deletions lib/Test/Mojo.pm
Expand Up @@ -260,10 +260,10 @@ sub reset_session {
return $self;
}

sub send_message_ok {
sub send_ok {
my ($self, $message, $desc) = @_;

$self->tx->send_message($message, sub { Mojo::IOLoop->stop });
$self->tx->send($message, sub { Mojo::IOLoop->stop });
Mojo::IOLoop->start;
local $Test::Builder::Level = $Test::Builder::Level + 1;
Test::More::ok 1, $desc || 'send message';
Expand Down Expand Up @@ -400,7 +400,7 @@ Test::Mojo - Testing Mojo!
->json_is('/results/4/title' => 'Perl rocks!');
$t->websocket_ok('/echo')
->send_message_ok('hello')
->send_ok('hello')
->message_is('echo: hello')
->finish_ok;
Expand Down Expand Up @@ -679,13 +679,16 @@ arguments as L<Mojo::UserAgent/"put">.
Reset user agent session.
=head2 C<send_message_ok>
=head2 C<send_ok>
$t = $t->send_message_ok('hello');
$t = $t->send_message_ok('hello', 'sent successfully');
$t = $t->send_ok({binary => $bytes});
$t = $t->send_ok({text => $bytes});
$t = $t->send_ok([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]);
$t = $t->send_ok('hello');
$t = $t->send_ok('hello', 'sent successfully');
Send C<WebSocket> message. Note that this method is EXPERIMENTAL and might
change without warning!
Send C<WebSocket> message or single frame. Note that this method is
EXPERIMENTAL and might change without warning!
=head2 C<status_is>
Expand Down

0 comments on commit 6bc5b15

Please sign in to comment.