The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

AnyEvent::JSONRPC::Lite::Server - Simple TCP-based JSONRPC server

SYNOPSIS

    use AnyEvent::JSONRPC::Lite::Server;
    
    my $server = AnyEvent::JSONRPC::Lite::Server->new( port => 4423 );
    $server->reg_cb(
        echo => sub {
            my ($res_cv, @params) = @_;
            $res_cv->result(@params);
        },
        sum => sub {
            my ($res_cv, @params) = @_;
            $res_cv->result( $params[0] + $params[1] );
        },
    );

DESCRIPTION

This module is server part of AnyEvent::JSONRPC::Lite.

METHOD

new (%options)

Create server object, start listening socket, and return object.

    my $server = AnyEvent::JSONRPC::Lite::Server->new(
        port => 4423,
    );

Available %options are:

port (Required)

Listening port.

address (Optional)

Bind address. Default to undef: This means server binds all interfaces by default.

handler_options (Optional)

Hashref options of AnyEvent::Handle that is used to handle client connections.

reg_cb (%callbacks)

Register JSONRPC methods.

    $server->reg_cb(
        echo => sub {
            my ($res_cv, @params) = @_;
            $res_cv->result(@params);
        },
        sum => sub {
            my ($res_cv, @params) = @_;
            $res_cv->result( $params[0] + $params[1] );
        },
    );

callback arguments

JSONRPC callback arguments consists of $result_cv, and request @params.

    my ($result_cv, @params) = @_;

$result_cv is AnyEvent::JSONRPC::Lite::CondVar object. Callback must be call <$result_cv-result>> to return result or <$result_cv-error>> to return error.

If $result_cv is not defined, it is notify request, so you don't have to return response. See AnyEvent::JSONRPC::Lite::Client notify method.

@params is same as request parameter.

AUTHOR

Daisuke Murase <typester@cpan.org>

COPYRIGHT AND LICENSE

Copyright (c) 2009 by KAYAC Inc.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.