Hprose 是高性能远程对象服务引擎(High Performance Remote Object Service Engine)的缩写。
它是一个先进的轻量级的跨语言跨平台面向对象的高性能远程动态通讯中间件。它不仅简单易用,而且功能强大。你只需要稍许的时间去学习,就能用它轻松构建跨语言跨平台的分布式应用系统了。
Hprose 支持众多编程语言,例如:
- AAuto Quicker
- ActionScript
- ASP
- C++
- Dart
- Delphi/Free Pascal
- dotNET(C#, Visual Basic...)
- Golang
- Java
- JavaScript
- Node.js
- Objective-C
- Perl
- PHP
- Python
- Ruby
- ...
通过 Hprose,你就可以在这些语言之间方便高效的实现互通了。
本项目是基于 swoole 扩展的 Hprose 的 PHP 语言版本实现。
Hprose 2.0 更多文档: https://github.com/hprose/hprose-php/wiki
{
"require": {
"hprose/hprose-swoole": "dev-master"
}
}
你首先需要安装 swoole。swoole 被支持的最低版本为 1.8.8.
Hprose for PHP 使用起来很简单,例如:
http_server.php
<?php
require_once "vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
return 'Hello ' . $name;
}
$server = new Server('http://0.0.0.0:80/');
$server->addFunction('hello');
$server->start();
tcp_server.php
<?php
require_once "vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
return 'Hello ' . $name;
}
$server = new Server('tcp://0.0.0.0:2016');
$server->addFunction('hello');
$server->start();
unix_server.php
<?php
require_once "vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
return 'Hello ' . $name;
}
$server = new Server('unix:/tmp/my.sock');
$server->addFunction('hello');
$server->start();
websocket_server.php
<?php
require_once "vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
return 'Hello ' . $name;
}
$server = new Server('ws://0.0.0.0:8000/');
$server->addFunction('hello');
$server->start();
WebSocket 服务器同时也是 HTTP 服务器,所以既可以用 WebSocket 客户端访问,也可以用 HTTP 客户端访问。
然后你可以创建一个 Hprose 的客户端来调用它了,就像这样:
http_client.php
<?php
require_once "vendor/autoload.php";
use Hprose\Swoole\Client;
$client = new Client('http://127.0.0.1/');
$client->hello('World')->then(function($result) {
echo $result;
}, function($e) {
echo $e;
});
$client->hello('World 0', function() {
echo "ok\r\n";
});
$client->hello('World 1', function($result) {
echo $result . "\r\n";
});
$client->hello('World 2', function($result, $args) {
echo $result . "\r\n";
});
$client->hello('World 3', function($result, $args, $error) {
echo $result . "\r\n";
});
tcp_client.php
<?php
require_once "vendor/autoload.php";
use Hprose\Swoole\Client;
$client = new Client('tcp://127.0.0.1:2016');
$client->hello('World')->then(function($result) {
echo $result;
}, function($e) {
echo $e;
});
$client->hello('World 0', function() {
echo "ok\r\n";
});
$client->hello('World 1', function($result) {
echo $result . "\r\n";
});
$client->hello('World 2', function($result, $args) {
echo $result . "\r\n";
});
$client->hello('World 3', function($result, $args, $error) {
echo $result . "\r\n";
});
直接调用的结果是一个 Promise 对象,也可以在调用时直接指定回调函数,回调函数支持 0 - 3 个参数。它们分别表示:
参数 | 解释 |
---|---|
结果 | 就是服务器端的返回结果,如果没有结果则为 null。 |
调用参数 | 是一个包含了调用参数的数组,如果调用没有参数,则为 0 个元素的数组。 |
错误 | 一个 Exception 对象,如果没有错误则为 null。 |