库下载:

Go:https://github.com/hprose/hprose-golang

Delphi:https://github.com/hprose/hprose-delphi


感谢creantan、虫虫 大神指导


// 服务端(因为Delphi 版 hprose不支持TCP,所以这里用http了)

package main

import (
   "github.com/astaxie/beego"
   "fmt"
   "github.com/hprose/hprose-golang/rpc"
)

func hello(name string) string {
   fmt.Println("call1 from client...")
   return "Hello " + name + "!"
}

func add(a, b int) int {
   fmt.Println("call1 add() from client...")
   return a + b
}

type HelloService struct {
   Hello  func(func(string, error), string)
   Add    func(func(int, error), int, int)
}

func (h *HelloService)hello(name string) string  {
   fmt.Println("call hello() from client...")
   return "Hello " + name + "!"
}

func (h *HelloService)add(a, b int) int  {
   fmt.Println("call add() from client...")
   return a + b
}

func main() {
   service := rpc.NewHTTPService()
   helloService := HelloService{}
   service.AddFunction("hello", helloService.hello)
   service.AddFunction("add", helloService.add)

   beego.Handler("/", service)
   beego.Run(":1234")
}


// Delphi客户端

procedure TForm1.btn1Click(Sender: TObject);
var
  client: THproseHttpClient;
begin
  client := THproseHttpClient(VarToObj(THproseHttpClient.New('http://localhost:1234/')));
  try
    ShowMessage(client.Invoke('add', [100, 200]));
    ShowMessage(client.Invoke('hello', ['PiaoYun']));
  finally
    client.Free;
  end;
end;


//  Go客户端

package main

import (
   "fmt"
   "time"

   "github.com/hprose/hprose-golang/rpc"
)

type SubService struct {
   Hello2 func(func(string), string) `name:"hello"`  // 相当于其他语言的函数重载
}

type HelloService struct {
   SubService
   Hello  func(func(string, error), string)
   Add    func(func(int, error), int, int)
}

func main() {
   //client := rpc.NewTCPClient("tcp://localhost:1234/")
   client := rpc.NewHTTPClient("http://localhost:1234/")
   var helloService *HelloService
   client.UseService(&helloService)

   helloService.Hello(func(result string, err error) {
      fmt.Println(result, err)
   }, "PiaoYun")

   helloService.Hello2(func(result string) {
      fmt.Println(result)
   }, "PiaoYun")

   helloService.Add(func(result int, err error) {
      fmt.Printf("add result = %d\n", result)
   }, 10, 20)

   time.Sleep(time.Second * 2)
}


你可能感兴趣的文章

评论区

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。