Documentation ¶
Overview ¶
Package jsonrpc implements JSON-RPC 2.0 protocol for Neptulon framework.
Example ¶
Example demonstrating the Neptulon JSON-RPC server.
package main const debug = false // Example demonstrating the Neptulon JSON-RPC server. func main() { // ** Output: Server started }
Output:
Index ¶
- type Client
- func (c *Client) Close() error
- func (c *Client) ConnID() string
- func (c *Client) Connect(addr string, debug bool) error
- func (c *Client) HandleNotification(route string, handler func(ctx *NotCtx) error)
- func (c *Client) HandleRequest(route string, handler func(ctx *ReqCtx) error)
- func (c *Client) SendNotification(method string, params interface{}) error
- func (c *Client) SendNotificationArr(method string, params ...interface{}) error
- func (c *Client) SendRequest(method string, params interface{}, resHandler func(ctx *ResCtx) error) (reqID string, err error)
- func (c *Client) SendRequestArr(method string, resHandler func(ctx *ResCtx) error, params ...interface{}) (reqID string, err error)
- func (c *Client) SendResponse(id string, result interface{}, err *ResError) error
- func (c *Client) Session() *cmap.CMap
- func (c *Client) SetDeadline(seconds int)
- func (c *Client) UseTLS(ca, clientCert, clientCertKey []byte)
- type Middleware
- type MiddlewareHandler
- type NotCtx
- type Notification
- type ReqCtx
- type Request
- type ResCtx
- type ResError
- type Response
- type Router
- type Sender
- func (s *Sender) SendNotification(connID string, method string, params interface{}) error
- func (s *Sender) SendNotificationArr(connID string, method string, params ...interface{}) error
- func (s *Sender) SendRequest(connID string, method string, params interface{}, ...) (reqID string, err error)
- func (s *Sender) SendRequestArr(connID string, method string, resHandler func(ctx *ResCtx) error, ...) (reqID string, err error)
- func (s *Sender) SendResponse(connID string, id string, result interface{}, err *ResError) error
- type Server
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { Middleware Conn *neptulon.Conn // contains filtered or unexported fields }
Client is a Neptulon JSON-RPC client.
func NewClient ¶
NewClient creates a new Client object. msgWG = (optional) sets the given *sync.WaitGroup reference to be used for counting active gorotuines that are used for handling incoming/outgoing messages. disconnHandler = (optional) registers a function to handle client disconnection events.
func (*Client) Connect ¶
Connect connectes to the server at given network address and starts receiving messages.
func (*Client) HandleNotification ¶
HandleNotification regiters a handler for incoming notifications.
func (*Client) HandleRequest ¶
HandleRequest regiters a handler for incoming requests.
func (*Client) SendNotification ¶
SendNotification sends a JSON-RPC notification through the client connection with structured params object.
func (*Client) SendNotificationArr ¶
SendNotificationArr sends a JSON-RPC notification message through the client connection with array params.
func (*Client) SendRequest ¶
func (c *Client) SendRequest(method string, params interface{}, resHandler func(ctx *ResCtx) error) (reqID string, err error)
SendRequest sends a JSON-RPC request through the client connection with an auto generated request ID. resHandler is called when a response is returned.
func (*Client) SendRequestArr ¶
func (c *Client) SendRequestArr(method string, resHandler func(ctx *ResCtx) error, params ...interface{}) (reqID string, err error)
SendRequestArr sends a JSON-RPC request through the client connection, with array params and auto generated request ID. resHandler is called when a response is returned.
func (*Client) SendResponse ¶
SendResponse sends a JSON-RPC response through the client connection.
func (*Client) Session ¶
Session is a thread-safe data store for storing arbitrary data for this connection session.
func (*Client) SetDeadline ¶
SetDeadline set the read/write deadlines for the connection, in seconds.
func (*Client) UseTLS ¶
UseTLS enables Transport Layer Security for the connection. ca = Optional CA certificate to be used for verifying the server certificate. Useful for using self-signed server certificates. clientCert, clientCertKey = Optional certificate/privat key pair for TLS client certificate authentication. All certificates/private keys are in PEM encoded X.509 format.
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware is a Neptulon middleware for handling JSON-RPC protocol and relevant JSON-RPC middleware.
func (*Middleware) NotMiddleware ¶
func (mw *Middleware) NotMiddleware(notMiddleware ...func(ctx *NotCtx) error)
NotMiddleware registers middleware to handle notification messages.
func (*Middleware) ReqMiddleware ¶
func (mw *Middleware) ReqMiddleware(reqMiddleware ...func(ctx *ReqCtx) error)
ReqMiddleware registers middleware to handle request messages.
func (*Middleware) ResMiddleware ¶
func (mw *Middleware) ResMiddleware(resMiddleware ...func(ctx *ResCtx) error)
ResMiddleware registers middleware to handle response messages.
type MiddlewareHandler ¶
type MiddlewareHandler interface { ReqMiddleware(reqMiddleware ...func(ctx *ReqCtx) error) NotMiddleware(notMiddleware ...func(ctx *NotCtx) error) ResMiddleware(resMiddleware ...func(ctx *ResCtx) error) }
MiddlewareHandler defines the middleware registrar functions for a middleware stack.
type NotCtx ¶
type NotCtx struct { Client *Client // contains filtered or unexported fields }
NotCtx encapsulates connection and notification objects.
type Notification ¶
type Notification struct { Method string `json:"method"` Params interface{} `json:"params,omitempty"` }
Notification is a JSON-RPC notification object.
type ReqCtx ¶
type ReqCtx struct { Res interface{} // Response to be returned. Err *ResError // Error to be returned. Client *Client // Client connection. // contains filtered or unexported fields }
ReqCtx encapsulates connection, request, and reponse objects.
type Request ¶
type Request struct { ID string `json:"id"` Method string `json:"method"` Params interface{} `json:"params,omitempty"` }
Request is a JSON-RPC request object.
type ResCtx ¶
type ResCtx struct { Client *Client // contains filtered or unexported fields }
ResCtx encapsulates connection and response objects.
type ResError ¶
type ResError struct { Code int `json:"code"` Message string `json:"message"` Data interface{} `json:"data,omitempty"` }
ResError is a JSON-RPC response error object.
type Response ¶
type Response struct { ID string `json:"id"` Result interface{} `json:"result,omitempty"` Error *ResError `json:"error,omitempty"` }
Response is a JSON-RPC response object.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is a JSON-RPC message routing middleware.
func NewRouter ¶
func NewRouter(m MiddlewareHandler) (*Router, error)
NewRouter creates a JSON-RPC router instance and registers it as a Neptulon JSON-RPC middleware.
func (*Router) Notification ¶
Notification adds a new notification route registry.
type Sender ¶
type Sender struct {
// contains filtered or unexported fields
}
Sender is a JSON-RPC middleware for sending requests and handling responses asynchronously.
func NewSender ¶
func NewSender(m *Middleware, send func(connID string, msg []byte) error) Sender
NewSender creates a new Sender middleware.
func (*Sender) SendNotification ¶
SendNotification sends a JSON-RPC notification through the connection denoted by the connection ID with structured params object.
func (*Sender) SendNotificationArr ¶
SendNotificationArr sends a JSON-RPC notification message through the connection denoted by the connection ID with array params.
func (*Sender) SendRequest ¶
func (s *Sender) SendRequest(connID string, method string, params interface{}, resHandler func(ctx *ResCtx) error) (reqID string, err error)
SendRequest sends a JSON-RPC request through the connection denoted by the connection ID with an auto generated request ID. resHandler is called when a response is returned.
func (*Sender) SendRequestArr ¶
func (s *Sender) SendRequestArr(connID string, method string, resHandler func(ctx *ResCtx) error, params ...interface{}) (reqID string, err error)
SendRequestArr sends a JSON-RPC request through the connection denoted by the connection ID, with array params and auto generated request ID. resHandler is called when a response is returned.