1. Go to this page and download the library: Download wuzhc/swrpc 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/ */
wuzhc / swrpc example snippets
use Swrpc\LogicService;
class SchoolService extends LogicService
{
public function getUserSchool($userID) {
$name = $userID == 123 ? '火星' : '水星';
return $name.'学校';
}
}
namespace SwrpcTests;
use Swrpc\Server;
$basePath = dirname(dirname(__FILE__));
rpc.pid',
];
$server = new Server('School_Module', '127.0.0.1', 9501, 1, $options);
$server->addService(\SwrpcTests\services\SchoolService::class);
$server->start();
namespace SwrpcTests;
use Swrpc\Request;
use Swrpc\LogicService;
use Swrpc\Client;
class UserService extends LogicService
{
public function getUserSchoolName()
{
$userID = 123;
$module = 'School_Module'; //请求目标模块名称,需要和服务端定义的一致
$client = Client::create($module, '127.0.0.1', 9501);
return $client->send(Request::create('\SwrpcTests\services\SchoolService_getUserSchool', [$userID]));
}
}
//调用
echo UserService::factory()->getUserSchoolName();
use Swrpc\Server;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
$logger = new Logger('swrpc');
$logger->pushHandler(new StreamHandler(fopen('xxxx.log','w+'), Logger::DEBUG));
$server = new Server('127.0.0.1', 9501, ['enable_coroutine'=>true]);
$server->addService(UserService::class);
$server->addLogger($logger); //覆盖默认日志处理器
$server->start();
use Swrpc\Server;
$packer = new \Swrpc\Packer\SerializeLengthPacker();
$server = new Server('127.0.0.1', 9501, ['enable_coroutine'=>true]);
$server->addService(UserService::class);
$server->addPacker($packer); //覆盖默认值