Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    HOW TO CONVERT BINARY DATA OF RECEIVED FROM WEBSOCKET

    General Discussion
    0
    2
    19
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      rajsharma123 last edited by

      THIS IS MY CODE

      async void ConnectWebsocket1(string agr, string Feedtoken)
      {
      TokenList tokenList1 = new TokenList();
      List<string> tokens1 = new List<string>();
      tokens1.Add("3045");
      tokenList1.tokens = tokens1;
      tokenList1.exchangeType = 1;
      TokenList tokenList2 = new TokenList();
      List<string> tokens2 = new List<string>();
      tokens2.Add("99926009");
      tokenList2.tokens = tokens2;
      tokenList2.exchangeType = 5;
      List<TokenList> tokenLists = new List<TokenList>();
      tokenLists.Add(tokenList1);
      tokenLists.Add(tokenList2);

      Params parameters = new Params();
      parameters.tokenList = tokenLists;
      parameters.mode = 1;
      
      WebSocketData webSocketData = new WebSocketData();
      webSocketData.@params = parameters;
      webSocketData.action = 1;
      webSocketData.correlationID = ""; 
      string jsonString = JsonConvert.SerializeObject(webSocketData);             
      var ws = new ClientWebSocket();   
      ws.Options.SetRequestHeader("Authorization", "Bearer " + agr);
      ws.Options.SetRequestHeader("x-api-key", "XXXXXX");
      ws.Options.SetRequestHeader("x-client-code", "XXXXXX");
      ws.Options.SetRequestHeader("x-feed-token", Feedtoken);
      await ws.ConnectAsync(new Uri("ws://smartapisocket.angelone.in/smart-stream"), CancellationToken.None);
      
      byte[] buf = new byte[1056];
      ArraySegment<byte> buffer = new ArraySegment<byte>(buf);
      
      string mynewstring = jsonString;
      
      var encoded = Encoding.UTF8.GetBytes(mynewstring);
      var sbuffer = new ArraySegment<Byte>(encoded, 0, encoded.Length);
      await ws.SendAsync(sbuffer, WebSocketMessageType.Text, true, cancellationToken: CancellationToken.None);
      

      while (ws.State == System.Net.WebSockets.WebSocketState.Open)
      {
      var result = await ws.ReceiveAsync(buffer, CancellationToken.None);

          if (result.MessageType == WebSocketMessageType.Close)
          {
              await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
              Response.Write(result.CloseStatusDescription + "<br/>");
              Console.WriteLine(result.CloseStatusDescription);
          }
          else
          {          
             Response.Write(Encoding.UTF8.GetString(buffer.Array, 0, result.Count) + "<br/>");       
              break;         
          }
      }
      

      }

      M 1 Reply Last reply Reply Quote 1
      • M
        Moderator_2 @rajsharma123 last edited by

        Hi @rajsharma123

        Apologies for delayed response.
        Kindly explore on our sample websocket code on github
        https://github.com/angel-one/smartapi-java/tree/main/src/main/java/com/angelbroking/smartapi/sample

        Thanks & Regards,
        SmartAPI team

        1 Reply Last reply Reply Quote 0
        • First post
          Last post