forked from therecipe/qt
/
client.go
executable file
·61 lines (49 loc) · 1.22 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
56
57
58
59
60
61
//hello.proto and this code is taken from: https://github.com/kenshaw/go-jakarta/tree/master/02-gomobile-and-grpc
package main
import (
"errors"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// A wrapper type to expose via gomobile.
type HelloClient struct {
conn *grpc.ClientConn
client HelloServiceClient
}
// New creates a new HelloClient with the endpoint addr.
func New(addr string) (*HelloClient, error) {
var err error
// create connection
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithTimeout(10*time.Second))
if err != nil {
return nil, err
}
// return our "object"
return &HelloClient{
conn: conn,
client: NewHelloServiceClient(conn),
}, nil
}
// SayHello calls HelloClient.
func (hc *HelloClient) SayHello(s string) (string, error) {
// create request
req := &HelloRequest{Greeting: s}
// some safety checking
if hc.conn == nil || hc.client == nil {
return "", errors.New("unable to SayHello")
}
// call method
res, err := hc.client.SayHello(context.Background(), req)
if err != nil {
return "", err
}
return res.Reply, nil
}
// Shutdown closes connections.
func (hc *HelloClient) Shutdown() error {
if hc.conn != nil {
return hc.conn.Close()
}
return nil
}