1. Go to this page and download the library: Download hyperf/jet 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/ */
hyperf / jet example snippets
use Hyperf\Jet\DataFormatter\DataFormatter;
use Hyperf\Jet\Packer\JsonEofPacker;
use Hyperf\Jet\PathGenerator\PathGenerator;
use Hyperf\Jet\ProtocolManager;
use Hyperf\Jet\Transporter\StreamSocketTransporter;
ProtocolManager::register($protocol = 'jsonrpc', [
ProtocolManager::TRANSPORTER => new StreamSocketTransporter(),
ProtocolManager::PACKER => new JsonEofPacker(),
ProtocolManager::PATH_GENERATOR => new PathGenerator(),
ProtocolManager::DATA_FORMATTER => new DataFormatter(),
]);
use Hyperf\Jet\DataFormatter\DataFormatter;
use Hyperf\Jet\Packer\JsonEofPacker;
use Hyperf\Jet\PathGenerator\PathGenerator;
use Hyperf\Jet\ProtocolManager;
use Hyperf\Jet\Transporter\ConsulTransporter;
// If you use consul service, you should register ProtocolManager::NODE_SELECTOR.
ProtocolManager::register($protocol = 'consul', [
ProtocolManager::TRANSPORTER => new StreamSocketTransporter(),
ProtocolManager::PACKER => new JsonEofPacker(),
ProtocolManager::PATH_GENERATOR => new PathGenerator(),
ProtocolManager::DATA_FORMATTER => new DataFormatter(),
ProtocolManager::NODE_SELECTOR => new NodeSelector($this->host, $this->port, $config),
]);
use Hyperf\Jet\ServiceManager;
// Bind CalculatorService with jsonrpc protocol, and set the static nodes info.
ServiceManager::register($service = 'CalculatorService', $protocol = 'jsonrpc', [
ServiceManager::NODES => [
[$host = '127.0.0.1', $port = 9503],
],
]);
use Hyperf\Jet\ClientFactory;
$clientFactory = new ClientFactory();
$client = $clientFactory->create($service = 'CalculatorService', $protocol = 'jsonrpc');
// Call the remote method `add` with arguments `1` and `2`.
// The $result is the result of the remote method.
$result = $client->add(1, 2);
use Hyperf\Jet\AbstractClient;
use Hyperf\Jet\Packer\JsonEofPacker;
use Hyperf\Jet\Transporter\StreamSocketTransporter;
use Hyperf\Rpc\Contract\DataFormatterInterface;
use Hyperf\Rpc\Contract\PackerInterface;
use Hyperf\Rpc\Contract\PathGeneratorInterface;
use Hyperf\Rpc\Contract\TransporterInterface;
/**
* @method int add(int $a, int $b);
*/
class CalculatorService extends AbstractClient
{
// Define `CalculatorService` as the default value of $service.
public function __construct(
string $service = 'CalculatorService',
TransporterInterface $transporter = null,
PackerInterface $packer = null,
?DataFormatterInterface $dataFormatter = null,
?PathGeneratorInterface $pathGenerator = null
) {
// Specific the transporter here, you could also retrieve the transporter from ProtocolManager or passing by constructor.
$transporter = new StreamSocketTransporter('127.0.0.1', 9503);
// Specific the packer here, you could also retrieve the packer from ProtocolManager or passing by constructor.
$packer = new JsonEofPacker();
parent::__construct($service, $transporter, $packer, $dataFormatter, $pathGenerator);
}
}
use Hyperf\Jet\AbstractClient;
use Hyperf\Jet\Packer\JsonEofPacker;
use Hyperf\Jet\Transporter\StreamSocketTransporter;
use Hyperf\Rpc\Contract\DataFormatterInterface;
use Hyperf\Rpc\Contract\PackerInterface;
use Hyperf\Rpc\Contract\PathGeneratorInterface;
use Hyperf\Rpc\Contract\TransporterInterface;
use Hyperf\Jet\NodeSelector\NodeSelector;
/**
* @method int add(int $a, int $b);
*/
class CalculatorService extends AbstractClient
{
// Define `CalculatorService` as the default value of $service.
public function __construct(
string $service = 'CalculatorService',
TransporterInterface $transporter = null,
PackerInterface $packer = null,
?DataFormatterInterface $dataFormatter = null,
?PathGeneratorInterface $pathGenerator = null
) {
// Specific the transporter here, you could also retrieve the transporter from ProtocolManager or passing by constructor.
$transporter = new StreamSocketTransporter();
$nodeSelector = new NodeSelector('127.0.0.1', 8500, $config);
[$transporter->host, $transporter->port] = $nodeSelector->selectRandomNode($service, 'jsonrpc');
// Specific the packer here, you could also retrieve the packer from ProtocolManager or passing by constructor.
$packer = new JsonEofPacker();
parent::__construct($service, $transporter, $packer, $dataFormatter, $pathGenerator);
}
}
// Call the remote method `add` with arguments `1` and `2`.
// The $result is the result of the remote method.
$client = new CalculatorService();
$result = $client->add(1, 2);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.