PHP code example of chj / swoole-rpc

1. Go to this page and download the library: Download chj/swoole-rpc 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/ */

    

chj / swoole-rpc example snippets



/**
* 开启TCP(9501)&HTTP(8090)服务
**/
$server = new chj\Swoole\Coroutine\Server\Server();
//配置
$server->initSetting([
        'http' =>[   
            'host' => '0.0.0.0',
            'port' => 8090,
        ],
        'tcp' =>[ 
            'host' => '0.0.0.0',
            'port' => 9501,
        ],
        'custom' =>[ 
            'tcpPack' => 'length',    // 1.eof,eof拆包 2.length,length拆包
        ],
        // 服务注册
        'serviceRegisterSetting' =>[
            'host' => '127.0.0.1',
            'port' => 6379,
            'serviceName' => 'account-service',
        ],
]);
//运行服务
$server->run();



$client = \chj\Swoole\Coroutine\Client\RpcClient::getInstance(['host'=>'0.0.0.0','port'=>'6667']);

$data = $client->login('chj','login');

print_r($data);


chj\Swoole\Library\Router::group(['namespace' => 'Application\Controller'], function ($route) {
    $route::add('login','Account@login');
    $route::add('login2','Account@login2');
    $route::add('getUserByName', function ($name,$tes) {
        return 'name: ' . $name;
    });
    $route::add('getUserByName2', function ($name) {
        return 'name: ' . $name;
    });
});