RejectedSoftware Forums

Sign up

SessionVar via runTask

class SomeWebInterface
{
    void get()
    {
        ...
        runTask(toDelegate(&someTask));
        ...
    }

    private auto someTask()
    {
        ...
        mySessionVar = "hello";
        ...
    }
}

Is there any way to have someTask set a SessionVar?

Re: SessionVar via runTask

On Thu, 23 Jul 2015 18:33:21 GMT, Lemonfiend wrote:

class SomeWebInterface
{
    void get()
    {
        ...
        runTask(toDelegate(&someTask));
        ...
    }

    private auto someTask()
    {
        ...
        mySessionVar = "hello";
        ...
    }
}

Is there any way to have someTask set a SessionVar?

You can find an answer to this by reading the web interface generator in the docs:
http://vibed.org/docs#web-interface-generator

Basically, you would need to do this:

class SomeWebInterface
{
  private SessionVar!(string, "mySessionVar") ms_mySessionVar;

  void getIndex()
  {
    ...
    someTask(); // The request info is task local. So, if you run this as a delegate in another task, the details about the request will be lost and you will get a segmentation fault! All requests have their own task, so it should not be necessary.
    ...
  }
  private auto someTask()
  {
    ms_mySessionVar = "hello"; // This will automatically emit a unique session cookie in the user's browser and set his server-side variable in the session storage of your choosing (in the HTTPServerSettings.sessionStore), so that it is available regardless of connection state.
  }
}

You can access this page using GET http://domain:port/index

Re: SessionVar via runTask

Am 23.07.2015 um 20:33 schrieb Lemonfiend:

class SomeWebInterface
{
     void get()
     {
         ...
         runTask(toDelegate(&someTask));
         ...
     }

     private auto someTask()
     {
         ...
         mySessionVar = "hello";
         ...
     }
}

Is there any way to have someTask set a SessionVar?

Since the request that determines the active session is bound to the
task that runs the request handler, this won't work using SessionVar -
it's basically implicitly a TaskLocal. But you could pass the
Session to the task and set the session field manually.

For that, the get method would have to have a HTTPServerRequest req
parameter and then pass req.session as an argument to runTask, which
forwards it to someTask.

Re: SessionVar via runTask

On Thu, 23 Jul 2015 23:11:33 +0200, Sönke Ludwig wrote:

Am 23.07.2015 um 20:33 schrieb Lemonfiend:

class SomeWebInterface
{
     void get()
     {
         ...
         runTask(toDelegate(&someTask));
         ...
     }

     private auto someTask()
     {
         ...
         mySessionVar = "hello";
         ...
     }
}

Is there any way to have someTask set a SessionVar?

Since the request that determines the active session is bound to the
task that runs the request handler, this won't work using SessionVar -
it's basically implicitly a TaskLocal. But you could pass the
Session to the task and set the session field manually.

For that, the get method would have to have a HTTPServerRequest req
parameter and then pass req.session as an argument to runTask, which
forwards it to someTask.

Ah, so SessionVars are simply a convenience for accessing req.session.get/set? I never made the connection.. that makes a lot of sense :)
Thanks.