main.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Binary client is an example client.
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "log"
  25. "time"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. pb "google.golang.org/grpc/examples/features/proto/echo"
  29. "google.golang.org/grpc/status"
  30. )
  31. var addr = flag.String("addr", "localhost:50051", "the address to connect to")
  32. func sendMessage(stream pb.Echo_BidirectionalStreamingEchoClient, msg string) error {
  33. fmt.Printf("sending message %q\n", msg)
  34. return stream.Send(&pb.EchoRequest{Message: msg})
  35. }
  36. func recvMessage(stream pb.Echo_BidirectionalStreamingEchoClient, wantErrCode codes.Code) {
  37. res, err := stream.Recv()
  38. if status.Code(err) != wantErrCode {
  39. log.Fatalf("stream.Recv() = %v, %v; want _, status.Code(err)=%v", res, err, wantErrCode)
  40. }
  41. if err != nil {
  42. fmt.Printf("stream.Recv() returned expected error %v\n", err)
  43. return
  44. }
  45. fmt.Printf("received message %q\n", res.GetMessage())
  46. }
  47. func main() {
  48. flag.Parse()
  49. // Set up a connection to the server.
  50. conn, err := grpc.Dial(*addr, grpc.WithInsecure())
  51. if err != nil {
  52. log.Fatalf("did not connect: %v", err)
  53. }
  54. defer conn.Close()
  55. c := pb.NewEchoClient(conn)
  56. // Initiate the stream with a context that supports cancellation.
  57. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  58. stream, err := c.BidirectionalStreamingEcho(ctx)
  59. if err != nil {
  60. log.Fatalf("error creating stream: %v", err)
  61. }
  62. // Send some test messages.
  63. if err := sendMessage(stream, "hello"); err != nil {
  64. log.Fatalf("error sending on stream: %v", err)
  65. }
  66. if err := sendMessage(stream, "world"); err != nil {
  67. log.Fatalf("error sending on stream: %v", err)
  68. }
  69. // Ensure the RPC is working.
  70. recvMessage(stream, codes.OK)
  71. recvMessage(stream, codes.OK)
  72. fmt.Println("cancelling context")
  73. cancel()
  74. // This Send may or may not return an error, depending on whether the
  75. // monitored context detects cancellation before the call is made.
  76. sendMessage(stream, "closed")
  77. // This Recv should never succeed.
  78. recvMessage(stream, codes.Canceled)
  79. }