1MojoX::JSON::RPC(3) User Contributed Perl Documentation MojoX::JSON::RPC(3)
2
3
4
6 MojoX::JSON::RPC - a Perl implementation of the JSON-RPC 2.0 protocol
7 for Mojolicious
8
10 Server as a plugin (Mojolicious::Plugin::JsonRpcDispatcher):
11
12 #!/usr/bin/env perl
13 use Mojolicious::Lite;
14 use MojoX::JSON::RPC::Service;
15
16 plugin 'json_rpc_dispatcher' => {
17 services => {
18 '/jsonrpc' => MojoX::JSON::RPC::Service->new->register(
19 'sum',
20 sub {
21 my @params = @_;
22 my $sum = 0;
23 $sum += $_ for @params;
24 return $sum;
25 }
26 )
27 }
28 };
29
30 app->start;
31
32 Client (MojoX::JSON::RPC::Client):
33
34 #!/usr/bin/env perl
35 use MojoX::JSON::RPC::Client;
36
37 my $client = MojoX::JSON::RPC::Client->new;
38 my $url = 'http://www.example.com/jsonrpc';
39 my $callobj = {
40 id => 1,
41 method => 'sum',
42 params => [ 17, 25 ]
43 };
44
45 my $res = $client->call($url, $callobj);
46
47 if($res) {
48 if ($res->is_error) { # RPC ERROR
49 print 'Error : ', $res->error_message;
50 }
51 else {
52 print $res->result;
53 }
54 }
55 else {
56 my $tx_res = $client->tx->res; # Mojo::Message::Response object
57 print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
58 }
59
60 Non-blocking client:
61
62 #!/usr/bin/env perl
63 use MojoX::JSON::RPC::Client;
64
65 my $client = MojoX::JSON::RPC::Client->new;
66 my $url = 'http://www.example.com/jsonrpc';
67 my $callobj = {
68 id => 1,
69 method => 'sum',
70 params => [ 17, 25 ]
71 };
72
73 $client->call($url, $callobj, sub {
74 # With callback
75 my $res = pop;
76
77 if($res) {
78 if ($res->is_error) { # RPC ERROR
79 print 'Error : ', $res->error_message;
80 }
81 else {
82 print $res->result;
83 }
84 }
85 else {
86 my $tx_res = $client->tx->res; # Mojo::Message::Response object
87 print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
88 }
89
90 Mojo::IOLoop->stop;
91 });
92
93 Mojo::IOLoop->start;
94
96 This module implments a client plugin and a server plugin for JSON-RPC
97 2.0 for use with Mojolicious.
98
99 This module follows the draft specficiation for JSON-RPC 2.0. More
100 information can be found at
101 <http://groups.google.com/group/json-rpc/web/json-rpc-2-0>.
102
104 Mojolicious::Plugin::JsonRpcDispatcher, MojoX::JSON::RPC::Dispatcher,
105 MojoX::JSON::RPC::Client
106
108 Henry Tang
109
111 Igor Afanasyev
112
113 Renee Baecker (https://github.com/reneeb)
114
115 Vidar Tyldum <vidar@tyldum.com>
116
118 Copyright (C) 2011-2016, Henry Tang.
119
120 MojoX::JSON::RPC is provided "as is" and without any express or implied
121 warranties, including, without limitation, the implied warranties of
122 merchantibility and fitness for a particular purpose.
123
124 This program is free software, you can redistribute it and/or modify it
125 under the terms of the Artistic License version 2.0.
126
128 Dmitry Karasik <dmitry@karasik.eu.org>
129
130
131
132perl v5.34.0 2022-01-21 MojoX::JSON::RPC(3)