-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbi_stream.go
48 lines (41 loc) · 908 Bytes
/
bi_stream.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
package main
import (
"context"
"io"
"log"
"time"
pb "github.com/akhil/grpc-demo/proto"
)
func callSayHelloBidirectionalStream(client pb.GreetServiceClient, names *pb.NamesList) {
log.Printf("Bidirectional Streaming started")
stream, err := client.SayHelloBidirectionalStreaming(context.Background())
if err != nil {
log.Fatalf("Could not send names: %v", err)
}
waitc := make(chan struct{})
go func() {
for {
message, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("Error while streaming %v", err)
}
log.Println(message)
}
close(waitc)
}()
for _, name := range names.Names {
req := &pb.HelloRequest{
Name: name,
}
if err := stream.Send(req); err != nil {
log.Fatalf("Error while sending %v", err)
}
time.Sleep(2 * time.Second)
}
stream.CloseSend()
<-waitc
log.Printf("Bidirectional Streaming finished")
}