Documentation ¶
Overview ¶
Package jsonrpc implements a JSONRPC2.0 compliant server as described in https://www.jsonrpc.org/specification
Index ¶
- Constants
- Variables
- func ConnFromContext(ctx context.Context) (io.Writer, bool)
- type ConnKey
- type Error
- type EventListener
- type HTTP
- type Method
- type NewRequestListener
- type Parameter
- type SelectiveListener
- type Server
- func (s *Server) Handle(ctx context.Context, data []byte) ([]byte, error)
- func (s *Server) HandleReader(ctx context.Context, reader io.Reader) ([]byte, 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 * 1024 * 1024 // 10MB
Variables ¶
var (
ErrInvalidID = errors.New("id should be a string or an integer")
)
Functions ¶
Types ¶
type Error ¶
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) Handle ¶
Handle 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) 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