Beispiel #1
0
        /// <inheritdoc />
        public BinanceWebsocketClient(IBinanceCommunicator communicator)
        {
            BnbValidations.ValidateInput(communicator, nameof(communicator));

            _communicator = communicator;
            _messageReceivedSubscription = _communicator.MessageReceived.Subscribe(HandleMessage);
        }
Beispiel #2
0
        /// <summary>
        /// Combine url with subscribed streams
        /// </summary>
        public Uri PrepareSubscriptions(Uri baseUrl, params SubscriptionBase[] subscriptions)
        {
            BnbValidations.ValidateInput(baseUrl, nameof(baseUrl));

            if (subscriptions == null || !subscriptions.Any())
            {
                throw new BinanceBadInputException("Please provide at least one subscription");
            }

            var streams     = subscriptions.Select(x => x.StreamName).ToArray();
            var urlPart     = string.Join("/", streams);
            var urlPartFull = $"/stream?streams={urlPart}";

            var currentUrl = baseUrl.ToString().Trim();

            if (currentUrl.Contains("stream?"))
            {
                // do nothing, already configured
                return(baseUrl);
            }

            var newUrl = new Uri($"{currentUrl.TrimEnd('/')}{urlPartFull}");

            return(newUrl);
        }
        /// <inheritdoc />
        public BinanceWebsocketClient(IBinanceCommunicator communicator, IBinanceReferenceOrderBookCommunicator referenceOrderBookCommunicator = null)
        {
            BnbValidations.ValidateInput(communicator, nameof(communicator));

            _communicator = communicator;
            _messageReceivedSubscription = _communicator.MessageReceived.Subscribe(HandleMessage);

            _referenceOrderBookCommunicator  = referenceOrderBookCommunicator;
            _rlobMessageReceivedSubscription = _referenceOrderBookCommunicator.MessageReceived.Subscribe(HandleMessage);
        }
Beispiel #4
0
        /// <summary>
        /// Serializes request and sends message via websocket communicator.
        /// It logs and re-throws every exception.
        /// </summary>
        /// <param name="request">Request/message to be sent</param>
        public async Task Send <T>(T request)
        {
            try
            {
                BnbValidations.ValidateInput(request, nameof(request));

                var serialized = BinanceJsonSerializer.Serialize(request);
                await _communicator.Send(serialized).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Log.Error(e, L($"Exception while sending message '{request}'. Error: {e.Message}"));
                throw;
            }
        }