Documentation ¶
Overview ¶
Package wsrpc provides a partial implementation of a JSON-RPC 2.0 websocket client. Inspired by net/rpc, clients call methods by their name with arguments and return values marshaled by encoding/json. The client may be used to create convenience calls with types specific to an application.
Receiving notifications is supported but it is up to the caller to unmarshal the JSON-RPC parameters into meaningful data.
This package currently does not implement JSON-RPC 2.0 request batching or keyed request parameters when performing calls.
Index ¶
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 implements JSON-RPC calls and notifications over a websocket.
func Dial ¶
Dial establishes an RPC client connection to the server described by addr. Addr must be the URL of the websocket, e.g., "wss://[::1]:9109/ws".
func (*Client) Call ¶
func (c *Client) Call(ctx context.Context, method string, result interface{}, args ...interface{}) (err error)
Call performs the JSON-RPC described by method with positional parameters passed through args. Result should point to an object to unmarshal the result, or equal nil to discard the result.
type DialFunc ¶
DialFunc dials a network connection. Custom dialers may utilize a proxy or set connection timeouts.
type Error ¶
type Error struct { Code int64 `json:"code"` Message string `json:"message"` Data json.RawMessage `json:"data,omitempty"` }
Error represents a JSON-RPC error object.
type Notification ¶
type Notification struct { Method string Params json.RawMessage }
Notification represents a JSON-RPC notification. Method defines the type of notification and Params describes the arguments (positional or keyed) if any were included in the Request object.
type Notifier ¶
type Notifier chan Notification
Notifier is a channel of received JSON-RPC notifications. Notifications are sent in the order received. Notifier is closed after the client connection is broken. Notifications may be read from the channel after client disconnect if there is enough backpressure.
type Option ¶
type Option func(*options)
Option modifies the behavior of Dial.
func WithBasicAuth ¶
WithBasicAuth enables basic access authentication using the user and password.
func WithNotifier ¶
WithNotifier specifies a channel to read received JSON-RPC notifications. The channel is closed when no futher notifications will be sent. Notifications may be read from the channel after the client has closed.
func WithTLSConfig ¶
WithTLSConfig specifies a TLS config when connecting to a secure websocket (wss) server. If unspecified, the default TLS config will be used.