-
-
Save harche/f82e76602d3f67531bd4753043356bf9 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