Exemplo n.º 1
0
        private void InvokeMessageReceived(string channelName, string requestType, string message)
        {
            MessageType messageType = GetMessageType(requestType);

            switch (messageType)
            {
            case MessageType.OrderData:
                var orderMarketInfo = MarketInfo.ParseMarketInfo(channelName);
                var orders          = Helper.ToEntity <OrderResponse>(message);

                OnOrderMessage?.Invoke(orderMarketInfo.Exchange, orderMarketInfo.PrimaryCurrency, orderMarketInfo.SecondaryCurrency, orders.OrderData.Orders);
                break;

            case MessageType.TradeData:
                var tradeMarketInfo = MarketInfo.ParseMarketInfo(channelName);
                var trade           = Helper.ToEntity <TradeResponse>(message);

                OnTradeMessage?.Invoke(tradeMarketInfo.Exchange, tradeMarketInfo.PrimaryCurrency, tradeMarketInfo.SecondaryCurrency, trade.TradeData.Trade);
                break;

            case MessageType.BlockData:
                var blockInfo = BlockItem.ParseBlockInfo(channelName);

                OnBlockMessage?.Invoke(blockInfo, new BlockItem());
                break;

            case MessageType.FavoriteData:
                var favourites = Helper.ToEntity <FavoriteResponse>(message);

                OnFavoriteMessage?.Invoke(favourites.Data.FavoritesDataData.Favorites);
                break;

            case MessageType.NotificationData:
                var notification = Helper.ToEntity <NotificationResponse>(message);

                OnNotificationMessage?.Invoke(notification.NotificationData.NotificationDataData.NotificationDataItem);
                break;

            case MessageType.NewsData:
                var news = Helper.ToEntity <NewsResponse>(message);

                OnNewsMessage?.Invoke(news.NewsData.NewsDataItem);
                break;

            case MessageType.NewMarket:
                var newMarket = Helper.ToEntity <NewMarketResponse>(message);

                OnNewMarketMessage?.Invoke(newMarket.NewMarketData.NewMarketDataItem);
                break;
            }

            OnMessage?.Invoke(messageType, message);
        }
Exemplo n.º 2
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;
            }
        }