<?php function __autoload($name) { $file = str_replace('\\', DIRECTORY_SEPARATOR, $name) . '.php'; include __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $file; } use ZeroRPC\Client as ZeroRPC; $test = new ZeroRPC('tcp://127.0.0.1:18181'); // Example 1 // This will sleep for ~6 seconds $start = microtime(true); print $test->sleep(2) . PHP_EOL; print $test->sleep(3) . PHP_EOL; print $test->sleep(1) . PHP_EOL; print 'Example 1: Slept for a total of ' . (microtime(true) - $start) . ' ms' . PHP_EOL; // Example 2 // The calls being made in parralel, this will sleep for ~3 seconds $start = microtime(true); $test->invoke('sleep', array(2), function ($event) { print $event->args[0] . PHP_EOL; }); $test->invoke('sleep', array(3), function ($event) { print $event->args[0] . PHP_EOL; }); $test->invoke('sleep', array(1), function ($event) { print $event->args[0] . PHP_EOL; }); $test->dispatch(); print 'Example 2: Slept for a total of ' . (microtime(true) - $start) . ' ms' . PHP_EOL;
<?php require 'vendor/autoload.php'; use ZeroRPC\Hook\ConfigMiddleware; use ZeroRPC\Context; use ZeroRPC\Client; /** * This client provided a powful configuration to get zerorpc client. * * Configure use `ZERORPC_` as prefix, that means, if define `ZERORPC_TIME` as key, * then you can get the client by `Client("time")`, it's NOT case sensitive. * * And also support multi-version, for each version of the service, you can either put single * endpoint or an array of endpoints. */ $middleware = new ConfigMiddleware(array('ZERORPC_TIME' => array('1.0' => 'tcp://127.0.0.1:1234', '2.0' => array('tcp://127.0.0.1:2345', 'tcp://127.0.0.1:1234'), 'access_key' => 'testing_client_key', 'default' => '1.0'))); $context = new Context(); $context->registerHook('resolve_endpoint', $middleware->resolveEndpoint()); $context->registerHook('before_send_request', $middleware->beforeSendRequest()); $client = new Client("time", '1.0', $context); print $client->time() . PHP_EOL; $anotherClient = new Client("time", '2.0', $context); print $anotherClient->time() . PHP_EOL;
<?php require 'vendor/autoload.php'; use ZeroRPC\Client; use ZeroRPC\Channel; $client = new Client("tcp://127.0.0.1:1234"); try { $client->should_response_error(); } catch (ZeroRPC\RemoteException $e) { print $e; } try { $client->sleep(3); } catch (ZeroRPC\TimeoutException $e) { print $e; } try { $client->async("sleep", array(1), $null_); $client->async("sleep", array(2), $null_); Channel::dispatch(); } catch (ZeroRPC\TimeoutException $e) { print $e; }
<?php require 'vendor/autoload.php'; use ZeroRPC\Client; use ZeroRPC\Channel; $clientA = new Client("tcp://127.0.0.1:1234"); $clientA->setTimeout(3500); $clientB = new Client("tcp://127.0.0.1:2345"); $clientB->setTimeout(3500); // normal example $time = $clientA->strftime("%Y/%m/%d %H:%M:%S"); $clientA->async("strftime", array("%Y/%m/%d %H:%M:%S"), $async_time); Channel::dispatch(); assert($time == $async_time); print "Time is {$async_time}!" . PHP_EOL; // sync example print "Example 1: start sync call:" . PHP_EOL; $start = microtime(true); $clientA->sleep(3); $clientB->sleep(2); print 'cost ' . (microtime(true) - $start) . ' s' . PHP_EOL; // async example print "Example 2: start async call:" . PHP_EOL; $start = microtime(true); $clientA->async("sleep", array(3), $sleep1); $clientB->async("sleep", array(2), $sleep2); Channel::dispatch(3500); print 'cost ' . (microtime(true) - $start) . ' s' . PHP_EOL;