jsonrpc

package module
v0.0.0-...-c9eb695 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2019 License: GPL-3.0 Imports: 11 Imported by: 0

README

jsonrpc

simple jsonrpc 2.0 client

usage

example

package main

import (
    "github.com/fangdingjun/jsonrpc"
    "log"
)

type result struct{
    R1 string `json:"r1"`
    R2 string `json:"r2"`
}

func main(){
    client, _ := jsonrpc.NewClient("http://admin:123@127.0.0.1:2312/jsonrpc")
    // client.Debug = true
    // client.HTTPClient = &http.Client{...}

    var ret result
    var args = []interface{}{1, "a", 2}
    err := client.Call("some_method", args, &ret)
    if err != nil{
        log.Fatal(err)
    }
    log.Println(ret)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConnClosed = errors.New("connection closed")

ErrConnClosed error for connection closed

Functions

This section is empty.

Types

type Client

type Client struct {
	// URL is remote url,
	// example
	//     http://username:password@192.168.1.3:1001/jsonrpc
	//     ws://192.168.0.1:9121/
	URL       string
	Transport Transport
}

Client json rpc client

func NewClient

func NewClient(uri string) (*Client, error)

NewClient create a new jsonrpc client

func (*Client) Call

func (c *Client) Call(method string, args interface{}, reply interface{}) error

Call invoke a method with args and return reply

func (*Client) Subscribe

func (c *Client) Subscribe(method, notifyMethod string,
	args interface{}, reply interface{}) (chan json.RawMessage, chan *Error, error)

Subscribe subscribe for change

type Error

type Error struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data"`
}

Error rpc error

func (*Error) Error

func (e *Error) Error() string

type HTTPTransport

type HTTPTransport struct {
	Client *http.Client
	URL    string

	Mu *sync.Mutex
	// contains filtered or unexported fields
}

HTTPTransport json rpc over http

func (*HTTPTransport) Call

func (h *HTTPTransport) Call(method string, args interface{}, reply interface{}) error

Call call a remote method

func (*HTTPTransport) Subscribe

func (h *HTTPTransport) Subscribe(method string, notifyMethod string,
	args interface{}, reply interface{}) (chan json.RawMessage, chan *Error, error)

Subscribe subscribe for change

type Transport

type Transport interface {
	Call(method string, args interface{}, reply interface{}) error
	Subscribe(method string, notifyMethod string,
		args interface{}, reply interface{}) (chan json.RawMessage, chan *Error, error)
}

Transport json rpc transport

func NewHTTPTransport

func NewHTTPTransport(uri string, client *http.Client) (Transport, error)

NewHTTPTransport create a new http transport

func NewWebsocketTransport

func NewWebsocketTransport(ctx context.Context, uri string) (Transport, error)

NewWebsocketTransport create a new websocket transport

type WebsocketTransport

type WebsocketTransport struct {
	Conn *websocket.Conn
	URL  string
	Mu   *sync.Mutex
	// contains filtered or unexported fields
}

WebsocketTransport json rpc over websocket

func (*WebsocketTransport) Call

func (h *WebsocketTransport) Call(method string, args interface{}, reply interface{}) error

Call call a remote method

func (*WebsocketTransport) Context

func (h *WebsocketTransport) Context() context.Context

Context return the context transport used

func (*WebsocketTransport) Subscribe

func (h *WebsocketTransport) Subscribe(method string, notifyMethod string,
	args interface{}, reply interface{}) (chan json.RawMessage, chan *Error, error)

Subscribe subscribe for change

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL