/// <summary> /// Reads stream message. /// </summary> private void ReadStreamMessage() { try { String message = ReadMessage(); if (message != null) { JSONObject responseBody = JSONObject.Parse(message); string commandName = responseBody["command"].ToString(); if (commandName == "tickPrices") { StreamingTickRecord tickRecord = new StreamingTickRecord(); tickRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (TickRecordReceived != null) { TickRecordReceived.Invoke(tickRecord); } if (sl != null) { sl.ReceiveTickRecord(tickRecord); } } else if (commandName == "trade") { StreamingTradeRecord tradeRecord = new StreamingTradeRecord(); tradeRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (TradeRecordReceived != null) { TradeRecordReceived.Invoke(tradeRecord); } if (sl != null) { sl.ReceiveTradeRecord(tradeRecord); } } else if (commandName == "balance") { StreamingBalanceRecord balanceRecord = new StreamingBalanceRecord(); balanceRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (BalanceRecordReceived != null) { BalanceRecordReceived.Invoke(balanceRecord); } if (sl != null) { sl.ReceiveBalanceRecord(balanceRecord); } } else if (commandName == "tradeStatus") { StreamingTradeStatusRecord tradeStatusRecord = new StreamingTradeStatusRecord(); tradeStatusRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (TradeStatusRecordReceived != null) { TradeStatusRecordReceived.Invoke(tradeStatusRecord); } if (sl != null) { sl.ReceiveTradeStatusRecord(tradeStatusRecord); } } else if (commandName == "profit") { StreamingProfitRecord profitRecord = new StreamingProfitRecord(); profitRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (ProfitRecordReceived != null) { ProfitRecordReceived.Invoke(profitRecord); } if (sl != null) { sl.ReceiveProfitRecord(profitRecord); } } else if (commandName == "news") { StreamingNewsRecord newsRecord = new StreamingNewsRecord(); newsRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (NewsRecordReceived != null) { NewsRecordReceived.Invoke(newsRecord); } if (sl != null) { sl.ReceiveNewsRecord(newsRecord); } } else if (commandName == "keepAlive") { StreamingKeepAliveRecord keepAliveRecord = new StreamingKeepAliveRecord(); keepAliveRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (KeepAliveRecordReceived != null) { KeepAliveRecordReceived.Invoke(keepAliveRecord); } if (sl != null) { sl.ReceiveKeepAliveRecord(keepAliveRecord); } } else if (commandName == "candle") { StreamingCandleRecord candleRecord = new StreamingCandleRecord(); candleRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (CandleRecordReceived != null) { CandleRecordReceived.Invoke(candleRecord); } if (sl != null) { sl.ReceiveCandleRecord(candleRecord); } } else { throw new APICommunicationException("Unknown streaming record received"); } } } catch (Exception) { } }
/// <summary> /// Handler for streaming message. /// </summary> /// <param name="sender">Sender</param> /// <param name="args">Args</param> private void StreamingMessageHandler(object sender, IncomingMessageArgs args) { var message = args.Message.Parameters.GetString("update"); JSONObject responseBody = (JSONObject)JSONObject.Parse(message); string commandName = responseBody["command"].ToString(); if (RecordReceived != null) { RecordReceived.Invoke(message); } switch (commandName) { case "tickPrices": TickRecord tickRecord = new TickRecord((JSONObject)responseBody["data"]); if (TickPricesRecordReceived != null) { TickPricesRecordReceived.Invoke(tickRecord); } break; case "trade": StreamingTradeRecord tradeRecord = new StreamingTradeRecord((JSONObject)responseBody["data"]); if (TradeRecordReceived != null) { TradeRecordReceived.Invoke(tradeRecord); } break; case "indicators": IndicatorsRecord balanceRecord = new IndicatorsRecord((JSONObject)responseBody["data"]); if (AccountIndicatorsRecordReceived != null) { AccountIndicatorsRecordReceived.Invoke(balanceRecord); } break; case "orderStatus": OrderStatusRecord tradeStatusRecord = new OrderStatusRecord((JSONObject)responseBody["data"]); if (OrderStatusRecordReceived != null) { OrderStatusRecordReceived.Invoke(tradeStatusRecord); } break; case "profit": ProfitRecord profitRecord = new ProfitRecord((JSONObject)responseBody["data"]); if (ProfitRecordReceived != null) { ProfitRecordReceived.Invoke(profitRecord); } break; case "news": NewsRecord newsRecord = new NewsRecord((JSONObject)responseBody["data"]); if (NewsRecordReceived != null) { NewsRecordReceived.Invoke(newsRecord); } break; case "keepAlive": KeepAliveRecord keepAliveRecord = new KeepAliveRecord((JSONObject)responseBody["data"]); if (KeepAliveRecordReceived != null) { KeepAliveRecordReceived.Invoke(keepAliveRecord); } break; case "candle": StreamingCandleRecord candleRecord = new StreamingCandleRecord((JSONObject)responseBody["data"]); if (CandleRecordReceived != null) { CandleRecordReceived.Invoke(candleRecord); } break; case "error": StreamingErrorRecord streamingErrorRecord = new StreamingErrorRecord((JSONObject)responseBody["data"]); if (ErrorRecordReceived != null) { ErrorRecordReceived.Invoke(streamingErrorRecord); } break; default: throw new APICommunicationException("Unknown streaming record received"); } }