[PHP]PHP rpc框架hprose测试
程序员文章站
2022-03-25 16:17:14
建立composer.json 执行 建立server.php 建立client.php 执行 结果 ......
建立composer.json
{ "name": "hprose/examples", "description": "examples of hprose", "authors": [ { "name": "andot", "email": "mabingyao@gmail.com" } ], "require": { "php": ">=5.3.0", "hprose/hprose": "dev-master" } }
执行
composer install
建立server.php
<?php require_once "./vendor/autoload.php"; use hprose\socket\server; function hello($name) { return "hello $name!"; } $server = new server("tcp://0.0.0.0:1314"); $server->seterrortypes(e_all); $server->setdebugenabled(); $server->addfunction('hello'); $server->start();
建立client.php
<?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); } });
执行
php server.php php client.php
结果
string(19) "hello yield world1!" string(19) "hello yield world2!" string(19) "hello yield world3!" string(19) "hello yield world4!" string(19) "hello yield world5!" string(19) "hello yield world6!"