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 ¶
- type Call
- type Client
- func (c *Client) Call(ctx context.Context, method string, result interface{}, args ...interface{}) (err error)
- func (c *Client) Close() error
- func (c *Client) Done() <-chan struct{}
- func (c *Client) Err() error
- func (c *Client) Go(ctx context.Context, method string, result interface{}, done chan Call, ...) Call
- func (c *Client) String() string
- type DialFunc
- type Error
- type Notifier
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Call ¶ added in v2.2.2
Call represents a JSON-RPC method invocation. Result returns the provided return result and any error occurring during the call.
Result must only be called after the call has completed. Completion is signaled by the call being sent over the channel returned by Done. Implementations are allowed to panic when Result is called before this.
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.
func (*Client) Close ¶
Close sends a websocket close control message and closes the underlying network connection.
func (*Client) Done ¶
func (c *Client) Done() <-chan struct{}
Done returns a channel that is closed after the client's final error is set.
func (*Client) Go ¶ added in v2.2.2
func (c *Client) Go(ctx context.Context, method string, result interface{}, done chan Call, args ...interface{}) Call
Go asynchronously calls 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. The done channel will be written with the returned call after completion or error and must be buffered if non-nil.
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 Notifier ¶
type Notifier interface {
Notify(method string, params json.RawMessage) error
}
Notifier handles JSON-RPC notifications. Method defines the type of notification and params describes the arguments (positional or keyed) if any were included in the Request object.
Notify is never called concurrently and is called with notifications in the order received. Blocking in Notify only blocks other Notify calls and does not prevent the Client from receiving further buffered notifications and processing calls.
If Notify returns an error, the client is closed and no more notifications are processed. If this is the first error observed by the client, it will be returned by Err.
If Notifier implements io.Closer, Close is called following the final notification.
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 Notifier to handle received JSON-RPC notifications. Notifications may continue to be processed after the client has closed. Notifications are dropped by Client if a Notifier is not configured.
func WithPingPeriod ¶ added in v2.1.0
WithPingPeriod specifies a duration between pings sent on a timer. A pong message not received within this period (plus a tolerance) causes connection termination. A period of 0 disables the mechanism.
The default value is one minute.
func WithTLSConfig ¶
WithTLSConfig specifies a TLS config when connecting to a secure websocket (wss) server. If unspecified, the default TLS config will be used.
func WithoutPongDeadline ¶ added in v2.2.0
func WithoutPongDeadline() Option
WithoutPongDeadline disables any default or custom pong deadline. Pings will still be written every ping period unless disabled. This option is reset by later WithPingPeriod options.