greeter_client.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env ruby
  2. # Copyright 2015 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Sample app that connects to a Greeter service.
  16. #
  17. # Usage: $ path/to/greeter_client.rb
  18. this_dir = File.expand_path(File.dirname(__FILE__))
  19. lib_dir = File.join(this_dir, 'lib')
  20. $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
  21. require 'grpc'
  22. require 'helloworld_services_pb'
  23. def main
  24. user = ARGV.size > 0 ? ARGV[0] : 'world'
  25. hostname = ARGV.size > 1 ? ARGV[1] : 'localhost:50051'
  26. stub = Helloworld::Greeter::Stub.new(hostname, :this_channel_is_insecure)
  27. begin
  28. message = stub.say_hello(Helloworld::HelloRequest.new(name: user)).message
  29. p "Greeting: #{message}"
  30. rescue GRPC::BadStatus => e
  31. abort "ERROR: #{e.message}"
  32. end
  33. end
  34. main