0

I'm using XSockets to communicate between an ASP.NET website and some server. When I receive a callback from the server via XSockets I would like to notify the frontend of the ASP.NET website using Response().

My main Default.aspx.cs page looks like so:

// I want to access all these variables in the callback method
public HttpContext ctx;
public HttpSessionState session;
public int _theNumber;
public static CommHandler _commHandler

private void Page_Load(object sender, System.EventArgs e)
{
    _theNumber = 4; // Random number
    ctx = HttpContext.Current;

    _commHandler = new CommHandler( ...
                                    aPromptCallback: PromptCallback
                                    ...) //The callback, amongst othr parameters 

    session = HttpContext.Current.Session; //Try to access this in callback
    HttpContext.Current.Session["SomeClass"] = _someObject;
}

The CommHandler basically sends data to some server using XSockets and expects (asynchronously) a reply with some data.

In the same Default.aspx.cs I have a function for a button click defined as below:

protected void Deploy(object sender, EventArgs e)
{
    _commHandler.SendData()
}

The CommHandler makes use of the XSockets Send() method to send the data. When the CommHandler receives data a callback inside it is triggered which calls the PromptCallback()

Now the PromptCallback() is defined like so in the same Default.aspx.cs file:

protected void PromptCallback(string aStr)
{
    var v = _theNumber; // This works as expected, the _theNumber is 4

    var j = (SomeClass)session["SomeClass"] // I can still access the session..

    ctx.Response.Write(aStr); // This does not work, amazingly ctx is now null

    //HttpContext.Current.Response.Write(aStr); // This does not work either
}

Why is it that the _theNumber is still accessible at the callback, but the HttpContext is null by then?

(I get the idea that the lifetime of the callback may outlive the lifetime of the HttpContext, but why is the session/_theNumber accessible still there?)

I've read similar questions (1,2) / articles (1,2) but am still uncertain on how to solve this.

Community
  • 1
  • 1
Serge P
  • 1,591
  • 8
  • 22
  • 42

1 Answers1

0

Does not the browser used support websockets since you are using communication from the server side instead of JavaScript?

You will probably now be able to use the HttpContext since the call to XSockets (in version 3.0.6) always is async.

Use XSockets 4.0 (currently in pre-release) and you will be able to do synchron communication and wait for the respons.. Then send it back in the response. Search this page http://xsockets.github.io/XSockets.NET-4.0/ for "Return data synchronously to caller" There are sample both for C# and JS.

Still do not understand the use-case, but I am sure you have a good reason for this backward thing :)

Uffe
  • 2,275
  • 1
  • 13
  • 9