Ejemplo n.º 1
0
        private void HandleData(KrakenMessageEventArgs eventArgs, SubscriptionStatus subscription)
        {
            var dataType = subscription.Subscription.Name;

            if (dataType == SubscribeOptionNames.Ticker)
            {
                var tickerMessage = TickerMessage.CreateFromString(eventArgs.RawContent, subscription);
                TickerReceived.InvokeAll(this, new TickerEventArgs(subscription.ChannelId.Value, subscription.Pair, tickerMessage));
            }
            if (dataType == SubscribeOptionNames.OHLC)
            {
                var ohlcMessage = OhlcMessage.CreateFromString(eventArgs.RawContent);
                OhlcReceived.InvokeAll(this, new OhlcEventArgs(subscription.ChannelId.Value, subscription.Pair, ohlcMessage));
            }
            if (dataType == SubscribeOptionNames.Trade)
            {
                var tradeMessage = TradeMessage.CreateFromString(eventArgs.RawContent);
                TradeReceived.InvokeAll(this, new TradeEventArgs(subscription.ChannelId.Value, subscription.Pair, tradeMessage));
            }
            if (dataType == SubscribeOptionNames.Spread)
            {
                var spreadMessage = SpreadMessage.CreateFromString(eventArgs.RawContent);
                SpreadReceived.InvokeAll(this, new SpreadEventArgs(subscription.ChannelId.Value, subscription.Pair, spreadMessage));
            }
            if (dataType == SubscribeOptionNames.Book)
            {
                if (eventArgs.RawContent.Contains(@"""as"":") && eventArgs.RawContent.Contains(@"""bs"":"))
                {
                    var bookSnapshotMessage = BookSnapshotMessage.CreateFromString(eventArgs.RawContent);
                    BookSnapshotReceived.InvokeAll(this, new KrakenDataEventArgs <BookSnapshotMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookSnapshotMessage));
                }
                if (eventArgs.RawContent.Contains(@"""a"":") || eventArgs.RawContent.Contains(@"""b"":"))
                {
                    var bookUpdateMessage = BookUpdateMessage.CreateFromString(eventArgs.RawContent);
                    BookUpdateReceived.InvokeAll(this, new KrakenDataEventArgs <BookUpdateMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookUpdateMessage));
                }
            }
        }
Ejemplo n.º 2
0
        private void HandleIncomingMessage(object sender, KrakenMessageEventArgs eventArgs)
        {
            logger.LogDebug("Handling incoming '{event}' message", eventArgs.Event);

            switch (eventArgs.Event)
            {
            case Heartbeat.EventName:
                var heartbeat = serializer.Deserialize <Heartbeat>(eventArgs.RawContent);
                logger.LogTrace("Heartbeat received: {heartbeat}", heartbeat);
                HeartbeatReceived.InvokeAll(this, heartbeat);
                break;

            case SystemStatus.EventName:
                var systemStatus = serializer.Deserialize <SystemStatus>(eventArgs.RawContent);
                logger.LogTrace("System status changed: {systemStatus}", systemStatus);
                SystemStatus = systemStatus;
                SystemStatusChanged.InvokeAll(this, systemStatus);
                break;

            case SubscriptionStatus.EventName:
                var subscriptionStatus = serializer.Deserialize <SubscriptionStatus>(eventArgs.RawContent);
                logger.LogTrace("Subscription status changed: {subscriptionStatus}", subscriptionStatus);

                SynchronizeSubscriptions(subscriptionStatus);
                SubscriptionStatusChanged.InvokeAll(this, subscriptionStatus);

                break;

            case "data":

                var subscription = Subscriptions.ContainsKey(eventArgs.ChannelId.Value) ? Subscriptions[eventArgs.ChannelId.Value] : null;
                if (subscription == null)
                {
                    logger.LogWarning("Didn't find a subscription for channelId {channelId}", eventArgs.ChannelId);
                    break;
                }

                var dataType = subscription.Subscription.Name;

                if (dataType == SubscribeOptionNames.Ticker)
                {
                    var tickerMessage = TickerMessage.CreateFromString(eventArgs.RawContent, subscription);
                    TickerReceived.InvokeAll(this, new TickerEventArgs(subscription.ChannelId.Value, subscription.Pair, tickerMessage));
                }
                if (dataType == SubscribeOptionNames.OHLC)
                {
                    var ohlcMessage = OhlcMessage.CreateFromString(eventArgs.RawContent);
                    OhlcReceived.InvokeAll(this, new OhlcEventArgs(subscription.ChannelId.Value, subscription.Pair, ohlcMessage));
                }
                if (dataType == SubscribeOptionNames.Trade)
                {
                    var tradeMessage = TradeMessage.CreateFromString(eventArgs.RawContent);
                    TradeReceived.InvokeAll(this, new TradeEventArgs(subscription.ChannelId.Value, subscription.Pair, tradeMessage));
                }
                if (dataType == SubscribeOptionNames.Spread)
                {
                    var spreadMessage = SpreadMessage.CreateFromString(eventArgs.RawContent);
                    SpreadReceived.InvokeAll(this, new SpreadEventArgs(subscription.ChannelId.Value, subscription.Pair, spreadMessage));
                }
                if (dataType == SubscribeOptionNames.Book)
                {
                    if (eventArgs.RawContent.Contains(@"""as"":") && eventArgs.RawContent.Contains(@"""bs"":"))
                    {
                        var bookSnapshotMessage = BookSnapshotMessage.CreateFromString(eventArgs.RawContent);
                        BookSnapshotReceived.InvokeAll(this, new KrakenDataEventArgs <BookSnapshotMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookSnapshotMessage));
                    }
                    if (eventArgs.RawContent.Contains(@"""a"":") || eventArgs.RawContent.Contains(@"""b"":"))
                    {
                        var bookUpdateMessage = BookUpdateMessage.CreateFromString(eventArgs.RawContent);
                        BookUpdateReceived.InvokeAll(this, new KrakenDataEventArgs <BookUpdateMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookUpdateMessage));
                    }
                }

                break;

            default:
                logger.LogWarning("Could not handle incoming message: {message}", eventArgs.RawContent);
                break;
            }
        }