-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
158 lines (124 loc) · 2.59 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"grpc-stream-example/pb"
"log"
"net"
"strings"
"sync"
"time"
)
var client pb.GreeterClient
func main() {
conn, err := grpc.Dial("localhost:8000", grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()
client = pb.NewGreeterClient(conn)
srv := grpc.NewServer()
s := new(ClientServer)
pb.RegisterClientServer(srv, s)
reflection.Register(srv)
l, err := net.Listen("tcp", ":8001")
if err != nil {
log.Fatalf("could not listen to %s: %v", "8001", err)
}
fmt.Println("server is running on port 8001")
err = srv.Serve(l)
if err != nil {
log.Fatalf("could not serve: %v", err)
}
}
type ClientServer struct {
pb.UnimplementedClientServer
}
func (s *ClientServer) BidirectionalStream(ctx context.Context, r *pb.Request) (*pb.Response, error) {
out, err := client.BidirectionalStream(context.Background())
if err != nil {
return nil, err
}
ticker := time.NewTicker(1000 * time.Millisecond)
defer ticker.Stop()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for c := 0; c < 10; {
select {
case <-ticker.C:
r.Name = fmt.Sprintf("%s %d", r.Name, c)
if c == 5 {
r.Action = "BREAK"
}
err = out.Send(r)
if err != nil {
fmt.Println("error send:", err)
return
}
c++
}
}
}()
go func() {
defer wg.Done()
for {
resp, err := out.Recv()
if err != nil {
fmt.Println("error recv:", err)
return
}
fmt.Println("got msg BidirectionalStream:", resp.Message)
if resp.Message == "BREAK" {
return
}
}
}()
wg.Wait()
return &pb.Response{
Message: "done",
}, nil
}
func (s *ClientServer) ServerStream(ctx context.Context, r *pb.Request) (*pb.Response, error) {
out, err := client.ServerStream(ctx, r)
if err != nil {
return nil, err
}
for {
resp, err := out.Recv()
if err != nil {
fmt.Println("error recv:", err)
return nil, err
}
fmt.Println("got msg ServerStream:", resp.Message)
if strings.Contains(resp.Message, "Bye") {
return resp, nil
}
}
}
func (s *ClientServer) ClientStream(ctx context.Context, r *pb.Request) (*pb.Response, error) {
out, err := client.ClientStream(ctx)
if err != nil {
return nil, err
}
ticker := time.NewTicker(1000 * time.Millisecond)
defer ticker.Stop()
for c := 0; c < 10; {
select {
case <-ticker.C:
r.Name = fmt.Sprintf("%s %d", r.Name, c)
if c == 5 {
r.Action = "BREAK"
}
err = out.Send(r)
if err != nil {
fmt.Println("error send:", err)
}
c++
}
}
return out.CloseAndRecv()
}