Giter VIP home page Giter VIP logo

mojox-json-rpc's Introduction

NAME

MojoX::JSON::RPC - Perl implementation of JSON-RPC 2.0 protocol for Mojolicious

SYNOPSIS

Server as plugin (Mojolicious::Plugin::JsonRpcDispatcher):

#!/usr/bin/env perl
use Mojolicious::Lite;
use MojoX::JSON::RPC::Service;

plugin 'json_rpc_dispatcher' => {
    services => {
        '/jsonrpc' => MojoX::JSON::RPC::Service->new->register(
            'sum',
            sub {
                my @params = @_;
                my $sum    = 0;
                $sum += $_ for @params;
                return $sum;
            }
        )
    }
};

app->start;

Client (MojoX::JSON::RPC::Client):

#!/usr/bin/env perl
use MojoX::JSON::RPC::Client;

my $client = MojoX::JSON::RPC::Client->new;
my $url    = 'http://www.example.com/jsonrpc';
my $callobj = {
    id      => 1,
    method  => 'sum',
    params  => [ 17, 25 ]
};

my $res = $client->call($url, $callobj);

if($res) {
    if ($res->is_error) { # RPC ERROR
        print 'Error : ', $res->error_message;
    }
    else {
        print $res->result;
    }
}
else {
    my $tx_res = $client->tx->res; # Mojo::Message::Response object
    print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
}

Non-blocking client:

#!/usr/bin/env perl
use MojoX::JSON::RPC::Client;

my $client = MojoX::JSON::RPC::Client->new;
my $url    = 'http://www.example.com/jsonrpc';
my $callobj = {
    id      => 1,
    method  => 'sum',
    params  => [ 17, 25 ]
};

$client->call($url, $callobj, sub {
    # With callback
    my $res = pop;

    if($res) {
        if ($res->is_error) { # RPC ERROR
            print 'Error : ', $res->error_message;
        }
        else {
            print $res->result;
        }
    }
    else {
       my $tx_res = $client->tx->res; # Mojo::Message::Response object
       print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
    }

    Mojo::IOLoop->stop;
});

Mojo::IOLoop->start;

DESCRIPTION

This module implments a client and a server plugin for JSON-RPC 2.0 for use with Mojolicious.

This module follows the draft specficiation for JSON-RPC 2.0. More information can be found at http://groups.google.com/group/json-rpc/web/json-rpc-2-0.

SEE ALSO

Mojolicious::Plugin::JsonRpcDispatcher, MojoX::JSON::RPC::Dispatcher, MojoX::JSON::RPC::Client

AUTHOR

Henry Tang

CREDITS

    Igor Afanasyev

COPYRIGHT & LICENSE

Copyright (C) 2011-2014, Henry Tang.

MojoX::JSON::RPC is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

mojox-json-rpc's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.