Пример #1
0
 private void processMessage(byte[] receivedData)
 {
     using (MemoryStream responseStream = new MemoryStream(receivedData))
     {
         protobuf.ws.WsResponse response = ProtoBuf.Serializer.Deserialize <protobuf.ws.WsResponse>(responseStream);
         if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.TickerChannelSubscribed)
         {
             using (MemoryStream messageStream = new MemoryStream(response.Msg))
             {
                 protobuf.ws.TickerChannelSubscribedResponse message = ProtoBuf.Serializer.Deserialize <protobuf.ws.TickerChannelSubscribedResponse>(messageStream);
                 string channelId = "Ticker_" + message.CurrencyPair;
                 WsClient_OnSubscribe(channelId);
                 message.Datas.ForEach(delegate(protobuf.ws.TickerEvent evt) {
                     TickerEvent preparedEvent = prepareTickerEvent(evt);
                     preparedEvent.ChannelId   = "Ticker_" + message.CurrencyPair;
                     WsClient_OnTicker(preparedEvent);
                 });
             }
         }
         else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.TickerNotify)
         {
             using (MemoryStream messageStream = new MemoryStream(response.Msg))
             {
                 protobuf.ws.TickerNotification message = ProtoBuf.Serializer.Deserialize <protobuf.ws.TickerNotification>(messageStream);
                 string channelId = "Ticker_" + message.CurrencyPair;
                 message.Datas.ForEach(delegate(protobuf.ws.TickerEvent evt) {
                     TickerEvent preparedEvent = prepareTickerEvent(evt);
                     preparedEvent.ChannelId   = "Ticker_" + message.CurrencyPair;
                     WsClient_OnTicker(preparedEvent);
                 });
             }
         }
         else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.Error)
         {
             using (MemoryStream messageStream = new MemoryStream(response.Msg))
             {
                 protobuf.ws.ErrorResponse message = ProtoBuf.Serializer.Deserialize <protobuf.ws.ErrorResponse>(messageStream);
                 WsClient_OnError(message.Code, message.Message);
             }
         }
         else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.ChannelUnsubscribed)
         {
             using (MemoryStream messageStream = new MemoryStream(response.Msg)) {
                 protobuf.ws.ChannelUnsubscribedResponse message = ProtoBuf.Serializer.Deserialize <protobuf.ws.ChannelUnsubscribedResponse>(messageStream);
                 if (message.Type == protobuf.ws.UnsubscribeRequest.ChannelType.Ticker)
                 {
                     WsClient_OnUnsubscribe("Ticker_" + message.CurrencyPair);
                 }
             }
         }
         else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.LoginResponse)
         {
             WsClient_OnLogin();
         }
     }
 }
