Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/makasim/jsonrpc
TCP based JSON RPC 1.0 Client\Server
https://github.com/makasim/jsonrpc
client golang jsonrpc server
Last synced: 11 days ago
JSON representation
TCP based JSON RPC 1.0 Client\Server
- Host: GitHub
- URL: https://github.com/makasim/jsonrpc
- Owner: makasim
- License: mit
- Created: 2022-06-10T10:05:26.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-15T20:20:49.000Z (over 2 years ago)
- Last Synced: 2024-10-18T00:40:07.917Z (30 days ago)
- Topics: client, golang, jsonrpc, server
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TCP based JSON RPC 1.0 Client\Server
The package combines TCP connections management from [valyala/fasthttp](https://github.com/valyala/fasthttp) and
Go's standard [jsonrpc codec](https://pkg.go.dev/net/rpc/jsonrpc).Here is a simple example. A server wishes to export an object of type Arith:
```go
package serverimport "errors"
type Args struct {
A, B int
}type Quotient struct {
Quo, Rem int
}type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
```The server calls:
```go
package mainimport (
"context"
"log"
"net"
"github.com/makasim/jsonrpc""server"
)func main() {
s := &jsonrpc.Server{
Receivers: []interface{}{
&server.Arith{},
},
}
if err := s.ListenAndServe(":1234"); err != nil {
log.Fatalf("serve: %s", err)
}
}```
At this point, clients can see a service "Arith" with methods "Arith.Multiply" and "Arith.Divide".
To invoke one, a client does a remote call:Client:
```go
package mainimport (
"context"
"log"
"github.com/makasim/jsonrpc"
"server"
)func main() {
ctx := context.Background()
args := &server.Args{7,8}
var reply int
c := &jsonrpc.Client{}
err := c.Call(ctx, "127.0.0.1:9999", "FooService.Func", args, &reply)
if err != nil {
log.Fatal("call error:", err)
}
log.Println(reply)
}
```