rpcclient.lpr 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. {
  2. This file is part of the Free Component Library
  3. Demonstrate client-side JSON-RPC functionality using Invoke.
  4. Copyright (c) 2022 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program rpcclient;
  12. {$if not defined(CPU386) and not defined(WIN64)}
  13. {$define useffi}
  14. {$endif}
  15. uses
  16. sysutils, jsonparser, {$ifdef useffi} ffi.manager,{$endif} myapi, fprpcclient;
  17. Procedure DoTestRPC(RPC : TFPRPCClient);
  18. var
  19. client: IMyInterface;
  20. arr: TStringArray;
  21. s: String;
  22. res: Boolean;
  23. begin
  24. // Simple typecast to the needed interface
  25. client := RPC as IMyInterface;
  26. Writeln('===== Testing SayHello');
  27. client.SayHello;
  28. Writeln('===== Testing DoSum');
  29. Writeln(client.DoSum(2, 6));
  30. Writeln('===== Testing Split');
  31. arr := client.Split('Hello FPC World', ' ');
  32. Writeln('Split data:');
  33. for s in arr do
  34. Writeln(#9, s);
  35. Writeln('===== Testing DoVarTest');
  36. s := 'Foobar';
  37. res := client.DoVarTest(s);
  38. Writeln(res, ' ', s);
  39. s := 'Test';
  40. res := client.DoVarTest(s);
  41. Writeln(res, ' ', s);
  42. // Writeln('===== Testing Echo');
  43. // writeln(Client.Echo(['This','is','Sparta']));
  44. end;
  45. Procedure DoTestRPC2(RPC : TFPRPCClient);
  46. var
  47. client: IMyOtherInterface;
  48. begin
  49. // Explicitly create a service by name
  50. Client:=RPC.Specialize CreateService<IMyotherInterface>('Service2');
  51. Writeln('===== Testing SayHello');
  52. Writeln('Sayhello: ',client.SayHello);
  53. Writeln('===== Testing DoEcho');
  54. Writeln('Sayhello: ',client.Echo(['This','is','Sparta']));
  55. end;
  56. var
  57. aRPCClient : TFPRPCClient;
  58. begin
  59. RPCServiceRegistry.Add(TypeInfo(IMyInterface));
  60. RPCServiceRegistry.Add(TypeInfo(IMyOtherInterface),'Service2');
  61. aRPCClient:=TFPRPCClient.Create(Nil);
  62. try
  63. aRPCClient.BaseURL:=ParamStr(1);
  64. if (aRPCClient.BaseURL='') then
  65. aRPCClient.BaseURL:='http://localhost:8080/RPC';
  66. // Typecast
  67. DoTestRPC(aRPCClient);
  68. // Actually create service
  69. DoTestRPC2(aRPCClient);
  70. finally
  71. aRPCClient.Free;
  72. end;
  73. end.