Skip to content

Commit fc74ed8

Browse files
committed
fmt
1 parent e1e380b commit fc74ed8

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

README.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# go-jsonrpc
1+
go-jsonrpc
2+
==================
23

34
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/filecoin-project/go-jsonrpc)
45
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
@@ -23,42 +24,41 @@ func (h *SimpleServerHandler) AddGet(in int) int {
2324
func main() {
2425
// create a new server instance
2526
rpcServer := jsonrpc.NewServer()
26-
27+
2728
// create a handler instance and register it
2829
serverHandler := &SimpleServerHandler{}
2930
rpcServer.Register("SimpleServerHandler", serverHandler)
30-
31+
3132
// rpcServer is now http.Handler which will serve jsonrpc calls to SimpleServerHandler.AddGet
3233
// a method with a single int param, and an int response. The server supports both http and websockets.
33-
34+
3435
// serve the api
3536
testServ := httptest.NewServer(rpcServer)
3637
defer testServ.Close()
37-
38+
3839
fmt.Println("URL: ", "ws://"+testServ.Listener.Addr().String())
39-
40+
4041
[..do other app stuff / wait..]
4142
}
4243
```
4344

4445
### Client
45-
4646
```go
4747
func start() error {
4848
// Create a struct where each field is an exported function with signatures matching rpc calls
4949
var client struct {
5050
AddGet func(int) int
5151
}
52-
52+
5353
// Make jsonrp populate func fields in the struct with JSONRPC calls
5454
closer, err := jsonrpc.NewClient(context.Background(), rpcURL, "SimpleServerHandler", &client, nil)
5555
if err != nil {
5656
return err
5757
}
5858
defer closer()
59-
59+
6060
...
61-
61+
6262
n := client.AddGet(10)
6363
// if the server is the one from the example above, n = 10
6464

@@ -73,28 +73,28 @@ func start() error {
7373
type _ interface {
7474
// No Params / Return val
7575
Func1()
76-
76+
7777
// With Params
7878
// Note: If param types implement json.[Un]Marshaler, go-jsonrpc will use it
7979
Func2(param1 int, param2 string, param3 struct{A int})
80-
80+
8181
// Returning errors
8282
// * For some connection errors, go-jsonrpc will return jsonrpc.RPCConnectionError{}.
8383
// * RPC-returned errors will be constructed with basic errors.New(__"string message"__)
8484
// * JSON-RPC error codes can be mapped to typed errors with jsonrpc.Errors - https://pkg.go.dev/github.com/filecoin-project/go-jsonrpc#Errors
8585
// * For typed errors to work, server needs to be constructed with the `WithServerErrors`
8686
// option, and the client needs to be constructed with the `WithErrors` option
8787
Func3() error
88-
88+
8989
// Returning a value
9090
// Note: The value must be serializable with encoding/json.
9191
Func4() int
92-
92+
9393
// Returning a value and an error
9494
// Note: if the handler returns an error and a non-zero value, the value will not
9595
// be returned to the client - the client will see a zero value.
9696
Func4() (int, error)
97-
97+
9898
// With context
9999
// * Context isn't passed as JSONRPC param, instead it has a number of different uses
100100
// * When the context is cancelled on the client side, context cancellation should propagate to the server handler
@@ -103,9 +103,9 @@ type _ interface {
103103
// * If the context contains an opencensus trace span, it will be propagated to the server through a
104104
// `"Meta": {"SpanContext": base64.StdEncoding.EncodeToString(propagation.Binary(span.SpanContext()))}` field in
105105
// the jsonrpc request
106-
//
106+
//
107107
Func5(ctx context.Context, param1 string) error
108-
108+
109109
// With non-json-serializable (e.g. interface) params
110110
// * There are client and server options which make it possible to register transformers for types
111111
// to make them json-(de)serializable
@@ -115,7 +115,7 @@ type _ interface {
115115
// which will pass reader data through separate http streams on a different hanhler.
116116
// * Note: a similar mechanism for return value transformation isn't supported yet
117117
Func6(r io.Reader)
118-
118+
119119
// Returning a channel
120120
// * Only supported in websocket mode
121121
// * If no error is returned, the return value will be an int channelId
@@ -132,13 +132,12 @@ type _ interface {
132132
```
133133

134134
### Custom Transport Feature
135-
136135
The go-jsonrpc library supports creating clients with custom transport mechanisms (e.g. use for IPC). This allows for greater flexibility in how requests are sent and received, enabling the use of custom protocols, special handling of requests, or integration with other systems.
137136

138137
#### Example Usage of Custom Transport
139138

140139
Here is an example demonstrating how to create a custom client with a custom transport mechanism:
141-
140+
142141
```go
143142
// Setup server
144143
serverHandler := &SimpleServerHandler{} // some type with methods
@@ -176,7 +175,6 @@ fmt.Printf("Current value: %d\n", client.AddGet(5))
176175
```
177176

178177
### Reverse Calling Feature
179-
180178
The go-jsonrpc library also supports reverse calling, where the server can make calls to the client. This is useful in scenarios where the server needs to notify or request data from the client.
181179

182180
NOTE: Reverse calling only works in websocket mode
@@ -275,10 +273,11 @@ func main() {
275273
}
276274
```
277275

276+
278277
## Contribute
279278

280279
PRs are welcome!
281280

282281
## License
283282

284-
Dual-licensed under [MIT](https://github.com/filecoin-project/go-jsonrpc/blob/master/LICENSE-MIT) + [Apache 2.0](https://github.com/filecoin-project/go-jsonrpc/blob/master/LICENSE-APACHE)
283+
Dual-licensed under [MIT](https://github.com/filecoin-project/go-jsonrpc/blob/master/LICENSE-MIT) + [Apache 2.0](https://github.com/filecoin-project/go-jsonrpc/blob/master/LICENSE-APACHE)

0 commit comments

Comments
 (0)