mardi 22 septembre 2020

Returning new data in a gRPC steam that should stay open forever

I have a gRPC service that will stream data to the connected client until the client terminates the connection.

I've got it working with dummy data and using Task.Delay, but I'm having trouble figuring out how to put real data into the steam.

public override async Task SubscribeToNewData(Empty request, IServerStreamWriter<MyModel> responseStream, ServerCallContext context)
{

    while(true)
    {
        var data = await GetNewData();
        await responseStream.WriteAsync(data);
    }

}

// Dummy implementation
private async Task<MyModel> GetNewData()
{
    await Task.Delay(5000);
    MyModel output = new MyModel();
    return output;
} 

public void SetData(MyModel data)
{
    ??
}

SetData is called from outside the class roughly every 5 seconds. When this happens, I wan't to immediately write the data to the responseStream.

How can I implement SetData and GetNewData to obtain this functionality?

Aucun commentaire:

Enregistrer un commentaire