Documentation ¶
Overview ¶
Package jsonrpc implements a JSONRPC2.0 compliant server as described in https://www.jsonrpc.org/specification
Index ¶
- Constants
- Variables
- type Conn
- type ConnKey
- type Error
- type EventListener
- type HTTP
- type Method
- type NewRequestListener
- type Parameter
- type Request
- type SelectiveListener
- type Server
- func (s *Server) HandleReadWriter(ctx context.Context, rw io.ReadWriter) error
- func (s *Server) HandleReader(ctx context.Context, reader io.Reader) ([]byte, http.Header, error)
- func (s *Server) RegisterMethods(methods ...Method) error
- func (s *Server) WithListener(listener EventListener) *Server
- func (s *Server) WithValidator(validator Validator) *Server
- type Validator
- type Websocket
- type WebsocketConnParams
Constants ¶
const ( InvalidJSON = -32700 // Invalid JSON was received by the server. InvalidRequest = -32600 // The JSON sent is not a valid Request object. MethodNotFound = -32601 // The method does not exist / is not available. InvalidParams = -32602 // Invalid method parameter(s). InternalError = -32603 // Internal JSON-RPC error. )
const MaxRequestBodySize = 10 * utils.Megabyte
Variables ¶
var (
ErrInvalidID = errors.New("id should be a string or an integer")
)
Functions ¶
This section is empty.
Types ¶
type ConnKey ¶ added in v0.7.0
type ConnKey struct{}
ConnKey the key used to retrieve the connection from the context passed to a handler. It is exported to allow transports to set it manually if they decide not to use HandleReadWriter, which sets it automatically. Manually setting the connection can be especially useful when testing handlers.
type Error ¶
type Error struct { Code int `json:"code"` Message string `json:"message"` Data any `json:"data,omitempty"` }
func (*Error) CloneWithData ¶ added in v0.7.4
CloneWithData copies the error and sets the data field on the copy
type EventListener ¶ added in v0.7.0
type HTTP ¶ added in v0.2.1
type HTTP struct {
// contains filtered or unexported fields
}
func (*HTTP) ServeHTTP ¶ added in v0.2.1
func (h *HTTP) ServeHTTP(writer http.ResponseWriter, req *http.Request)
ServeHTTP processes an incoming HTTP request
func (*HTTP) WithListener ¶ added in v0.7.0
func (h *HTTP) WithListener(listener NewRequestListener) *HTTP
WithListener registers a NewRequestListener
type NewRequestListener ¶ added in v0.7.0
type NewRequestListener interface {
OnNewRequest(method string)
}
type SelectiveListener ¶ added in v0.7.0
type SelectiveListener struct { OnNewRequestCb func(method string) OnRequestHandledCb func(method string, took time.Duration) OnRequestFailedCb func(method string, data any) }
func (*SelectiveListener) OnNewRequest ¶ added in v0.7.0
func (l *SelectiveListener) OnNewRequest(method string)
func (*SelectiveListener) OnRequestFailed ¶ added in v0.7.0
func (l *SelectiveListener) OnRequestFailed(method string, data any)
func (*SelectiveListener) OnRequestHandled ¶ added in v0.7.0
func (l *SelectiveListener) OnRequestHandled(method string, took time.Duration)
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func NewServer ¶
func NewServer(poolMaxGoroutines int, log utils.SimpleLogger) *Server
NewServer instantiates a JSONRPC server
func (*Server) HandleReadWriter ¶ added in v0.7.1
HandleReadWriter permits methods to send messages on the connection after the server sends the initial response. rw must permit concurrent writes. A non-nil error indicates the initial response could not be sent, and that no method will be able to write the connection.
func (*Server) HandleReader ¶
HandleReader processes a request to the server It returns the response in a byte array, only returns an error if it can not create the response byte array
func (*Server) RegisterMethods ¶ added in v0.7.0
RegisterMethods verifies and creates an endpoint that the server recognises.
- name is the method name - handler is the function to be called when a request is received for the associated method. It should have (any, *jsonrpc.Error) as its return type - paramNames are the names of parameters in the order that they are expected by the handler
func (*Server) WithListener ¶ added in v0.7.0
func (s *Server) WithListener(listener EventListener) *Server
WithListener registers an EventListener
func (*Server) WithValidator ¶ added in v0.4.0
WithValidator registers a validator to validate handler struct arguments
type Websocket ¶ added in v0.4.0
type Websocket struct {
// contains filtered or unexported fields
}
func NewWebsocket ¶ added in v0.4.0
func NewWebsocket(rpc *Server, log utils.SimpleLogger) *Websocket
func (*Websocket) ServeHTTP ¶ added in v0.6.0
func (ws *Websocket) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP processes an HTTP request and upgrades it to a websocket connection. The connection's entire "lifetime" is spent in this function.
func (*Websocket) WithConnParams ¶ added in v0.4.0
func (ws *Websocket) WithConnParams(p *WebsocketConnParams) *Websocket
WithConnParams sanity checks and applies the provided params.
func (*Websocket) WithListener ¶ added in v0.7.0
func (ws *Websocket) WithListener(listener NewRequestListener) *Websocket
WithListener registers a NewRequestListener
type WebsocketConnParams ¶ added in v0.4.0
type WebsocketConnParams struct { // Maximum message size allowed. ReadLimit int64 // Maximum time to write a message. WriteDuration time.Duration }
func DefaultWebsocketConnParams ¶ added in v0.4.0
func DefaultWebsocketConnParams() *WebsocketConnParams