Giter VIP home page Giter VIP logo

hprose-swoole's Introduction

Hprose

Hprose for Swoole

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

Introduction

Hprose is a High Performance Remote Object Service Engine.

It is a modern, lightweight, cross-language, cross-platform, object-oriented, high performance, remote dynamic communication middleware. It is not only easy to use, but powerful. You just need a little time to learn, then you can use it to easily construct cross language cross platform distributed application system.

Hprose supports many programming languages, for example:

  • AAuto Quicker
  • ActionScript
  • ASP
  • C++
  • Dart
  • Delphi/Free Pascal
  • dotNET(C#, Visual Basic...)
  • Golang
  • Java
  • JavaScript
  • Node.js
  • Objective-C
  • Perl
  • PHP
  • Python
  • Ruby
  • ...

Through Hprose, You can conveniently and efficiently intercommunicate between those programming languages.

This project is the implementation of Hprose for PHP based on swoole.

More Documents for Hprose 2.0: https://github.com/hprose/hprose-php/wiki

Installation

Download Source Code

Download Link

install by composer

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

Usage

You need to install swoole first. The minimum version of swoole been supported is 1.8.8.

You also need to install hprose-pecl 1.6.5+.

Server

Hprose for PHP is very easy to use.

You can create a standalone hprose http server like this:

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();

The websocket server is also a http server.

Client

Then you can create a hprose client to invoke it like this:

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";
    });

The result of invoking is a promise object, you can also specify the callback function after the arguments, the callback function supports 0 - 3 parameters:

params comments
result The result is the server returned, if no result, its value is null.
arguments It is an array of arguments. if no argument, it is an empty array.
error It is an object of Exception, if no error, its value is null.

hprose-swoole's People

Contributors

andot avatar breath-co2 avatar pgyf avatar yurunsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hprose-swoole's Issues

composer.json是否有误?

"require": {
"php": ">=5.3.0",
"hprose/hprose": ">=2.0.9",
"ext-swoole": ">=1.8.8"
},

应为 "hprose/hprose-php":">=2.0.9"

建议推出协程版

现在用的异步已经被swoole新版本砍掉,作为独立扩展了。使用swoole推荐用协程,是否实现一下协程版本。其实主要是websocket、http版本需要swoole的。socket版本可以使用swoole一键协程化来做支持。

在使用特定设备访问接口的情况下,swoole服务会卡住,不再接收其它接口的访问请求

一句话描述

  • 在使用hprose-swoole 2.0.7搭建服务,用户使用iphone 6s QQ浏览器访问服务发布的接口时,接口会停止服务,其它任何方式的访问接口都无返回值,重启服务后恢复正常。
  • 对于这个问题,我目前的解决办法是使用hprose 1.5.6版本。
  • 希望确认问题后与我联系,这个bug稳定重现。

bug重现环境

bug出现环境:

  • 服务端
    • 使用库:hprose-swoole: 2.0.7/2.0.11
  • 客户端
    • 操作系统: apple 6s
    • 浏览器:qq浏览器 7.0.0.2825
  • 操作
    • 使用上述客户端,通过hprose-html5访问接口,处理该次请求的服务器进程即拒绝包含这次服务的以后所有服务,只有通过重启服务解决。

异常数据头信息

{
    "host":"192.168.2.67:8003",
    "origin":"http://192.168.2.102:8080",
    "accept-encoding":"gzip, deflate",
    "connection":"keep-alive",
    "accept":"*/*",
    "user-agent":"Mozilla/5.0 (iPhone 6sp; CPU iPhone OS 10_1_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 MQQBrowser/7.2 Mobile/14B100 Safari/8536.25 MttCustomUA/2 QBWebViewType/1",
    "content-length":"0",
    "referer":"http://192.168.2.102:8080/hprose-web/index.html?_ijt=g21st5f9d04he2p1dd96tbhgoh",
    "accept-language":"zh-cn"
}

测试

测试目标

  • 使用hprose-swoole库的不同版本
  • 使用hprose库的不同版本
  • 使用不同浏览器
  • 使用不同操作系统
  • 使用不同方式访问: hprosehttp
  • 模拟异常数据头信息进行访问

测试结果

  1. 使用hprose-swoole在2.0.7与2.0.11两个版本中,使用apple 6s qq浏览器通过hprose访问,将出现这个问题。其中2.0.11为最新版本。
  2. 使用hprose版本为1.5.6时,任意浏览器或访问方式均访问正常。1.5.61.*.* 最后一个版本。其中1.*.*自带swoole服务的创建,而到了2.*.*时,不带swoole服务的创建,已经转向了hprose-swoole库中。
  3. 使用arc工具模拟异常包的头信息发送,bug未重现。
  4. 红米note3上的qq浏览器,使用hprose访问接口,bug未出现。
  5. 使用apple 6s qq浏览器通过http访问接口,无bug出现。
  6. 在服务器获取到异常包时,手动unsetcontent-length头信息,无效果。

重现方式

  • 服务端使用下面测试代码中的 hprose-swoole-2.0.11 代码
  • web端使用下面的 web端代码代码
  • 使用 apple 6s qq浏览器访问web端页面,此时bug重现。

结论

  • 这是存在于hprose-swoole库中的一个bug。
  • 目前只发现在使用iphone 6s qq浏览器通过hprose方式访问hprose-swoole的服务时,会出现此bug。
  • 因为发现异常包与正常包之间,差别在于:只存在于异常包的content-length: 0,故临时解决办法,在服务器获取到content-length: 0的包时,不做处理直接return。
  • 长远解决办法
    • 提交bug到hprose-swoole的git issues中,等待bug确认并修复。
    • hprose-swoole库更换为1.5.6版本的hprose

附测试代码

web端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
    <script type="text/javascript" src="hprose/hprose-html5.src.js"></script>
    <script src="//cdn.jsdelivr.net/eruda/1.0.5/eruda.min.js"></script>
    <script>eruda.init();</script>
</head>
<body>
<div id="retData"></div>
<script type="text/javascript">
    var retData = document.getElementById('retData')
    var url = 'http://192.168.2.67:8089'    // 服务地址
    var client = hprose.Client.create(url)
    // 访问服务
    client.invoke('hello', [], function (data) {
        console.info('success', data)
        retData.textContent = data
    },function (error) {
        console.info('error', error)
        retData.textContent = error
    })

</script>
</body>
</html>

hprose-swoole-2.0.11 代码

composer.json

{
    "require": {
        "hprose/hprose-swoole": "2.0.11"
    }
}

SwooleServer.php

<?php
require_once "./vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
    return "Hello $name!";
}
$url = "http://0.0.0.0:8089";
echo "url is $url" . PHP_EOL;
$server = new Server($url);
$server->addFunction('hello');
$server->start();

hprose-swoole-2.0.7 代码

composer.json

{
    "require": {
        "hprose/hprose-swoole": "2.0.7"
    }
}

SwooleServer

<?php
require_once "./vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
    return "Hello $name!";
}
$url = "http://0.0.0.0:8086";
echo "url is $url" . PHP_EOL;
$server = new Server("http://0.0.0.0:8086");
$server->addFunction('hello');
$server->start();

hprose-1.5.6

composer.json

{
  "require": {
    "hprose/hprose": "1.5.6"
  }
}

SwooleServer.json

<?php
require_once "./vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
    return 'Hello ' . $name;
}
$url = "http://0.0.0.0:8087";
echo "url is $url" . PHP_EOL;
$server = new Hprose\Swoole\Server($url);
$server->addFunction('hello');
$server->start();

PHP Notice: Undefined offset: 5 in hprose-swoole/src/Hprose/Swoole/Socket/Service.php on line 150

当worker进程意外挂掉后,之前的连接并未中断,新的worker进程重启后重新接管之前的socket连接,此时连接的上下文信息全部丢失,并且不会再次触发onConnect,只会触发onReceive事件, onReceives数组此时必然为空,导致如下异常:

PHP Notice: Undefined offset: 5 in /home/test/git/server/vendor/hprose/hprose-swoole/src/Hprose/Swoole/Socket/Service.php on line 150
PHP Fatal error: Function name must be a string in /home/test/git/server/vendor/hprose/hprose-swoole/src/Hprose/Swoole/Socket/Service.php on line 151
[2016-12-15 11:26:17 *95097.0] ERROR zm_deactivate_swoole (ERROR 503): Fatal error: Function name must be a string in /home/test/git/server/vendor/hprose/hprose-swoole/src/Hprose/Swoole/Socket/Service.php on line 151.

hprose-ruby客户端data长度超过124,swoole服务端无响应

客户端:使用hprose-ruby,ruby:2.5.0,rails:5.1.5
服务端:使用hprose/swoole/server, php: 7.1.16, laravel: 5.5

实际使用过程中,客户端调用服务时,参数总体长度超过124时服务端疑似卡在

$self->defaultHandle($data, $context)->then(function($data) use ($self, $server, $socket, $id) { $self->socketSend($server, $socket, $data, $id); });

Class 'swoole_http_client' not found i

Class 'swoole_http_client' not found in /Users/chenxi/web/service-backend-project/vendor/hprose/hprose-swoole/src/Hprose/Swoole/Http/Transporter.php:42

image
报如下错误:
image

composer install run demo script got error

composer.json

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

index.php

<?php
require_once 'vendor/autoload.php';

use Hprose\Swoole\Http\Server;

function hello($name)
{
    return "Hello $name!";
}

$server = new Server('http://0.0.0.0:8000');
$server->add('hello');
$server->debug       

php index.php get error as blow:

php index.php 
PHP Warning:  require(/media/sf_php/test/hprose_demo/vendor/composer/../hprose/hprose/src/Hprose.php): failed to open stream: No such file or directory in /media/sf_php/test/hprose_demo/vendor/composer/autoload_real.php on line 66
PHP Fatal error:  require(): Failed opening required '/media/sf_php/test/hprose_demo/vendor/composer/../hprose/hprose/src/Hprose.php' (include_path='.:/usr/share/php') in /media/sf_php/test/hprose_demo/vendor/composer/autoload_real.php on line 66

hprose-swoole does not support swoole 4.4

hprose-swoole does not support swoole 4.4, even async is installed, Undefined property still exists.
PHP Notice: Undefined property: Swoole\Http\Client::$id in /var/www/html/enclog /vendor/hprose/hprose-swoole/src/Hprose/Swoole/Http/Transporter.php on line 142

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.