WebSocket Response c#

Amol
I am able to connect to web Socket server but the server return message every second, but it's just an array with a element "0".

I have tried following messages:
1: string message = @{'a': 'subscribe', v: [48447]};
ws.Send(JsonConvert.SerializeObject(message));

2: string message = "{\"a\": \"subscribe\", v: [48447]}";
ws.Send(JsonConvert.SerializeObject(message));

3 string message = "{\"a\": \"mode\", \"v\": [\"full\", [48447]}";
ws.Send(JsonConvert.SerializeObject(message));

Let me know where i am doing wrong.
  • Vivek
    @Amol Its an heartbeat signal, you can safely ignore those messages and also your client can use it to find any disconnections happened.
  • Amol
    @vivek . I got it ,it is heartbeat message but still i m not receiving data.What should i do for data part.
  • Vivek
    @Amol Did you subscribe for any tokens? Until you subscribe for a tradingsymbol with the corresponding token you won't get data.
  • Amol
    @vivek I have me mention in 1st message the request which i m sending for subscribing the data.
  • Vivek
    @Amol Please try for liquid scrip such as Reliance(738561) and let us know
  • Amol
    @vivek no still not receiving data can u check my message format is it ok.
    and i have only subscribe for NFO so can i receive data of NSE Reliance.


    1: string message = @{'a': 'subscribe', v: [738561]};
    ws.Send(JsonConvert.SerializeObject(message));

    2: string message = "{\"a\": \"subscribe\", v: [738561]}";
    ws.Send(JsonConvert.SerializeObject(message));

    3 string message = "{\"a\": \"mode\", \"v\": [\"full\", [738561]}";
    ws.Send(JsonConvert.SerializeObject(message));
  • Vivek
    @amol It seems like your JSON format is wrong. For subscription you have send JSON string like
    '{"a": "subscribe", "v": [738561]}'
  • Amol
    @vivek please mention the right format which u exprect at ur end .I will really helpful
  • Amol
    still not receiving data after using above format
    '{"a": "subscribe", "v": [260355]}'
  • Kailash
    @Amol are you getting any messages at all (you should get a 1-byte / second heart beat). If you are not, it's likely that your connection itself was not established properly.
  • Amol
    @Kailash I am able to connect and receive data but data received is not getting parsed properly.

    Data Received is byte array of 48 length and i am sending request for full mode
    Message format for subscription :
    1.message = {"a": "subscribe", "v": [12402434]};
    2.message = {"a": "mode", "v": ["full", [12402434]};

    byte[48] :
    [0]: 0 [1]: 1 [2]: 0 [3]: 44 [4]: 7 [5]: 162 [6]: 186
    [7]: 4 [8]: 0 [9]: 2 [10]: 86 [11]: 182 [12]: 0 [13]: 0
    [14]: 0 [15]: 20 [16]: 0 [17]: 0 [18]: 0 [19]: 27 [20]: 0 [21]: 0
    [22]: 6 [23]: 206 [24]: 0 [25]: 0 [26]: 32 [27]: 44 [28]: 0 [29]: 0
    [30]: 27 [31]: 6 [32]: 0 [33]: 2 [34]: 82 [35]: 106 [36]: 0 [37]: 2
    [38]: 88 [39]: 100 [40]: 0 [41]: 2 [42]: 81 [43]: 192 [44]: 0 [45]: 2
    [46]: 82 [47]: 106

    1.First two byte give o/p as 1 proper
    int i = BitConverter.ToInt16(e.TextB, 1);

    2.next 2 byte give o/p as 44 proper
    int i = BitConverter.ToInt16(e.TextB, 3);

    3.next 4 byte give o/p as 147389 but expected o/p was 12402434
    int i = BitConverter.ToInt32(e.TextB, 5);

  • Kailash
    @Amol , I looked up the documentation for BitConverter.ToInt16 and 32, and the second parameter is described as 'startIndex'. Your code snippet seems to be passing the endIndex. That means, your code should be in the following manner, right?
    int i = BitConverter.ToInt16(e.TextB, 0);
    int i = BitConverter.ToInt16(e.TextB, 2);
    int i = BitConverter.ToInt32(e.TextB, 4);
  • Amol
    @Kailash I already tried the above mention input but after using this output is different.

    1: System.BitConverter.ToInt16(e.TextB, 1) o/p : 1
    2: System.BitConverter.ToInt16(e.TextB, 0) o/p : 256

    3.System.BitConverter.ToInt16(e.TextB, 3) o/p : 1836
    4.System.BitConverter.ToInt16(e.TextB, 2) o/p : 11264

    5.System.BitConverter.ToInt32(e.TextB, 4) o/p : 37731584
    6.System.BitConverter.ToInt32(e.TextB, 5) o/p : 147389
  • Kailash
    @Amol Let me see if I can replicate this in C# at my end.
  • Shabeershah2002
    Hi Amol,
    Have you succeeded converting above byte() array ? if yes please share with me too as i am facing the issue ,

    Hi Kailash , have you tried this at your end ?, i am also getting the same out put as Amol mentioned while converting to int16
  • Amol
    @Shabeershah2002 Yes I succeeded.Issue is related to byte array.

    You have to reverse the array which you receive but field wise.i.e

    byte[48] :
    [0]: 0 [1]: 1 [2]: 0 [3]: 44 [4]: 7 [5]: 162 [6]: 186
    [7]: 4 [8]: 0 [9]: 2 [10]: 86 [11]: 182 [12]: 0 [13]: 0
    [14]: 0 [15]: 20 [16]: 0 [17]: 0 [18]: 0 [19]: 27 [20]: 0 [21]: 0
    [22]: 6 [23]: 206 [24]: 0 [25]: 0 [26]: 32 [27]: 44 [28]: 0 [29]: 0
    [30]: 27 [31]: 6 [32]: 0 [33]: 2 [34]: 82 [35]: 106 [36]: 0 [37]: 2
    [38]: 88 [39]: 100 [40]: 0 [41]: 2 [42]: 81 [43]: 192 [44]: 0 [45]: 2
    [46]: 82 [47]: 106

    if you want int 32 then take 4 byte from array then reverse the small array and try to convert it.

    Actual array : [4]: 7 [5]: 162 [6]: 186 [7]: 4
    Reverse Array :[7]: 4 [6]: 186 [5]: 162 [4]: 7

    then use System.BitConverter.ToInt32({4,186,162,7})

    revert in case of any query
  • chaudhariapurva
    chaudhariapurva edited June 2016
    use-->
    check for BitConverter.IsLittleEndian first

    then-->
    char[] b = new char[4];
    Array.Copy(a, 0, b, 0, 4);
    a = source array
    0 = start index in source array
    b = destination array
    0 = start index in destination array
    4 = elements to copy

    then --> byte[] reversed = bytes.Reverse().ToArray(); on the sub array

    then use bit Converter
  • Shabeershah2002
    Thank you so much !, all is well now........yes after reversing each field of array and converting worked ......
  • Shabeershah2002
    Hi

    Now i can successfully get quote by passing this JSON :
    {"a": "subscribe", "v": [408065, 884737]}

    But i can subscribe neither "ltp" nor "full" by default i am getting "qoute", have tried below formats to change mode but nothing seems getting changed , please let me know what i am missing ?

    {"a": "mode", "v": ["full", [408065]}
    {"a": "mode", "v": ["ltp", [408065]}
  • RS4988
    Hi

    Could you please share the sample piece of code to connect web socket server using C#

    Thanks
  • shortwire
    Yes, thats going to be extremely useful. We lack C# library unlike Python and Java. Hence if someone can post a code starting from authentication, getting token, using the same to make a subscription and then receiving and parsing a full mode quote, thats really going to be helpful
Sign In or Register to comment.