Example #1
0
        protected override Task HandleMessageAsync(IEnumerable <Action <DepthUpdateEventArgs> > callbacks, string stream, string json, CancellationToken token = default)
        {
            try
            {
                var jObject = JObject.Parse(json);

                var eventType = jObject["e"]?.Value <string>();

                DepthUpdateEventArgs eventArgs;

                switch (eventType)
                {
                case null:     // partial depth stream.
                {
                    var symbol = stream.Split('@')[0].ToUpperInvariant();

                    // Simulate event time.
                    var eventTime = DateTime.UtcNow.ToTimestamp().ToDateTime();

                    var lastUpdateId = jObject["lastUpdateId"].Value <long>();

                    var bids = jObject["bids"].Select(entry => (entry[0].Value <decimal>(), entry[1].Value <decimal>())).ToArray();
                    var asks = jObject["asks"].Select(entry => (entry[0].Value <decimal>(), entry[1].Value <decimal>())).ToArray();

                    eventArgs = new DepthUpdateEventArgs(eventTime, token, symbol, lastUpdateId, lastUpdateId, bids, asks);
                    break;
                }

                case "depthUpdate":
                {
                    var symbol    = jObject["s"].Value <string>();
                    var eventTime = jObject["E"].Value <long>().ToDateTime();

                    var firstUpdateId = jObject["U"].Value <long>();
                    var lastUpdateId  = jObject["u"].Value <long>();

                    var bids = jObject["b"].Select(entry => (entry[0].Value <decimal>(), entry[1].Value <decimal>())).ToArray();
                    var asks = jObject["a"].Select(entry => (entry[0].Value <decimal>(), entry[1].Value <decimal>())).ToArray();

                    eventArgs = new DepthUpdateEventArgs(eventTime, token, symbol, firstUpdateId, lastUpdateId, bids, asks);
                    break;
                }

                default:
                    Logger?.LogWarning($"{nameof(DepthClient)}.{nameof(HandleMessageAsync)}: Unexpected event type ({eventType}).");
                    return(Task.CompletedTask);
                }

                try
                {
                    if (callbacks != null)
                    {
                        foreach (var callback in callbacks)
                        {
                            callback(eventArgs);
                        }
                    }
                    DepthUpdate?.Invoke(this, eventArgs);
                }
                catch (OperationCanceledException) { /* ignore */ }
                catch (Exception e)
                {
                    if (!token.IsCancellationRequested)
                    {
                        Logger?.LogWarning(e, $"{nameof(DepthClient)}: Unhandled depth update event handler exception.");
                    }
                }
            }
            catch (OperationCanceledException) { /* ignore */ }
            catch (Exception e)
            {
                if (!token.IsCancellationRequested)
                {
                    Logger?.LogError(e, $"{nameof(DepthClient)}.{nameof(HandleMessageAsync)}");
                }
            }

            return(Task.CompletedTask);
        }
Example #2
0
        protected override void OnWebSocketEvent(WebSocketStreamEventArgs args, IEnumerable <Action <DepthUpdateEventArgs> > callbacks)
        {
            Logger?.LogDebug($"{nameof(DepthWebSocketClient)}: \"{args.Json}\"");

            try
            {
                var jObject = JObject.Parse(args.Json);

                var eventType = jObject["e"]?.Value <string>();

                DepthUpdateEventArgs eventArgs;

                switch (eventType)
                {
                case null:
                {
                    // Simulate event time.
                    var eventTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

                    var lastUpdateId = jObject["lastUpdateId"].Value <long>();

                    var bids = jObject["bids"].Select(entry => (entry[0].Value <decimal>(), entry[1].Value <decimal>())).ToArray();
                    var asks = jObject["asks"].Select(entry => (entry[0].Value <decimal>(), entry[1].Value <decimal>())).ToArray();

                    eventArgs = new DepthUpdateEventArgs(eventTime, args.Token, Symbol, lastUpdateId, lastUpdateId, bids, asks);
                    break;
                }

                case "depthUpdate":
                {
                    var symbol    = jObject["s"].Value <string>();
                    var eventTime = jObject["E"].Value <long>();

                    var firstUpdateId = jObject["U"].Value <long>();
                    var lastUpdateId  = jObject["u"].Value <long>();

                    var bids = jObject["b"].Select(entry => (entry[0].Value <decimal>(), entry[1].Value <decimal>())).ToArray();
                    var asks = jObject["a"].Select(entry => (entry[0].Value <decimal>(), entry[1].Value <decimal>())).ToArray();

                    eventArgs = new DepthUpdateEventArgs(eventTime, args.Token, symbol, firstUpdateId, lastUpdateId, bids, asks);
                    break;
                }

                default:
                    Logger?.LogWarning($"{nameof(DepthWebSocketClient)}.{nameof(OnWebSocketEvent)}: Unexpected event type ({eventType}).");
                    return;
                }

                try
                {
                    if (callbacks != null)
                    {
                        foreach (var callback in callbacks)
                        {
                            callback(eventArgs);
                        }
                    }
                    DepthUpdate?.Invoke(this, eventArgs);
                }
                catch (OperationCanceledException) { }
                catch (Exception e)
                {
                    if (!args.Token.IsCancellationRequested)
                    {
                        Logger?.LogError(e, $"{nameof(DepthWebSocketClient)}: Unhandled depth update event handler exception.");
                    }
                }
            }
            catch (OperationCanceledException) { }
            catch (Exception e)
            {
                if (!args.Token.IsCancellationRequested)
                {
                    Logger?.LogError(e, $"{nameof(DepthWebSocketClient)}.{nameof(OnWebSocketEvent)}");
                }
            }
        }