zoukankan      html  css  js  c++  java
  • hprose rpc使用实例(同时有Java和Delphi客户端的例子)

    php server

    [php] view plain copy
     
    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

    [php] view plain copy
     
    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.   
    7. echo '<br/>';  
    8. // var_dump($test->invoke("hello", $args, 0, HproseResultMode::Raw, 0));  
    9.   
    10. echo '<br/>';  
    11. // var_dump($test->invoke("hello", $args, 0, HproseResultMode::RawWithEndTag, 0));  
    12.   
    13. echo $test->asyncHello("WORLD");  
    14. echo '<br/>';  
    15. $test->asyncHello("WORLD", function ($result)  
    16. {  
    17.     echo "from client result: ";  
    18.     var_dump($result);  
    19. });  
    20.   
    21. echo '<br/>';  
    22. /* 
    23. $test->dnslookup("www.baidu.com", function($result, $args) { 
    24.     echo "result: "; 
    25.     var_dump($result); 
    26.     echo "args: "; 
    27.     var_dump($args); 
    28. }); 
    29. */  
    30. ?>  


    java client

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


    delphi client:

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


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

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

  • 相关阅读:
    The Python Standard Library
    Python 中的round函数
    Python文件类型
    Python中import的用法
    Python Symbols 各种符号
    python 一行写多个语句
    免费SSL证书(https网站)申请,便宜SSL https证书申请
    元宇宙游戏Axie龙头axs分析
    OLE DB provider "SQLNCLI10" for linked server "x.x.x.x" returned message "No transaction is active.".
    The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "xxx.xxx.xxx.xxx" was unable to begin a distributed transaction.
  • 原文地址:https://www.cnblogs.com/findumars/p/5338835.html
Copyright © 2011-2022 走看看