1. Go to this page and download the library: Download andphp/jsonrpc library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
/**
*
* @RpcService(name="CalculatorService", protocol="jsonrpc-bl", server="jsonrpc-http")
*/
class CalculatorService implements CalculatorServiceInterface
{
// 实现一个加法方法,这里简单的认为参数都是 int 类型
public function add(int $a, int $b): int
{
// 这里是服务方法的具体实现
return $a + $b;
}
}
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Listener;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;
use Hyperf\JsonRpc\PathGenerator;
use Hyperf\Rpc\ProtocolManager;
/**
* @Listener
*/
class RpcProtocolListener implements ListenerInterface
{
/**
* @var ProtocolManager
*/
private $protocolManager;
public function __construct(ProtocolManager $protocolManager)
{
$this->protocolManager = $protocolManager;
}
public function listen(): array
{
return [
BootApplication::class
];
}
/**
* All official rpc protocols should register in here,
* and the others non-official protocols should register in their own component via listener.
*/
public function process(object $event)
{
$this->protocolManager->register('jsonrpc-bl', [
'packer' => \Bl\GoJsonEofPacker::class,
'transporter' => \Bl\GoJsonRpcTransporter::class,
'path-generator' => PathGenerator::class,
'data-formatter' => \Bl\GoDataFormatter::class,
]);
}
}
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
return [
\App\Listener\RpcProtocolListener::class
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.