private static void MyCustomCallback(MessageType messageType, string data)
        {
            // this is our custom callback we can do whatever we want with the data here
            OutputConsole.WriteLine($"{DateTime.UtcNow}: Received {messageType} message in private channel callback", ConsoleColor.Green);

            switch (messageType)
            {
            // if you want to deserialize into one of the premade classes you can do so quite easily by checking the MessageType and deserializing into the apropriate response class
            case MessageType.TradeData:
                try
                {
                    var deserializedClass = TradeResponse.FromJson(_client.Options.Serializer, data);
                    OutputConsole.WriteLine($"{DateTime.UtcNow}: We have deserialized a trade in our custom callback and the price in that trade is {deserializedClass.TradeData.Trade.Price}", ConsoleColor.Green);
                }
                catch (Exception ex)
                {
                    OutputConsole.WriteLine(ex.Message);
                }

                break;

            case MessageType.OrderData:
                try
                {
                    var deserializedClass = OrderResponse.FromJson(_client.Options.Serializer, data);
                    OutputConsole.WriteLine($"{DateTime.UtcNow}: We have deserialized an orderbook in our custom callback and the first price in that data is {deserializedClass.OrderData.Orders[0].Price}", ConsoleColor.Green);
                }
                catch (Exception ex)
                {
                    OutputConsole.WriteLine(ex.Message);
                }

                break;

            case MessageType.NewsData:
                break;

            case MessageType.BlockData:
                break;

            case MessageType.FavoriteData:
                break;

            case MessageType.NewMarket:
                break;

            case MessageType.NotificationData:
                break;

            case MessageType.Unknown:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(messageType), messageType, null);
            }
        }
Beispiel #2
0
        private static void MyCustomCallback(MessageType messageType, string data)
        {
            // this is our custom callback we can do whatever we want with the data here
            OutputConsole.WriteLine($"{DateTime.UtcNow}: Received {messageType} message in private channel callback", ConsoleColor.Green);

            // if you want to deserialize into one of thhe premade classes you can do so quite easily by checking the MessageType and deserializing into the apropriate response class
            if (messageType == MessageType.TradeData)
            {
                var deserializedClass = TradeResponse.FromJson(data);
                OutputConsole.WriteLine($"{DateTime.UtcNow}: We have deserialized a trade in our custom callbeck and the price in that trade is {deserializedClass.TradeData.Trade.Price}", ConsoleColor.Green);
            }
        }
Beispiel #3
0
        private void _scClient_OnMessage(string message)
        {
            Debug.WriteLine(message);
            // check if this is a publish message
            var m = Regex.Match(message, @"^{""event""*.:*.""#publish""");

            if (!m.Success)
            {
                return;
            }

            // get the channel

            var jobj = Options.Serializer.Deserialize <dynamic>(message);

            string channel = jobj["data"]["channel"].ToString();

            string reqtype;

            if (channel.Contains('-') && !Guid.TryParse(channel, out _))
            {
                reqtype = channel.Substring(0, channel.IndexOf('-'));
            }
            else
            {
                reqtype = channel;
                if (Guid.TryParse(channel, out var cguid))
                {
                    if (string.Equals(cguid.ToString(), channel, StringComparison.OrdinalIgnoreCase))
                    {
                        // ok we have a private channel so lets find out what data we have
                        reqtype = jobj["data"]["data"]["MessageType"].ToString();
                    }
                }
            }

            var hascb = (from c in _subscribeCallbacs where c.Key == channel select c).Any();

            if (hascb)
            {
                var cbs = from c in _subscribeCallbacs where c.Key == channel select c.Value;
                foreach (var cb in cbs)
                {
                    cb.Invoke(GetMessageType(reqtype), message);
                }
            }

            switch (reqtype.ToUpper())
            {
            case "ORDER":
                var ominfo = ParseMarketInfo(channel);
                var orders = OrderResponse.FromJson(Options.Serializer, message);
                OnOrderMessage?.Invoke(ominfo.Exchange, ominfo.Curr1, ominfo.Curr2, orders.OrderData.Orders);
                OnMessage?.Invoke(MessageType.OrderData, message);
                break;

            case "TRADE":
                var tminfo = ParseMarketInfo(channel);
                var trade  = TradeResponse.FromJson(Options.Serializer, message);
                OnTradeMessage?.Invoke(tminfo.Exchange, tminfo.Curr1, tminfo.Curr2, trade.TradeData.Trade);
                OnMessage?.Invoke(MessageType.TradeData, message);
                break;

            case "BLOCK":
                OnBlockMessage?.Invoke(ParseBlockInfo(channel), new BlockItem());
                OnMessage?.Invoke(MessageType.BlockData, message);
                break;

            case "FAVORITE":
                OnFavoriteMessage?.Invoke(FavoriteResponse.FromJson(Options.Serializer, message).Data.FavoritesDataData.Favorites);
                OnMessage?.Invoke(MessageType.FavoriteData, message);
                break;

            case "NOTIFICATION":
                OnNotificationMessage?.Invoke(NotificationResponse.FromJson(Options.Serializer, message).NotificationData.NotificationDataData.NotificationDataItem);
                OnMessage?.Invoke(MessageType.NotificationData, message);
                break;

            case "NEWS":
                OnNewsMessage?.Invoke(NewsResponse.FromJson(Options.Serializer, message).NewsData.NewsDataItem);
                OnMessage?.Invoke(MessageType.NewsData, message);
                break;

            case "NEWMARKET":
                OnNewMarketMessage?.Invoke(NewMarketResponse.FromJson(Options.Serializer, message).NewMarketData.NewMarketDataData);
                OnMessage?.Invoke(MessageType.NewMarket, message);
                break;

            default:
                OnMessage?.Invoke(MessageType.Unknown, message);
                break;
            }
        }