AnyEvent-STOMP

 view release on metacpan or  search on metacpan

lib/AnyEvent/STOMP.pm  view on Meta::CPAN


The port number to connect to.  If not specified, defaults to 61612 (if SSL/TLS
is used) or 61613 (if not).

=item ssl

If set to a true value, use SSL/TLS to connect.

=item destination

If defined, subscribe to the specified destination (queue) upon connection.

=item ack

Sets the behavior with respect to STOMP frame acknowledgments.

If this value is 0 or undef, no acknowledgment is required: the server will
consider all sent frames to be delivered, regardless of whether the client has
actually received them.  (This is the default behavior according to the STOMP
protocol.)

If set to C<auto>, the client will automatically acknowledge a frame upon
receipt.

If set to C<manual>, the caller must acknowledge frames manually via the
ack() method.

=item connect_headers

An anonymous hash of headers (key/value pairs) to send in the STOMP CONNECT
frame.

=item subscribe_headers

An anonymous hash of headers (key/value pairs) to send in the STOMP SUBSCRIBE
frame.

=back

=cut

sub connect {
    my $class = shift;
    my ($host, $port, $ssl, $destination, $ack,
        $connect_headers, $subscribe_headers) = @_;

    croak 'No host provided' unless $host;
    croak "ack value must be 0, undef, 'auto' or 'manual'"
        if $ack && $ack ne 'auto' && $ack ne 'manual';

    my $self = $class->SUPER::new;

    $self->{ack} = $ack;

    $port ||= ($ssl ? 61612 : 61613);

    my $connect_cb;
    $self->{handle} = AnyEvent::Handle->new(
        connect => [ $host, $port ],
        tls => $ssl ? 'connect' : undef,
        keepalive => 1,
        on_prepare => sub { $self->event('prepare', @_); },
        on_connect => sub {
            $self->event('connect', @_);
            $self->send_frame('CONNECT', undef, $connect_headers);
            if ($destination) {
                $subscribe_headers->{destination} = $destination;
                $subscribe_headers->{ack} = 'client' if $ack;
                $connect_cb = $self->reg_cb(CONNECTED => sub {
                        $self->{session_id} = $_[2]->{session};
                        $self->send_frame('SUBSCRIBE',
                            undef, $subscribe_headers);
                        undef $connect_cb;
                });
            }
        },
        on_connect_error => sub {
            $self->event('connect_error', $_[1]);
        },
        on_error => sub {
            $self->unreg_cb($connect_cb) if (defined $connect_cb);
            $self->{handle}->destroy;
            $self->event('io_error', $_[2]);
        },
        on_read => sub { $self->_receive_frame },
    );
    return bless($self, $class);
}

=head2 Sending a message

To send a message, just call send() with the body, the destination (queue)
name, and (optionally) any additional headers:

 $client->send($body, $destination, $headers); # headers may be undef

=cut

sub send {
    my $self = shift;
    my ($body, $destination, $headers) = @_;

    croak 'Missing destination' unless defined $destination;

    $headers->{destination} = $destination;
    $self->send_frame('SEND', $body, $headers);
}

=head2 Sending STOMP frames

You can also send arbitrary STOMP frames:

 $client->send_frame($command, $body, $headers); # headers may be undef

See the STOMP protocol documentation for more details on valid commands and
headers.

=head3 Content Length

The C<content-length> header is special because it is sometimes used to
indicate the length of the body but also the JMS type of the message in



( run in 0.864 second using v1.01-cache-2.11-cpan-df04353d9ac )