POE::Component::Server::JSONRPC - POE tcp based JSON-RPC server
POE::Component::Server::JSONRPC->new( Port => 3000, Handler => { 'echo' => 'echo', 'sum' => 'sum', }, ); sub echo { my ($kernel, $jsonrpc, @params) = @_[KERNEL, ARG0..$#_ ]; $kernel->post( $jsonrpc => 'result' => @params ); } sub sum { my ($kernel, $jsonrpc, @params) = @_[KERNEL, ARG0..$#_ ]; $kernel->post( $jsonrpc => 'result' => $params[0] + $params[1] ); }
This module is a POE component for tcp based JSON-RPC Server.
The specification is defined on http://json-rpc.org/ and this module use JSON-RPC 1.0 spec (1.1 does not cover tcp streams)
Create JSONRPC component session and return the session id.
Parameters:
Port number for listen.
Hash variable contains handler name as key, handler poe state name as value.
Handler name (key) is used as JSON-RPC method name.
So if you send {"method":"echo"}, this module call the poe state named "echo".
A session id of PoCo::Server::JSONRPC itself.
JSONRPC argguments
ex) If you send following request
{"method":"echo", "params":["foo", "bar"]}
then, "echo" handler is called and parameters is that ARG0 is component session id, ARG1 "foo", ARG2 "bar".
You must call either "result" or "error" state in your handlers to response result or error.
ex:
$kernel->post( $component_session_id => "result" => "result value" )
$component_session_id is ARG0 in handler. If you do above, response is:
{"result":"result value", "error":""}
Inner method for POE states.
Daisuke Murase <typester@cpan.org>
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.
To install POE::Component::Server::JSONRPC, copy and paste the appropriate command in to your terminal.
cpanm
cpanm POE::Component::Server::JSONRPC
CPAN shell
perl -MCPAN -e shell install POE::Component::Server::JSONRPC
For more information on module installation, please visit the detailed CPAN module installation guide.