微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

gRPC 调用未定义的方法 Helloworld\HelloReply::wait()

如何解决gRPC 调用未定义的方法 Helloworld\HelloReply::wait()

尝试在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 举报,一经查实,本站将立刻删除。