hprose rpc使用实例(同时有Java和Delphi客户端的例子)

时间:2023-03-09 09:16:48
hprose rpc使用实例(同时有Java和Delphi客户端的例子)

php server

  1. <?php
  2. require_once('src/Hprose.php');
  3. function hello($name) {
  4. echo "Hello $name!";
  5. return "Hello $name!";
  6. }
  7. function e() {
  8. throw new Exception("I am Exception");
  9. }
  10. function ee() {
  11. require("andot");
  12. }
  13. function asyncHello($name, $callback) {
  14. sleep(3);
  15. $callback("from server,Hello async $name!");
  16. }
  17. $server = new HproseHttpServer();
  18. $server->setErrorTypes(E_ALL);
  19. $server->setDebugEnabled();
  20. $server->addFunction('hello');
  21. $server->addFunctions(array('e', 'ee'));
  22. $server->addAsyncFunction('asyncHello');
  23. $server->addFilter(new HproseJSONRPCServiceFilter());
  24. $server->start();

php client

  1. <?php
  2. require_once ('src/Hprose.php');
  3. $test = new HproseHttpClient("http://localhost/hprose/http_server.php");
  4. echo '<br/>';
  5. // var_dump($test->invoke("hello", $args, 0, HproseResultMode::Serialized, 0));
  6. echo '<br/>';
  7. // var_dump($test->invoke("hello", $args, 0, HproseResultMode::Raw, 0));
  8. echo '<br/>';
  9. // var_dump($test->invoke("hello", $args, 0, HproseResultMode::RawWithEndTag, 0));
  10. echo $test->asyncHello("WORLD");
  11. echo '<br/>';
  12. $test->asyncHello("WORLD", function ($result)
  13. {
  14. echo "from client result: ";
  15. var_dump($result);
  16. });
  17. echo '<br/>';
  18. /*
  19. $test->dnslookup("www.baidu.com", function($result, $args) {
  20. echo "result: ";
  21. var_dump($result);
  22. echo "args: ";
  23. var_dump($args);
  24. });
  25. */
  26. ?>

java client

  1. package hprose.hello.client;
  2. import hprose.client.HproseHttpClient;
  3. import java.io.IOException;
  4. public class HelloClient {
  5. public static void main(String[] args) throws IOException {
  6. HproseHttpClient client = new HproseHttpClient();
  7. client.useService("http://10.0.0.100:9090/hprose_demo/Hello");
  8. String result = (String) client.invoke("sayHello", new Object[] { "Hprose" });
  9. System.out.println(result);
  10. result = (String) client.invoke("sayHello", new Object[] { "中国" });
  11. System.out.println(result);
  12. System.out.println(client.invoke("add",new Object[]{115,316}));
  13. }
  14. }
  1. package hprose.hello.client;
  2. import java.io.IOException;
  3. import hprose.client.HproseHttpClient;
  4. public class TestPHPClient {
  5. public static void main(String[] args) throws Exception {
  6. HproseHttpClient client = new HproseHttpClient();
  7. client.useService("http://10.0.0.105/hprose/http_server.php");
  8. String result = (String) client.invoke("hello",
  9. new Object[] { "Hprose来自Java客户端" });
  10. System.out.println(result);
  11. System.out.println(client.invoke("asyncHello",new Object[] { "Hprose来自Java客户端"}));
  12. }
  13. }

delphi client:

  1. unit Unit4;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, HproseHttpClient, HproseClient;
  6. type
  7. TForm4 = class(TForm)
  8. Button1: TButton;
  9. HproseHttpClient1: THproseHttpClient;
  10. Button2: TButton;
  11. procedure Button1Click(Sender: TObject);
  12. procedure Button2Click(Sender: TObject);
  13. private
  14. { Private declarations }
  15. public
  16. { Public declarations }
  17. end;
  18. var
  19. Form4: TForm4;
  20. implementation
  21. {$R *.dfm}
  22. procedure TForm4.Button1Click(Sender: TObject);
  23. begin
  24. HproseHttpClient1.UseService('http://10.0.0.100:9090/hprose_demo/Hello');
  25. ShowMessage(HproseHttpClient1.Invoke('sayHello', ['World中文']));
  26. ShowMessage(HproseHttpClient1.Invoke('add', [15,63]));
  27. end;
  28. //https://raw.githubusercontent.com/andot/hprose/master/doc/1.3/docx/pascal.docx
  29. procedure TForm4.Button2Click(Sender: TObject);
  30. begin
  31. HproseHttpClient1.UseService('http://10.0.0.105/hprose/http_server.php');
  32. ShowMessage(HproseHttpClient1.Invoke('hello', ['World时间']));
  33. ShowMessage(HproseHttpClient1.Invoke('asyncHello', ['xxx']));
  34. end;
  35. end.

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

http://blog.****.net/earbao/article/details/46501671