1MojoX::JSON::RPC::ServiUcsee(r3)Contributed Perl DocumenMtoajtoiXo:n:JSON::RPC::Service(3)
2
3
4
6 MojoX::JSON::RPC::Service - JSON RPC Service registration
7
9 use MojoX::JSON::RPC::Service;
10
11 my $svc = MojoX::JSON::RPC::Service->new;
12
13 $svc->register(
14 'sum',
15 sub {
16 my @params = @_;
17 my $sum = 0;
18 $sum += $_ for @params;
19 return $sum;
20 }
21 );
22
23 $svc->register(
24 'remote_address',
25 sub {
26 my $tx = shift;
27 return $tx->remote_address;
28 },
29 {
30 with_mojo_tx => 1
31 }
32 );
33
34
35 ## Then in Mojolicious application
36 $self->plugin(
37 'json_rpc_dispatcher',
38 services => {
39 '/jsonrpc' => $svc,
40 }
41 );
42
43 This package can also be used as a base class to make it easy to create
44 object-oriented JSON-RPC applications:
45
46 package MyService;
47
48 use Mojo::Base 'MojoX::JSON::RPC::Service';
49
50 sub sum {
51 my ($self, @params) = @_;
52 my $sum = 0;
53 $sum += $_ for @params;
54 return $sum;
55 }
56
57 __PACKAGE__->register_rpc_method_names( 'sum' );
58
59 ## Then in Mojolicious application
60 $self->plugin(
61 'json_rpc_dispatcher',
62 services => {
63 '/jsonrpc' => MyService->new,
64 }
65 );
66
68 Register JSON-RPC service calls.
69
71 MojoX::JSON::RPC::Service inherits all methods from Mojo::Base and
72 implements the following new ones.
73
74 "register"
75 Register RPC methods.
76
77 $svc->register(
78 'sum',
79 sub {
80 my @params = @_;
81 my $sum = 0;
82 $sum += $_ for @params;
83 return $sum;
84 }
85 );
86
87 with_mojo_tx can be passed as options. In that case, Mojo::Transaction
88 object will be pass as first argument of the subroutine.
89
90 $svc->register(
91 'remote_address',
92 sub {
93 my $tx = shift;
94 return $tx->remote_address;
95 },
96 {
97 with_mojo_tx => 1
98 }
99 );
100
101 "register_rpc_method_names"
102 Class method. Register a list of methods as JSON-RPC calls.
103
104 __PACKAGE__->register_rpc_method_names( 'sum', 'multiply' );
105
107 MojoX::JSON::RPC::Dispatcher
108
109
110
111perl v5.32.1 2021-01-27 MojoX::JSON::RPC::Service(3)