问题描述
尝试在PHP中学习grpc。以https://grpc.io/docs/languages/php/quickstart/#download-the-example为例。
当我尝试启动时,我发现了致命错误。调用未定义的方法 Helloworld\HelloReply::wait()。 HelloReply 继承了\Google\Protobuf\Internal\Message。 Message 类中没有等待方法。证明https://github.com/protocolbuffers/protobuf/blob/master/php/src/Google/Protobuf/Internal/Message.php。我可能发现了 PHP 的错误实现。
GreeterClient.PHP
class GreeterClient implements GreeterInterface
{
public function sayHello(HelloRequest $request) : HelloReply{
$reply = new HelloReply();
$reply->setMessage('Test');
return $reply;
}
}
GreeterInterface.PHP
interface GreeterInterface
{
/**
* Sends a greeting
*
* Method <code>sayHello</code>
*
* @param \Helloworld\HelloRequest $request
* @return \Helloworld\HelloReply
*/
public function sayHello(\Helloworld\HelloRequest $request);
}
greeter_client.PHP
function greet($hostname,$name)
{
$client = new Helloworld\GreeterClient($hostname,[
'credentials' => Grpc\ChannelCredentials::createInsecure(),]);
$request = new Helloworld\HelloRequest();
$request->setName($name);
list($response,$status) = $client->SayHello($request)->wait();
if ($status->code !== Grpc\STATUS_OK && !true) {
echo "ERROR: " . $status->code . "," . $status->details . PHP_EOL;
exit(1);
}
echo $response->getMessage() . PHP_EOL;
}
$name = !empty($argv[1]) ? $argv[1] : 'world';
$hostname = !empty($argv[2]) ? $argv[2] : 'localhost:50051';
greet($hostname,$name);
HelloReply.PHP
<?PHP
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: helloworld.proto
namespace Helloworld;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;
/**
* The response message containing the greetings
*
* Generated from protobuf message <code>helloworld.HelloReply</code>
*/
class HelloReply extends \Google\Protobuf\Internal\Message
{
/**
* Generated from protobuf field <code>string message = 1;</code>
*/
protected $message = '';
/**
* Constructor.
*
* @param array $data {
* Optional. Data for populating the Message object.
*
* @type string $message
* }
*/
public function __construct($data = NULL) {
\GPBMetadata\Helloworld::initOnce();
parent::__construct($data);
}
/**
* Generated from protobuf field <code>string message = 1;</code>
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* Generated from protobuf field <code>string message = 1;</code>
* @param string $var
* @return $this
*/
public function setMessage($var)
{
GPBUtil::checkString($var,True);
$this->message = $var;
return $this;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)