/// <summary> /// Subscribe to orders async /// </summary> /// <param name="action"></param> /// <returns>Subscription</returns> public async Task <CallResult <UpdateSubscription> > SubscribeOrdersAsync(Action <HitBTCOrder> action) { var request = new SubscribeOrdersRequest(NextId()); var internalHandler = new Action <HitBTCSocketSubscriptionResponse <JToken> >(response => { var error = response.Error; var method = response.Method; JToken data = response.Data; switch (method) { case "activeOrders": var orders = data.ToObject <IEnumerable <HitBTCOrder> >(); foreach (var order in orders) { action(order); } break; case "report": var report = data.ToObject <HitBTCReport>(); action(report); break; } }); return(await Subscribe <HitBTCSocketSubscriptionResponse <JToken> >(request, null, true, internalHandler).ConfigureAwait(false)); }
public override async Task SubscribeOrders(SubscribeOrdersRequest request, IServerStreamWriter <CoffeeOrder> responseStream, ServerCallContext context) { this._logger.LogInformation($"Subscribing to orders..."); _subscribers.Add(responseStream); // send the current state first foreach (var item in _orders) { await responseStream.WriteAsync(item.Value); } context.CancellationToken.Register(() => { this._logger.LogInformation($"Cancelling subscription."); _subscribers.Remove(responseStream); }); while (!context.CancellationToken.IsCancellationRequested) { await Task.Delay(1000); } }