https://github.com/weiwenchen2022/wsrpc
a tiny websocket rpc framework
https://github.com/weiwenchen2022/wsrpc
go library rpc websocket
Last synced: 11 months ago
JSON representation
a tiny websocket rpc framework
- Host: GitHub
- URL: https://github.com/weiwenchen2022/wsrpc
- Owner: weiwenchen2022
- License: bsd-3-clause
- Created: 2023-11-03T08:30:47.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-03T09:07:14.000Z (over 2 years ago)
- Last Synced: 2025-06-23T00:46:21.864Z (about 1 year ago)
- Topics: go, library, rpc, websocket
- Language: Go
- Homepage: https://pkg.go.dev/github.com/weiwenchen2022/wsrpc
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wsrpc
a tiny websocket rpc library.
## Install
```sh
go get github.com/weiwenchen2022/wsrpc
```
## Uasage
### define service
```go
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (*Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (*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
}
```
### start a server
```go
s := wsrpc.NewServer("")
s.Register(new(Arith))
http.ListenAndServe(":3000", s)
```
### start a client
```go
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cl, err := wsrpc.Dial(ctx, "http://localhost:3000","")
if err != nil {
log.Fatal("dialing:", err)
}
defer cl.Close()
// Synchronous call
args := &server.Args{7,8}
var reply int
err = client.Call("Arith.Multiply", args, &reply)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d * %d = %d", args.A, args.B, reply)
// Asynchronous call
quotient := new(Quotient)
divCall := client.Go("Arith.Divide", args, quotient, nil)
<-divCall.Done // will be equal to divCall
// check errors, print, etc.
```
## Reference
GoDoc: [https://pkg.go.dev/github.com/weiwenchen2022/wsrpc](https://pkg.go.dev/github.com/weiwenchen2022/wsrpc)