/
xclient.go
43 lines (33 loc) · 847 Bytes
/
xclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"context"
"flag"
"fmt"
"log"
example "github.com/bigdot123456/RPCWork"
"github.com/smallnest/rpcx/client"
"github.com/smallnest/rpcx/protocol"
)
var (
addr = flag.String("addr", "0.0.0.0:8972", "server address")
)
func main() {
flag.Parse()
ch := make(chan *protocol.Message)
d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
xclient := client.NewBidirectionalXClient("Arith", client.Failtry, client.RandomSelect, d, client.DefaultOption, ch)
defer xclient.Close()
args := &example.Args{
A: 10,
B: 20,
}
reply := &example.Reply{}
err := xclient.Call(context.Background(), "Mul", args, reply)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
log.Printf("%d * %d = %d", args.A, args.B, reply.C)
for msg := range ch {
fmt.Printf("receive msg from server: %s\n", msg.Payload)
}
}