logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

POE::Component::Server::JSONRPC - POE tcp or http based JSON-RPC server

Description

       This module is a POE component for tcp or http 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)

Handler Parameters

       ARG0
           A session id of PoCo::Server::JSONRPC itself.

       ARG1
           The id of the client you're treating, send that back in result/error.

       ARG2 .. ARGN
           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 is client id,
       ARG2 "foo", ARG3 "bar".

Handler Response

       You must call either "result" or "error" state in your handlers to response result or error.

       ex:

          $kernel->post( $component_session_id => "result" => $client_id, "result value" )

       $component_session_id is ARG0 in handler. If you do above, response is:

          {"result":"result value", "error":""}

Methods

new
       Create JSONRPC component session and return the session id.

       Parameters:

       Port
           Port number for listen.

       Handler
           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".

Name

       POE::Component::Server::JSONRPC - POE tcp or http based JSON-RPC server

Poe Methods

       Inner method for POE states.

   poe__startpoe_init_server
       Should be defined in Http or Tcp

   poe_input_handlerpoe_resultpoe_errorpoe_send
       Should be defined in Http or Tcp

       Côme Chilliet <mcmic@cpan.org> Daisuke Murase <typester@cpan.org>

Synopsis

           #http version:
           POE::Component::Server::JSONRPC::Http->new(
               # consider using something like JSON::MaybeXS for better performance
               json    => JSON::MaybeXS->new->utf8->allow_nonref,
               Port    => 3000,
               Handler => {
                   'echo' => 'echo',
                   'sum'  => 'sum',
               },
               SslKey  => '/path/to/the/server.key',
               SslCert => '/path/to/the/server.crt',
               Authenticate => \&authentication_handler,
               # authentication_handler must be a function that takes two parameters login and password,
               # and returns true if connection is successful, false otherwise
           );

           #tcp version:
           POE::Component::Server::JSONRPC::Tcp->new(
               Port    => 3000,
               Handler => {
                   'echo' => 'echo',
                   'sum'  => 'sum',
               },
           );

           sub echo {
               my ($kernel, $jsonrpc, $id, @params) = @_[KERNEL, ARG0..$#_ ];

               $kernel->post( $jsonrpc => 'result' => $id, @params );
           }

           sub sum {
               my ($kernel, $jsonrpc, $id, @params) = @_[KERNEL, ARG0..$#_ ];

               $kernel->post( $jsonrpc => 'result' => $id, $params[0] + $params[1] );
           }

See Also