You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

61 lines
1.7 KiB

  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/encoding/gzip" // Install the gzip compressor
  28. pb "google.golang.org/grpc/examples/features/proto/echo"
  29. )
  30. var addr = flag.String("addr", "localhost:50051", "the address to connect to")
  31. func main() {
  32. flag.Parse()
  33. // Set up a connection to the server.
  34. conn, err := grpc.Dial(*addr, grpc.WithInsecure())
  35. if err != nil {
  36. log.Fatalf("did not connect: %v", err)
  37. }
  38. defer conn.Close()
  39. c := pb.NewEchoClient(conn)
  40. // Send the RPC compressed. If all RPCs on a client should be sent this
  41. // way, use the DialOption:
  42. // grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name))
  43. const msg = "compress"
  44. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  45. defer cancel()
  46. res, err := c.UnaryEcho(ctx, &pb.EchoRequest{Message: msg}, grpc.UseCompressor(gzip.Name))
  47. fmt.Printf("UnaryEcho call returned %q, %v\n", res.GetMessage(), err)
  48. if err != nil || res.GetMessage() != msg {
  49. log.Fatalf("Message=%q, err=%v; want Message=%q, err=<nil>", res.GetMessage(), err, msg)
  50. }
  51. }