From a7e47b1d0b325c8ddf1c536f15715b94595f7b54 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Thu, 28 May 2020 18:19:04 -0700 Subject: [PATCH] Add option to pass in custom hostname to helloworld example for PHP/Ruby --- examples/php/greeter_client.php | 18 +++++++++++------- examples/php/greeter_proto_gen.sh | 3 +-- examples/ruby/greeter_client.rb | 3 ++- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/examples/php/greeter_client.php b/examples/php/greeter_client.php index e01ff73e3d9..1c7ec2a24fa 100644 --- a/examples/php/greeter_client.php +++ b/examples/php/greeter_client.php @@ -17,24 +17,28 @@ * */ -// php:generate protoc --proto_path=./../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../bins/opt/grpc_php_plugin ./../protos/helloworld.proto +// To generate the necessary proto classes: +// $ protoc --proto_path=../protos --php_out=. --grpc_out=. +// --plugin=protoc-gen-grpc=../../bins/opt/grpc_php_plugin +// ../protos/helloworld.proto require dirname(__FILE__).'/vendor/autoload.php'; -function greet($name) +function greet($hostname, $name) { - $client = new Helloworld\GreeterClient('localhost:50051', [ + $client = new Helloworld\GreeterClient($hostname, [ 'credentials' => Grpc\ChannelCredentials::createInsecure(), ]); $request = new Helloworld\HelloRequest(); $request->setName($name); - list($reply, $status) = $client->SayHello($request)->wait(); + list($response, $status) = $client->SayHello($request)->wait(); if ($status->code !== Grpc\STATUS_OK) { - echo "ERROR: ".$status->code.", ".$status->details."\n"; + echo "ERROR: " . $status->code . ", " . $status->details . PHP_EOL; exit(1); } - echo $reply->getMessage()."\n"; + echo $response->getMessage() . PHP_EOL; } $name = !empty($argv[1]) ? $argv[1] : 'world'; -greet($name); +$hostname = !empty($argv[2]) ? $argv[2] : 'localhost:50051'; +greet($hostname, $name); diff --git a/examples/php/greeter_proto_gen.sh b/examples/php/greeter_proto_gen.sh index a0d7b29c86e..4322bbda2e3 100755 --- a/examples/php/greeter_proto_gen.sh +++ b/examples/php/greeter_proto_gen.sh @@ -13,5 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -protoc --proto_path=./../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../bins/opt/grpc_php_plugin ./../protos/helloworld.proto - +protoc --proto_path=../protos --php_out=. --grpc_out=. --plugin=protoc-gen-grpc=../../bins/opt/grpc_php_plugin ../protos/helloworld.proto diff --git a/examples/ruby/greeter_client.rb b/examples/ruby/greeter_client.rb index 507d254ec9b..56b41e370d6 100755 --- a/examples/ruby/greeter_client.rb +++ b/examples/ruby/greeter_client.rb @@ -26,8 +26,9 @@ require 'grpc' require 'helloworld_services_pb' def main - stub = Helloworld::Greeter::Stub.new('localhost:50051', :this_channel_is_insecure) user = ARGV.size > 0 ? ARGV[0] : 'world' + hostname = ARGV.size > 1 ? ARGV[1] : 'localhost:50051' + stub = Helloworld::Greeter::Stub.new(hostname, :this_channel_is_insecure) message = stub.say_hello(Helloworld::HelloRequest.new(name: user)).message p "Greeting: #{message}" end