Esempio n. 1
0
        public async Task SubscribePriceStream(PriceSubscriptionRequestDto request)
        {
            _contextHolder.PricingHubClient = Clients;

            Log.InfoFormat("Received subscription request {0} from connection {1}", request, Context.ConnectionId);

            if (!_currencyPairRepository.Exists(request.CurrencyPair))
            {
                Log.WarnFormat("Received a subscription request for an invalid currency pair '{0}', it was ignored.", request.CurrencyPair);
                return;
            }

            // simulate slow response for EURCHF
            if (request.CurrencyPair == "EURCHF")
            {
                await Task.Delay(TimeSpan.FromSeconds(2));
            }

            // add client to this group
            var groupName = string.Format(PriceStreamGroupPattern, request.CurrencyPair);
            await Groups.Add(Context.ConnectionId, groupName);
            Log.InfoFormat("Connection {0} added to group '{1}'", Context.ConnectionId, groupName);

            // send current price to client
            var lastValue = _priceLastValueCache.GetLastValue(request.CurrencyPair);
            await Clients.Caller.OnNewPrice(lastValue);
            Log.InfoFormat("Snapshot published to {0}: {1}", Context.ConnectionId, lastValue);
        }
Esempio n. 2
0
        public async Task UnsubscribePriceStream(PriceSubscriptionRequestDto request)
        {
            Log.InfoFormat("Received unsubscription request {0} from connection {1}", request, Context.ConnectionId);

            if (!_currencyPairRepository.Exists(request.CurrencyPair))
            {
                Log.WarnFormat("Received an unsubscription request for an invalid currency pair '{0}', it was ignored.", request.CurrencyPair);
                return;
            }

            // remove client from the group
            var groupName = string.Format(PriceStreamGroupPattern, request.CurrencyPair);
            await Groups.Remove(Context.ConnectionId, groupName);
            Log.InfoFormat("Connection {0} removed from group '{1}'", Context.ConnectionId, groupName);
        }