TGrpcServer.Unary#
Registra um callback retido para uma chamada unary.
Declaração#
procedure Unary(
const Service: String;
const Method: String;
const Handler: Callback
);O handler deve obedecer à assinatura:
function Handler(const Context: TGrpcCallContext;
const RequestJson: String): String;Exemplo#
uses
Odisseia.Grpc;
var
Server: TGrpcServer;
Client: TGrpcClient;
Response: String;
function Echo(const Context: TGrpcCallContext;
const RequestJson: String): String;
begin
Result := RequestJson
end;
begin
Server := TGrpcServer.Create;
Server.Address('127.0.0.1');
Server.Port(50051);
Server.Descriptor('examples/grpc/echo.pb');
Server.Unary('examples.Echo', 'Call', Echo);
Server.Start;
Client := TGrpcClient.Create.Connect;
Response := Client.Unary(
'examples.Echo', 'Call', '{"value":"ok"}', 1000);
WriteLn(Response);
Client.Close;
Server.Close
end.Resultado esperado: {"value":"ok"}.
O registro exige grpc.server.use. O retorno do handler deve ser JSON válido e limitado. O contexto e o callback pertencem à sessão; Close revoga o callback retido.