Last active
February 22, 2022 07:32
-
-
Save pramonow/fb95cc7fbba02b1dc4aa10c602ebdc11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"io" | |
"log" | |
pb "github.com/pramonow/go-grpc-server-streaming-example/src/proto" | |
"google.golang.org/grpc" | |
) | |
func main() { | |
// dial server | |
conn, err := grpc.Dial(":50005", grpc.WithInsecure()) | |
if err != nil { | |
log.Fatalf("can not connect with server %v", err) | |
} | |
// create stream | |
client := pb.NewStreamServiceClient(conn) | |
in := &pb.Request{Id: 1} | |
stream, err := client.FetchResponse(context.Background(), in) | |
if err != nil { | |
log.Fatalf("open stream error %v", err) | |
} | |
done := make(chan bool) | |
go func() { | |
for { | |
resp, err := stream.Recv() | |
if err == io.EOF { | |
done <- true //means stream is finished | |
return | |
} | |
if err != nil { | |
log.Fatalf("cannot receive %v", err) | |
} | |
log.Printf("Resp received: %s", resp.Result) | |
} | |
}() | |
<-done //we will wait until all response is received | |
log.Printf("finished") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment