jsonrpc

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package jsonrpc implements a JSONRPC2.0 compliant server as described in https://www.jsonrpc.org/specification

Index

Constants

View Source
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.
)
View Source
const MaxRequestBodySize = 10 * 1024 * 1024 // 10MB

Variables

View Source
var ErrInvalidID = errors.New("id should be a string or an integer")

Functions

This section is empty.

Types

type Error

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

func Err added in v0.4.0

func Err(code int, data any) *Error

type HTTP added in v0.2.1

type HTTP struct {
	// contains filtered or unexported fields
}

func NewHTTP added in v0.2.1

func NewHTTP(listener net.Listener, rpc *Server, log utils.SimpleLogger) *HTTP

func (*HTTP) Run added in v0.2.1

func (h *HTTP) Run(ctx context.Context) error

Run starts to listen for HTTP requests

func (*HTTP) ServeHTTP added in v0.2.1

func (h *HTTP) ServeHTTP(writer http.ResponseWriter, req *http.Request)

ServeHTTP processes an incoming HTTP request

type Method

type Method struct {
	Name    string
	Params  []Parameter
	Handler any
}

type Parameter

type Parameter struct {
	Name     string
	Optional bool
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(log utils.SimpleLogger) *Server

NewServer instantiates a JSONRPC server

func (*Server) Handle

func (s *Server) Handle(data []byte) ([]byte, error)

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

func (s *Server) HandleReader(reader io.Reader) ([]byte, error)

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) RegisterMethod

func (s *Server) RegisterMethod(method Method) error

RegisterMethod 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) WithValidator added in v0.4.0

func (s *Server) WithValidator(validator Validator) *Server

WithValidator registers a validator to validate handler struct arguments

type Validator added in v0.4.0

type Validator interface {
	Struct(any) error
}

type Websocket added in v0.4.0

type Websocket struct {
	// contains filtered or unexported fields
}

func NewWebsocket added in v0.4.0

func NewWebsocket(listener net.Listener, rpc *Server, log utils.SimpleLogger) *Websocket

func (*Websocket) Handler added in v0.4.0

func (ws *Websocket) Handler(ctx context.Context) http.Handler

Handler processes an HTTP request and upgrades it to a websocket connection. The connection's entire "lifetime" is spent in this function.

func (*Websocket) Run added in v0.4.0

func (ws *Websocket) Run(ctx context.Context) error

func (*Websocket) WithConnParams added in v0.4.0

func (ws *Websocket) WithConnParams(p *WebsocketConnParams) *Websocket

WithConnParams sanity checks and applies the provided params.

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
	// Maximum time to read a message.
	ReadDuration time.Duration
}

func DefaultWebsocketConnParams added in v0.4.0

func DefaultWebsocketConnParams() *WebsocketConnParams

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL