Hprose asynchronous client & standalone server based on swoole

Overview

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.
Comments
  • TCP 方式请求,客户端不主动调用close的情况下报warning

    TCP 方式请求,客户端不主动调用close的情况下报warning

    用的是README里面的代码,执行 tcp_cliet.php 报warning:

    Warning: swoole_timer_clear(): timer#11 is not found. in src/Hprose/Swoole/Socket/Transporter.php on line 42

    PHP 版本: 7.2.2 Swoole 版本:2.1.0

    opened by zengbo 3
  • composer install run demo script got error

    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
    
    
    opened by kcloze 2
  • PHP Notice:  Undefined offset: 5 in hprose-swoole/src/Hprose/Swoole/Socket/Service.php on line 150

    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.

    opened by lshsuccess 1
  • composer.json是否有误?

    composer.json是否有误?

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

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

    opened by geekchu 1
  •  Class 'swoole_http_client' not found i

    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

    opened by chenxi2015 9
  • hprose-swoole does not support swoole 4.4

    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

    opened by curtis18 2
  • hprose-ruby客户端data长度超过124,swoole服务端无响应

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

    opened by wqyer 3
  • 在使用特定设备访问接口的情况下,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();
    
    opened by zhan3333 4
Releases(v2.0.11)
Owner
Hprose
Hprose is a High Performance Remote Object Service Engine.
Hprose
Multi-process coroutine edition Swoole spider !! Learn about Swoole's network programming and the use of its related APIs

swoole_spider php bin/spider // Just do it !! Cache use Swoole\Table; use App\Table\Cache; $table = new Table(1<<20); // capacity size $table->column

null 3 Apr 22, 2021
💫 Vega is a CLI mode HTTP web framework written in PHP support Swoole, WorkerMan / Vega 是一个用 PHP 编写的 CLI 模式 HTTP 网络框架,支持 Swoole、WorkerMan

Mix Vega 中文 | English Vega is a CLI mode HTTP web framework written in PHP support Swoole, WorkerMan Vega 是一个用 PHP 编写的 CLI 模式 HTTP 网络框架,支持 Swoole、Work

Mix PHP 46 Apr 28, 2022
swoole,easyswoole,swoole framework

EasySwoole - A High Performance Swoole Framework EasySwoole is a distributed, persistent memory PHP framework based on the Swoole extension. It was cr

null 4.6k Jan 2, 2023
swoole and golang ipc, use goroutine complete swoole coroutine

swoole and golang ipc demo swoole process module exec go excutable file as sider car, use goroutine complete swoole coroutine hub.php <?php require '

null 2 Apr 17, 2022
PHP Kafka client is used in PHP-FPM and Swoole. PHP Kafka client supports 50 APIs, which might be one that supports the most message types ever.

longlang/phpkafka Introduction English | 简体中文 PHP Kafka client is used in PHP-FPM and Swoole. The communication protocol is based on the JSON file in

Swoole Project 235 Dec 31, 2022
This package provides a high performance HTTP server to speed up your Laravel/Lumen application based on Swoole.

This package provides a high performance HTTP server to speed up your Laravel/Lumen application based on Swoole.

Swoole Taiwan 3.9k Jan 8, 2023
Asynchronous server-side framework for network applications implemented in PHP using libevent

phpDaemon https://github.com/kakserpom/phpdaemon Asynchronous framework in PHP. It has a huge number of features. Designed for highload. Each worker i

Vasily Zorin 1.5k Nov 30, 2022
Asynchronous tasks based on ticks.

TickAsync Asynchronous tasks based on ticks. If you like this project gift us a ⭐ . Installation. $ composer require thenlabs/task-loop 2.0.x-dev then

ThenLabs 1 Jan 3, 2022
PHP Coroutine HTTP client - Swoole Humanization Library

PHP Coroutine HTTP client - Swoole Humanization Library

swlib 973 Dec 17, 2022
Simple live support server with PHP Swoole Websocket and Telegram API

Telgraf Simple live support server with PHP Swoole Websocket and Telegram API. Usage Server Setup Clone repository with following command. git clone h

Adem Ali Durmuş 6 Dec 30, 2022
Socks5 proxy server written in Swoole PHP

php-socks This is a Socks5 proxy server implementation built with PHP & Swoole. To start the proxy server, clone this repo, run composer install to in

Nazmul Alam 3 Jan 23, 2022
A sample CakePHP api application using CakeDC/cakephp-api and swoole as server

CakePHP Application Skeleton composer create-project --prefer-dist cakephp/app Added sample data using https://github.com/annexare/Countries Created m

Marcelo Rocha 3 Jul 28, 2022
Hyperf instant messaging program based on swoole framework

Hyperf instant messaging program based on swoole framework

null 20 Aug 12, 2022
Asynchronous & Fault-tolerant PHP Framework for Distributed Applications.

Kraken PHP Framework ~ Release the Kraken! Note: This repository contains the core of the Kraken Framework. If you want to start developing new applic

Kraken 1.1k Dec 27, 2022
An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols. PHP>=5.3.

Workerman What is it Workerman is an asynchronous event-driven PHP framework with high performance to build fast and scalable network applications. Wo

walkor 10.2k Dec 31, 2022
一个极简高性能php框架,支持[swoole | php-fpm ]环境

One - 一个极简高性能php框架,支持[swoole | php-fpm ]环境 快 - 即使在php-fpm下也能1ms内响应请求 简单 - 让你重点关心用one做什么,而不是怎么用one 灵活 - 各个组件松耦合,可以灵活搭配使用,使用方法保持一致 原生sql可以和模型关系with搭配使用,

vic 862 Jan 1, 2023
球球大作战(PHP+Swoole)

球球大作战 在线多人H5游戏(H5客户端, 服务端) W A S D 控制 技术栈: PHP7.4+ Swoole4.6(多进程, WebSocket, 共享内存) SpriteJS v3(2D Canvas渲染) 演示Demo 安装: 环境要求:Linux,PHP7.4+(启用Swoole拓展)

Vacant 15 May 9, 2022
基于 swoole 的多进程队列系统,低延时(最低毫秒级)、低资源占用, 支持一键化协程、超时控制、失败重试。可与 laravel thinkphp 等框架配合使用

multi-process-queue 基于swoole的多进程队列系统,manage进程管理子进程,master进程监听队列分发任务,worker进程执行任务, 多进程、低延时(最低毫秒级)、低资源占用。可与 laravel thinkphp 等框架配合使用 版本要求: php>=7.1 swoo

yuntian 55 Dec 12, 2022
swoole worker

SwooleWorker SwooleWorker is a distributed long connection development framework based on Swoole4. 【Github】 【HomePage】 Manual 【ENGLISH】 【简体中文】 Usage s

null 123 Jan 9, 2023