Пример #2
0
        private void processMessage(byte[] receivedData)
        {
            // parsing response
            using (MemoryStream responseStream = new MemoryStream(receivedData))
            {
                protobuf.ws.WsResponse response = ProtoBuf.Serializer.Deserialize <protobuf.ws.WsResponse>(responseStream);
                if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.TickerChannelSubscribed)
                {
                    using (MemoryStream messageStream = new MemoryStream(response.Msg))
                    {
                        protobuf.ws.TickerChannelSubscribedResponse message = ProtoBuf.Serializer.Deserialize <protobuf.ws.TickerChannelSubscribedResponse>(messageStream);
                        string channelId = "Ticker_" + message.CurrencyPair;
                        WsClient_OnSubscribe(channelId);
                        message.Datas.ForEach(delegate(protobuf.ws.TickerEvent evt) {
                            TickerEvent preparedEvent = prepareTickerEvent(evt);
                            preparedEvent.ChannelId   = channelId;
                            WsClient_OnTicker(preparedEvent);
                        });
                    }
                }
                else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.TickerNotify)
                {
                    using (MemoryStream messageStream = new MemoryStream(response.Msg))
                    {
                        protobuf.ws.TickerNotification message = ProtoBuf.Serializer.Deserialize <protobuf.ws.TickerNotification>(messageStream);
                        string channelId = "Ticker_" + message.CurrencyPair;
                        message.Datas.ForEach(delegate(protobuf.ws.TickerEvent evt) {
                            TickerEvent preparedEvent = prepareTickerEvent(evt);
                            preparedEvent.ChannelId   = channelId;
                            WsClient_OnTicker(preparedEvent);
                        });
                    }
                }
                if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.TradeChannelSubscribed)
                {
                    using (MemoryStream messageStream = new MemoryStream(response.Msg))
                    {
                        protobuf.ws.TradeChannelSubscribedResponse message = ProtoBuf.Serializer.Deserialize <protobuf.ws.TradeChannelSubscribedResponse>(messageStream);
                        string channelId = "Trade_" + message.CurrencyPair;
                        WsClient_OnSubscribe(channelId);
                        message.Datas.ForEach(delegate(protobuf.ws.TradeEvent evt) {
                            TradeEvent preparedEvent = prepareTradeEvent(evt);
                            preparedEvent.ChannelId  = channelId;
                            WsClient_OnTrade(preparedEvent);
                        });
                    }
                }
                else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.TradeNotify)
                {
                    using (MemoryStream messageStream = new MemoryStream(response.Msg))
                    {
                        protobuf.ws.TradeNotification message = ProtoBuf.Serializer.Deserialize <protobuf.ws.TradeNotification>(messageStream);
                        string channelId = "Trade_" + message.CurrencyPair;
                        message.Datas.ForEach(delegate(protobuf.ws.TradeEvent evt) {
                            TradeEvent preparedEvent = prepareTradeEvent(evt);
                            preparedEvent.ChannelId  = channelId;
                            WsClient_OnTrade(preparedEvent);
                        });
                    }
                }
                else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.Error)
                {
                    using (MemoryStream messageStream = new MemoryStream(response.Msg))
                    {
                        protobuf.ws.ErrorResponse message = ProtoBuf.Serializer.Deserialize <protobuf.ws.ErrorResponse>(messageStream);
                        WsClient_OnError(response.Meta.Token, message.Code, message.Message);
                    }
                }
                else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.ChannelUnsubscribed)
                {
                    using (MemoryStream messageStream = new MemoryStream(response.Msg)) {
                        protobuf.ws.ChannelUnsubscribedResponse message = ProtoBuf.Serializer.Deserialize <protobuf.ws.ChannelUnsubscribedResponse>(messageStream);
                        if (message.Type == protobuf.ws.UnsubscribeRequest.ChannelType.Ticker)
                        {
                            WsClient_OnUnsubscribe("Ticker_" + message.CurrencyPair);
                        }
                        else if (message.Type == protobuf.ws.UnsubscribeRequest.ChannelType.Trade)
                        {
                            WsClient_OnUnsubscribe("Trade_" + message.CurrencyPair);
                        }
                    }
                }
                else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.LoginResponse)
                {
                    WsClient_OnLogin();
                }
                else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.PrivateTradeChannelSubscribed)
                {
                    Console.WriteLine("Private Trades channel subscribed!");
                    using (MemoryStream messageStream = new MemoryStream(response.Msg))
                    {
                        protobuf.ws.PrivateTradeChannelSubscribedResponse message = ProtoBuf.Serializer.Deserialize <protobuf.ws.PrivateTradeChannelSubscribedResponse>(messageStream);
                        message.Datas.ForEach(delegate(protobuf.ws.PrivateTradeEvent evt) {
                            performPrivateTradeEvent(evt);
                        });
                    }
                }
                else if (response.Meta.ResponseType == protobuf.ws.WsResponseMetaData.WsResponseMsgType.PrivateTradeNotify)
                {
                    using (MemoryStream messageStream = new MemoryStream(response.Msg))
                    {
                        Console.WriteLine("Private Trades notification!");
                        Console.WriteLine(ByteArrayToString(response.Msg));
                        protobuf.ws.PrivateTradeNotification message = ProtoBuf.Serializer.Deserialize <protobuf.ws.PrivateTradeNotification>(messageStream);
                        message.Datas.ForEach(delegate(protobuf.ws.PrivateTradeEvent evt) {
                            performPrivateTradeEvent(evt);
                        });
                    }
                }

                // here you can make your trade decision
            }
        }