NAME
JSON::RPC2::AnyEvent::Server - Yet-another, transport-independent, asynchronous and simple JSON-RPC 2.0 server
SYNOPSIS
use JSON::RPC2::AnyEvent::Server;
my $srv = JSON::RPC2::AnyEvent::Server->new(
hello => "[family_name, first_name]" => sub{ # This wants an array as its argument.
my ($cv, $args) = @_;
my ($family, $given) = @$args;
do_some_async_task(sub{
# Done!
$cv->send("Hello, $given $family!");
});
}
);
my $cv = $srv->dispatch({
jsonrpc => "2.0",
id => 1,
method => 'hello',
params => [qw(Sogoru Kyo Gunner)],
});
my $res = $cv->recv; # { jsonrpc => "2.0", id => 1, result => "Hello, Kyo Sogoru!" }
my $cv = $srv->dispatch({
jsonrpc => "2.0",
id => 2,
method => 'hello',
params => { # You can pass hash as well!
first_name => 'Ryoko',
family_name => 'Kaminagi',
position => 'Wizard'
}
});
my $res = $cv->recv; # { jsonrpc => "2.0", id => 2, result => "Hello, Ryoko Kaminagi!" }
# You can add method separately.
$srv->register(wanthash => '{family_name, first_name}' => sub{
my ($cv, $args, $as_is) = @_;
$cv->send({args => $args, as_is => $as_is});
});
# So, how is params translated?
my $cv = $srv->dispatch({
jsonrpc => "2.0",
id => 3,
method => 'wanthash',
params => [qw(Sogoru Kyo Gunner)],
});
my $res = $cv->recv;
# {
# jsonrpc => "2.0",
# id => 3,
# result => {
# args => { family_name => 'Sogoru', first_name => "Kyo" }, # translated to a hash
# as_is => ['Sogoru', 'Kyo', 'Gunner'], # original value
# },
# }
my $cv = $srv->dispatch({
jsonrpc => "2.0",
id => 4,
method => 'wanthash',
params => {first_name => 'Ryoko', family_name => 'Kaminagi', position => 'Wizard'},
});
my $res = $cv->recv;
# {
# jsonrpc => "2.0",
# id => 4,
# result => {
# args => {first_name => 'Ryoko', family_name => 'Kaminagi', position => 'Wizard'}, # passed as-is
# as_is => {first_name => 'Ryoko', family_name => 'Kaminagi', position => 'Wizard'},
# },
# }
# For Notification Request, just returns undef.
my $cv = $srv->dispatch({
jsonrpc => "2.0",
method => "hello",
params => [qw(Misaki Shizuno)]
});
not defined $cv; # true
DESCRIPTION
JSON::RPC2::AnyEvent::Server provides asynchronous JSON-RPC 2.0 server implementation. This just provides an abstract JSON-RPC layer and you need to combine concrete transport protocol to utilize this module. If you are interested in stream protocol like TCP, refer to JSON::RPC2::AnyEvent::Server::Handle.
THINK SIMPLE
JSON::RPC2::AnyEvent considers JSON-RPC as simple as possible. For example, JSON::RPC2::Server abstracts JSON-RPC server as a kind of hash filter. Unlike JSON::RPC2::Server accepts and outputs serialized JSON text, JSON::RPC2::AnyEvent::Server accepts and outputs Perl hash:
+----------+
| |
Inuput | JSON-RPC | Output
request ---------->| Server |----------> response
(as a hash) | | (as a hash)
+----------+
This has nothing to do with serializing Perl data or deserializing JSON text!
See also JSON::RPC2::AnyEvent for more information.
INTERFACE
CLASS->new( @args )
-> JSON::RPC2::AnyEvent::Server
Create new instance of JSON::RPC2::AnyEvent::Server. Arguments are passed to register
method.
$server->register( $method_name => $argspec => $callback )
-> $self
Registers a subroutine as a JSON-RPC method of $server
.
$server->dispatch( $val )
-> (AnyEvent::Condvar | undef)
Send $val
to $server
and execute corresponding method.
SEE ALSO
LICENSE
Copyright (C) Daisuke (yet another) Maki.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
Daisuke (yet another) Maki <maki.daisuke@gmail.com>