-
Notifications
You must be signed in to change notification settings - Fork 75
/
client.go
55 lines (51 loc) · 1.21 KB
/
client.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
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"log"
"net"
"time"
"github.com/lesismal/arpc"
)
func main() {
client, err := arpc.NewClient(func() (net.Conn, error) {
return net.DialTimeout("tcp", "localhost:8888", time.Second*3)
})
if err != nil {
panic(err)
}
defer client.Stop()
req := "hello"
rsp := ""
err = client.Call("/echo/sync", &req, &rsp, time.Second*5)
if err != nil {
log.Fatalf("Call /echo/sync failed: %v", err)
} else {
log.Printf("Call /echo/sync Response: \"%v\"", rsp)
}
err = client.Call("/echo/async", &req, &rsp, time.Second*5)
if err != nil {
log.Fatalf("Call /echo/async failed: %v", err)
} else {
log.Printf("Call /echo/async Response: \"%v\"", rsp)
}
done := make(chan string)
err = client.CallAsync("/echo/async", &req, func(ctx *arpc.Context, er error) {
if er != nil {
log.Fatalf("Call /echo/async failed: %v", err)
}
rsp := ""
er = ctx.Bind(&rsp)
if er != nil {
log.Fatalf("Call /echo/async Bind failed: %v", er)
}
if rsp != req {
log.Fatalf("Call /echo/async failed: %v", er)
}
done <- rsp
}, time.Second*5)
if err != nil {
log.Fatalf("Call /echo/async failed: %v", err)
} else {
rsp := <-done
log.Printf("CallAsync /echo/async Response: \"%v\"", rsp)
}
}