Skip to main content

Initialize bidirectional stream

Initializes a new bidirectional stream

Function InitializeBidirectionalStream(Val Connection, Val Service, Val Method, Val Timeout = 10000) Export

ParameterCLI optionTypeRequiredDescription
Connection-ArbitraryConnection object
Service-StringService name
Method-StringMethod name
Timeout-NumberTimeout (in ms)

Returns: Map Of KeyAndValue - Processing result

caution

NOCLI: this method is not available in CLI version


1C:Enterprise/OneScript code example
    Address = "https://grpcb.in:9001";

Proto1 = "https://hut.openintegrations.dev/test_data/grpcbin_with_import.proto"; // String, path to file or URL
Proto2 = "https://hut.openintegrations.dev/test_data/mt.proto"; // String, path to file or URL

Scheme = New Map;
Scheme.Insert("main.proto" , Proto1); // Primary
Scheme.Insert("my_types.proto", Proto2); // For import in primary

Parameters = OPI_GRPC.GetConnectionParameters(Address, Scheme);
Tls = OPI_GRPC.GetTlsSettings(True);

Service = "grpcbin.GRPCBin";
Method = "DummyBidirectionalStreamStream";

Connection = OPI_GRPC.CreateConnection(Parameters, Tls);

If Not OPI_GRPC.IsConnector(Connection) Then
Raise Connection["error"];
EndIf;

StingsArray = New Array;
StingsArray.Add("one");
StingsArray.Add("two");
StingsArray.Add("three");

Data = New Map;
Data.Insert("f_string" , "Test message");
Data.Insert("f_bool" , True);
Data.Insert("f_strings", StingsArray);
Data.Insert("f_sub" , New Structure("f_string", "Nested value"));

Result = OPI_GRPC.InitializeBidirectionalStream(Connection, Service, Method); // <---

If Not Result["result"] Then
Raise Result["error"];
Else
StreamID = Result["streamId"];
EndIf;

ResultArray = New Array;
Counter = 0;

While Counter < 10 Do

Data.Insert("f_int32", Counter + 1);

CurrentSend = OPI_GRPC.SendMessage(Connection, StreamID, Data);

If Not CurrentSend["result"] Then

Error = CurrentSend["error"];

If Error = "Timeout" Then
Continue;
Else
Raise StrTemplate("Send error: %1 (processed messages %2)", Error, Counter);
EndIf;

EndIf;

CurrentReceive = OPI_GRPC.GetMessage(Connection, StreamID);

If Not CurrentReceive["result"] Then
Raise StrTemplate("Receive error: %1 (processed messages %2)", Error, Counter);
EndIf;

ResultArray.Add(CurrentReceive["message"]);
Counter = Counter + 1;

EndDo;

OPI_GRPC.CloseStream(Connection, StreamID);
Result
{
"result": true,
"streamId": "c0c82359-dc1a-444c-91c9-ef1bf0b003ea"
}