Websocket IsConnected Status False

Debasish55_halder
private const string MyAPIKey = "sfgsgsg";
private const string MySecret = "xfvcvv";
static string MyUserId = "cbxhb";
Kite kite = new Kite(MyAPIKey, Debug: true);


protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["request_token"] != null)
{
string RequestToken = Convert.ToString(Request.QueryString["request_token"]);
User user = kite.GenerateSession(RequestToken, MySecret);
string MyAccessToken = user.AccessToken;
string MyPublicToken = user.PublicToken;
string UserId = "ZG6149";
Ticker ticker = new Ticker(MyAPIKey, UserId, MyPublicToken);
ticker.OnTick += OnTick;
ticker.OnOrderUpdate += OnOrderUpdate;
ticker.OnReconnect += OnReconnect;
ticker.OnNoReconnect += OnNoReconnect;
ticker.OnError += OnError;
ticker.OnClose += OnClose;
ticker.OnConnect += OnConnect;
ticker.EnableReconnect(Interval: 5, Retries: 50);
ticker.Connect();
ticker.Subscribe(Tokens: new UInt32[] { 256265 });
ticker.SetMode(Tokens: new UInt32[] { 256265 }, Mode: Constants.MODE_LTP);
LblResult.Text = Utils.JsonSerialize(ticker);
}
}


private static void initSession()
{
string RequestToken = Convert.ToString(HttpContext.Current.Session["REQUEST_TOKEN"]);
Kite kite = new Kite(MyAPIKey, Debug: true);
User user = kite.GenerateSession(RequestToken, MySecret);
Console.WriteLine(Utils.JsonSerialize(user));
}

private static void initTicker()
{
string RequestToken = Convert.ToString(HttpContext.Current.Session["REQUEST_TOKEN"]);
Kite kite = new Kite(MyAPIKey, Debug: true);
string MyAccessToken = Convert.ToString(HttpContext.Current.Session["ACCESS_TOKEN"]);
string MyPublicToken = Convert.ToString(HttpContext.Current.Session["PUBLIC_TOKEN"]);
string UserId = "ZG6149";
kite.SetAccessToken(MyAccessToken);
Ticker ticker = new Ticker(MyAPIKey, UserId, MyPublicToken);

ticker = new Ticker(MyAPIKey, MyAccessToken);

ticker.OnTick += OnTick;
ticker.OnReconnect += OnReconnect;
ticker.OnNoReconnect += OnNoReconnect;
ticker.OnError += OnError;
ticker.OnClose += OnClose;
ticker.OnConnect += OnConnect;
ticker.OnOrderUpdate += OnOrderUpdate;

ticker.EnableReconnect(Interval: 5, Retries: 50);
ticker.Connect();

ticker.Subscribe(Tokens: new UInt32[] { 256265 });
ticker.SetMode(Tokens: new UInt32[] { 256265 }, Mode: Constants.MODE_LTP);
}

private static void OnTokenExpire()
{
Console.WriteLine("Need to login again");
}

private static void OnConnect()
{
Console.WriteLine("Connected ticker");
}

private static void OnClose()
{
Console.WriteLine("Closed ticker");
}

private static void OnError(string Message)
{
Console.WriteLine("Error: " + Message);
}

private static void OnNoReconnect()
{
Console.WriteLine("Not reconnecting");
}

private static void OnReconnect()
{
Console.WriteLine("Reconnecting");
}

private static void OnTick(Tick TickData)
{
Console.WriteLine("Tick " + Utils.JsonSerialize(TickData));
}

private static void OnOrderUpdate(Order OrderData)
{
Console.WriteLine("OrderUpdate " + Utils.JsonSerialize(OrderData));
}
  • Debasish55_halder
    I have developed the above line of code to retrieve live data through kite connect API call using ASP.NET C#. I am neither able to retrieve the live data nor getting any error. It is only showing {"IsConnected":false} while I am trying to execute ticker events. Please provide the sample of code if anything is missing or mention the solution in order to get the live data using the API.
  • tonystark
    tonystark edited January 2019
    Hi @Debasish55_halder, Ticker has a long-standing connection. It is inappropriate to create an instance of ticker in the context of web page load because it will get closed after the page load is finished.

    Also, you are creating a ticker object inside a function like this:
    Ticker ticker = new Ticker(MyAPIKey, UserId, MyPublicToken);
    This ticker instance will get disposed immediately after the function execution is completed.

    If you want a ticker running continuously in the server then you have to create a command line application. The example code given in the github repository is for that. Or you have to create a service that is not depended on page events.

    PS: Do not post your App's credentials(api key, secret, tokens etc.) on the forum publicly. This may lead to unauthorised access.
Sign In or Register to comment.