diff options
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 33 |
1 files changed, 20 insertions, 13 deletions
@@ -7,13 +7,15 @@ Go 1.18+ required ## Features: - [x] Batch request and responses -- [ ] WebSockets +- [ ] WebSocket transport -## Usage +## Usage (http transport) -1. Create JSON-RPC 2.0 server: +1. Create JSON-RPC/HTTP server: ```go - s := jsonrpc2.New() + import "github.com/neonxp/jsonrpc2/http" + ... + s := http.New() ``` 2. Write handler: ```go @@ -22,15 +24,19 @@ Go 1.18+ required } ``` Handler must have exact two arguments (context and input of any json serializable type) and exact two return values (output of any json serializable type and error) -3. Wrap handler with `jsonrpc2.Wrap` method and register it in server: +3. Wrap handler with `rpc.Wrap` method and register it in server: ```go - s.Register("multiply", jsonrpc2.Wrap(Multiply)) + s.Register("multiply", rpc.Wrap(Multiply)) ``` 4. Use server as common http handler: ```go http.ListenAndServe(":8000", s) ``` +## Custom transport + +See [http/server.go](/http/server.go) for example of transport implementation. + ## Complete example [Full code](/examples/http) @@ -39,18 +45,19 @@ Go 1.18+ required package main import ( - "context" - "net/http" + "context" + "net/http" - "github.com/neonxp/jsonrpc2" + httpRPC "github.com/neonxp/jsonrpc2/http" + "github.com/neonxp/jsonrpc2/rpc" ) func main() { - s := jsonrpc2.New() - s.Register("multiply", jsonrpc2.Wrap(Multiply)) // Register handlers - s.Register("divide", jsonrpc2.Wrap(Divide)) + s := httpRPC.New() + s.Register("multiply", rpc.Wrap(Multiply)) + s.Register("divide", rpc.Wrap(Divide)) - http.ListenAndServe(":8000", s) + http.ListenAndServe(":8000", s) } func Multiply(ctx context.Context, args *Args) (int, error) { |