hprose/hprose-swoole

基于 swoole 的 Hprose 异步客户端和独立服务器

v2.0.12 2018-12-25 07:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:47:32 UTC


README

Hprose

Hprose for Swoole

Build Status Supported PHP versions: 5.3 .. 7.1 Packagist Packagist Download License

简介

Hprose 是一个高性能远程对象服务引擎。

它是一个现代、轻量级、跨语言、跨平台、面向对象、高性能的远程动态通信中间件。它不仅易于使用,而且功能强大。您只需要一点时间学习,然后就可以用它轻松构建跨语言跨平台的分布式应用系统。

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 for PHP 的实现。

Hprose 2.0 的更多文档:https://github.com/hprose/hprose-php/wiki

安装

下载源代码

下载链接

使用 composer 安装

{
    "require": {
        "hprose/hprose-swoole": "dev-master"
    }
}

用法

您首先需要安装 swoole。支持的 swoole 最小版本为 1.8.8。

您还需要安装 hprose-pecl 1.6.5+。

服务器

Hprose for PHP 非常易于使用。

您可以创建一个独立的 hprose http 服务器,如下所示

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 服务器。

客户端

然后您可以创建一个 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 个参数