您好,欢迎来到码863代码分享网! 请[登录] / [注册]

hprose与hprose-swoole通信示例

编辑

第一部分hprose-php

Hprose 2.0 for PHP 文档: https://github.com/hprose/hprose-php


php服务端  放入/public 

<?php
//define('ENTRY',microtime(true));
require __DIR__ . '/../vendor/autoload.php';
//require_once '../vendor/hprose/hprose/src/Hprose.php';
use Hprose\Http\Server;

function hello($name,$a,$b) {
	//return microtime(true)-ENTRY;
	return $name.' hello world! '."$a $b";
}

$server = new Server();
$server->addFunction('hello');
$server->start();



php客户端与服务端放不同服务器 thinkphp6  放入/public     远程调用hello 输出   zhang hello world! param1 param2

<?php
require __DIR__ . '/../vendor/autoload.php';
$client = new \Hprose\Http\Client('http://tp609.cc/hprose.php', false);
echo $client->hello('zhang','param1','param2');





html5客户端  与php客户端类似  alert输出zhang hello world! aa bb                   hprose-html5.min.js位于https://gitee.com/andot/hprose-html5/blob/master/dist/hprose-html5.min.js

<html>
<head>
<script type="text/javascript" src="static/hprose-html5.min.js"></script>
</head>
<body>
<script type="text/javascript">
    var client = new hprose.HttpClient("http://tp609.cc/hprose.php", ["hello"]);
    client.hello("zhang",'aa','bb', function(result) {
        alert(result);
    }, function(name, err) {
        alert(err);
    });
</script>
</body>



第二部分hprose-swoole

swoole http服务端,命令运行  php hprose_swoole_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:8081");
$server->setErrorTypes(E_ALL);
$server->setDebugEnabled();
$server->setCrossDomainEnabled();
$server->addFunction('hello');
$server->start();


html5连接测试   hprose-html5.min.js位于https://gitee.com/andot/hprose-html5/blob/master/dist/hprose-html5.min.js

<html>
<head>
<script type="text/javascript" src="static/hprose-html5.min.js"></script>
</head>
<body>
<script type="text/javascript">
    var client = new hprose.HttpClient("http://127.0.0.1:8081", ["hello"]);
    client.hello("zhang", function(result) {
        alert(result);
    }, function(name, err) {
        alert(err);
    });
</script>
</body>

alert回显正确

截图_选择区域_20220131162919.png





swoole websocket服务端,命令运行  php hprose_swoole_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:8082/');
    $server->addFunction('hello');
    $server->start();

html5连接测试,访问后console显示Hello World                            hprose-html5.min.js位于https://gitee.com/andot/hprose-html5/blob/master/dist/hprose-html5.min.js

<html>
<head>
<script type="text/javascript" src="static/hprose-html5.min.js"></script>
</head>
<body>
<script type="text/javascript">
(function() {
    'use strict';
    var client = hprose.Client.create('ws://127.0.0.1:8082', ['hello']);
    client.ready(function(stub) {
        stub.hello.idempotent = true;
        stub.hello('World')
        .then(function(result) {
            console.info(result);
        },function(e) {
            console.error(e);
        });
    },
    function(e) {
        console.error(e);
    });
})();

</script>
</body>


CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网 湘ICP备2023018646号-1
MYSQl共执行 3 个查询,用时 0.0023539066314697 秒,PHP脚本用时 0.004799 秒,占用内存 0.520 MB,Gzip 已启用