hprose rpc使用实例

水麒
2023-12-01

php server

<?php
    require_once('src/Hprose.php');
    function hello($name) {
        echo "Hello $name!";
        return "Hello $name!";
    }
    function e() {
        throw new Exception("I am Exception");
    }
    function ee() {
        require("andot");
    }
    function asyncHello($name, $callback) {
        sleep(3);
        $callback("from server,Hello async $name!");
    }
    $server = new HproseHttpServer();
    $server->setErrorTypes(E_ALL);
    $server->setDebugEnabled();
    $server->addFunction('hello');
    $server->addFunctions(array('e', 'ee'));
    $server->addAsyncFunction('asyncHello');
    $server->addFilter(new HproseJSONRPCServiceFilter());
    $server->start();

php client

<?php
require_once ('src/Hprose.php');
$test = new HproseHttpClient("http://localhost/hprose/http_server.php");
echo '<br/>';
// var_dump($test->invoke("hello", $args, 0, HproseResultMode::Serialized, 0));

echo '<br/>';
// var_dump($test->invoke("hello", $args, 0, HproseResultMode::Raw, 0));

echo '<br/>';
// var_dump($test->invoke("hello", $args, 0, HproseResultMode::RawWithEndTag, 0));

echo $test->asyncHello("WORLD");
echo '<br/>';
$test->asyncHello("WORLD", function ($result)
{
    echo "from client result: ";
    var_dump($result);
});

echo '<br/>';
/*
$test->dnslookup("www.baidu.com", function($result, $args) {
    echo "result: ";
    var_dump($result);
    echo "args: ";
    var_dump($args);
});
*/
?>

java client

package hprose.hello.client;

import hprose.client.HproseHttpClient;
import java.io.IOException;

public class HelloClient {
    public static void main(String[] args) throws IOException {
        HproseHttpClient client = new HproseHttpClient();
        client.useService("http://10.0.0.100:9090/hprose_demo/Hello");
        String result = (String) client.invoke("sayHello", new Object[] { "Hprose" });
        System.out.println(result);
        result = (String) client.invoke("sayHello", new Object[] { "中国" });
        System.out.println(result);
        System.out.println(client.invoke("add",new Object[]{115,316}));
    }
}

package hprose.hello.client;

import java.io.IOException;

import hprose.client.HproseHttpClient;

public class TestPHPClient {

	public static void main(String[] args) throws Exception {

		HproseHttpClient client = new HproseHttpClient();
		client.useService("http://10.0.0.105/hprose/http_server.php");
		String result = (String) client.invoke("hello",
				new Object[] { "Hprose来自Java客户端" });
		System.out.println(result);
		
		System.out.println(client.invoke("asyncHello",new Object[] { "Hprose来自Java客户端"}));
	}
}

delphi client:

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, HproseHttpClient, HproseClient;

type
  TForm4 = class(TForm)
    Button1: TButton;
    HproseHttpClient1: THproseHttpClient;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.Button1Click(Sender: TObject);
begin
  HproseHttpClient1.UseService('http://10.0.0.100:9090/hprose_demo/Hello');
  ShowMessage(HproseHttpClient1.Invoke('sayHello', ['World中文']));
  ShowMessage(HproseHttpClient1.Invoke('add', [15,63]));
end;

//https://raw.githubusercontent.com/andot/hprose/master/doc/1.3/docx/pascal.docx
procedure TForm4.Button2Click(Sender: TObject);
begin
    HproseHttpClient1.UseService('http://10.0.0.105/hprose/http_server.php');
  ShowMessage(HproseHttpClient1.Invoke('hello', ['World时间']));
  ShowMessage(HproseHttpClient1.Invoke('asyncHello', ['xxx']));
end;

end.

源码工程: http://pan.baidu.com/s/1o6vF1qE


 类似资料: