Skip to content

Simple JSON-RPC 2.0 Specification Client and Server

License

Notifications You must be signed in to change notification settings

go-zoox/jsonrpc

Repository files navigation

JSONRPC Client/Server

According to the JSON-RPC 2.0 specification, JSON-RPC is a lightweight remote procedure call (RPC) protocol.

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/jsonrpc

Getting Started

// server
package main

import (
	"context"

	"github.com/go-zoox/jsonrpc"
	"github.com/go-zoox/jsonrpc/server"
	"github.com/go-zoox/logger"
)

func main() {
	s := server.New()

	s.Register("echo", func(ctx context.Context, params jsonrpc.Params) (jsonrpc.Result, error) {
		logger.Info("params: %s", params)

		return jsonrpc.Result{
			"name": params.Get("name"),
			"age":  18,
		}, nil
	})

	s.Run()
}
// client
package main

import (
	"context"

	"github.com/go-zoox/core-utils/cast"
	"github.com/go-zoox/jsonrpc"
	"github.com/go-zoox/jsonrpc/client"
	"github.com/go-zoox/logger"
)

func main() {
	c := client.New("http://localhost:8080")

	r, err := c.Call(context.Background(), "echo", jsonrpc.Params{
		"name": "zero",
	})
	if err != nil {
		logger.Errorf("failed to call: %s", err)
		return
	}

	logger.Info("result: %d", cast.ToInt64(r.Get("age")))
}

License

GoZoox is released under the MIT License.

About

Simple JSON-RPC 2.0 Specification Client and Server

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages