Hi
I am trying to get multiple options ata through Websocket but
after parsing the result, the feed response shows only data for 1 instrument key
can you please help and tell me what I am missing
This is my code
public async Task GetLiveMarketData()
{
using (ClientWebSocket webSocket = new ClientWebSocket())
{
// Configure the WebSocket headers as needed for authorization
webSocket.Options.SetRequestHeader(“Authorization”, $“Bearer {this.AccessToken}”);
// Connect to the WebSocket server
Uri serverUri = new Uri(WebSocketUri);
await webSocket.ConnectAsync(serverUri, CancellationToken.None);
Console.WriteLine("Connected to the server");
// Send subscription message
await SendSubscriptionMessage(webSocket);
// Receive data
await ReceiveData(webSocket);
// Close the WebSocket connection
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
}
static async Task SendSubscriptionMessage(ClientWebSocket webSocket)
{
var message = new
{
guid = Guid.NewGuid().ToString(),
method = "sub",
data = new
{
mode = "option_chain",
instrumentKeys = inst_key
}
};
string messageJson = System.Text.Json.JsonSerializer.Serialize(message);
byte[] messageBytes = Encoding.UTF8.GetBytes(messageJson);
await webSocket.SendAsync(new ArraySegment<byte>(messageBytes), WebSocketMessageType.Binary, true, CancellationToken.None);
Debug.WriteLine("Subscription message sent");
}
async Task ReceiveData(ClientWebSocket webSocket)
{
var buffer = new byte[1024 * 4]; // Adjust the buffer size as needed
try
{
while (webSocket.State == WebSocketState.Open)
{
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
Debug.WriteLine(result);
if (result.MessageType == WebSocketMessageType.Binary && result.Count > 0)
{
var feedResponse = FeedResponse.Parser.ParseFrom(buffer, 0, result.Count);
Debug.WriteLine(feedResponse);
for (int i=0;i<HomeVM.SelectedContracts.Count;i++)
{
string s = HomeVM.SelectedContracts[i].instrument_key;
if(HomeVM.SelectedContracts[i].buysell_option=="buy")
{
HomeVM.SelectedContracts[i].ws_closeprice = feedResponse.Feeds[s].Oc.BidAskQuote.Bp;
}
if(HomeVM.SelectedContracts[i].buysell_option=="sell")
{
HomeVM.SelectedContracts[i].ws_closeprice= feedResponse.Feeds[s].Oc.BidAskQuote.Ap;
}
}
//Console.WriteLine(feedResponse.Feeds["NSE_INDEX|Nifty Bank"].Oc.BidAskQuote.Ano);
//Console.WriteLine(feedResponse);
}
else if (result.MessageType == WebSocketMessageType.Close)
{
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
Debug.WriteLine("WebSocket connection closed.");
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception in ReceiveData: " + ex.Message);
}
}