Documentation
¶
Overview ¶
Package jsonrpc implements a JSON-RPC 2.0 client over an io.ReadWriter (e.g. stdio). It supports bidirectional RPC: both the caller and the remote peer can initiate requests, and either side may send notifications (messages without an id).
Index ¶
- type Client
- func (c *Client) Call(ctx context.Context, method string, params interface{}) (json.RawMessage, error)
- func (c *Client) Listen(ctx context.Context) error
- func (c *Client) Notify(method string, params interface{}) error
- func (c *Client) RegisterNotificationHandler(method string, h NotificationHandler)
- func (c *Client) RegisterRequestHandler(method string, h RequestHandler)
- func (c *Client) Respond(id json.RawMessage, result interface{}, rpcErr *RPCError) error
- type Message
- type NotificationHandler
- type RPCError
- type RequestHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a bidirectional JSON-RPC 2.0 client that communicates over a pair of io.Reader/io.Writer (typically stdin/stdout of a subprocess).
func (*Client) Call ¶
func (c *Client) Call(ctx context.Context, method string, params interface{}) (json.RawMessage, error)
Call sends a JSON-RPC request and waits for the response.
func (*Client) Listen ¶
Listen starts the receive loop. It blocks until the context is cancelled or the underlying reader returns an error (e.g. EOF on process exit). Call this in a goroutine.
func (*Client) RegisterNotificationHandler ¶
func (c *Client) RegisterNotificationHandler(method string, h NotificationHandler)
RegisterNotificationHandler registers a handler for inbound notifications.
func (*Client) RegisterRequestHandler ¶
func (c *Client) RegisterRequestHandler(method string, h RequestHandler)
RegisterRequestHandler registers a handler for inbound requests from the remote peer (bidirectional RPC – the peer initiates the call).
type Message ¶
type Message struct {
JSONRPC string `json:"jsonrpc"`
ID *json.RawMessage `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *RPCError `json:"error,omitempty"`
}
Message is the union type for all JSON-RPC 2.0 messages.
type NotificationHandler ¶
type NotificationHandler func(params json.RawMessage)
NotificationHandler handles an inbound notification (no response expected).
type RPCError ¶
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data,omitempty"`
}
RPCError is a JSON-RPC 2.0 error object.
type RequestHandler ¶
type RequestHandler func(ctx context.Context, params json.RawMessage) (interface{}, error)
RequestHandler handles an inbound request from the remote peer. It must return a result (to be serialised) or an error.