hprose-php教程,PHP RPC框架 hprose 上手玩玩

RPC是啥? 远程过程调用, 简单来说就是 另一种api调用的形式, 不过是在tcp层次的。

例子:

php 7.2.9

composer 1.8.5

composer.json

{

"name": "hprose/examples",

"description": "examples of hprose",

"authors": [

{

"name": "frans",

"email": "dafa1680@gmail.com"

}

],

"require": {

"php": ">=7.0",

"hprose/hprose": "^2.0"

}

}

server.php

require './vendor/autoload.php';

function hello($name)

{

return "Hello " . $name . PHP_EOL;

}

use Hprose\Socket\Server;

$server = new Server("tcp://0.0.0.0:1314");

$server->setErrorTypes(E_ALL);

$server->setDebugEnabled();

$server->addFunction('hello');

$server->start();

client.php

require_once "./vendor/autoload.php";

use \Hprose\Future;

use \Hprose\Socket\Client;

$test = new Client("tcp://127.0.0.1:1314");

$test->fullDuplex = true;

Future\co(function () use ($test) {

try {

var_dump((yield $test->hello("yield world1")));

var_dump((yield $test->hello("yield world2")));

var_dump((yield $test->hello("yield world3")));

var_dump((yield $test->hello("yield world4")));

var_dump((yield $test->hello("yield world5")));

var_dump((yield $test->hello("yield world6")));

} catch (\Exception $e) {

echo $e->getMessage() . PHP_EOL;

echo $e->getTraceAsString();

}

});

最后跑起来:

php server.php // 没有输出,会常驻进程, 如果有输出那就是报错啦。

php client.php // 输出:

E:\rpc\hprose-php>php client.php

E:\rpc\hprose-php\client.php:12:

string(20) "Hello yield world1

"

E:\rpc\hprose-php\client.php:13:

string(20) "Hello yield world2

"

E:\rpc\hprose-php\client.php:14:

string(20) "Hello yield world3

"

E:\rpc\hprose-php\client.php:15:

string(20) "Hello yield world4

"

E:\rpc\hprose-php\client.php:16:

string(20) "Hello yield world5

"

E:\rpc\hprose-php\client.php:17:

string(20) "Hello yield world6

"

完毕!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值