Test Results

About us

 Using Sessions in Remote Functions
Total posts: 1
 Author Using Sessions in Remote Functions
Danijel Tkalcec [RTC]

01.06.2006 20:08:13
Registered user
How do you access Session data from the OnExecute event? Through the “Session” property of your Connection component (Sender:TRtcConnection).

If your remote functions have to use Sessions, the best thing to do is to activate automatic sessions (rtcServerModule.AutoSession:=True and rtcClientModule.AutoSessions:=True), so you can access Session data directly, without having to search for it and manually send Session ID’s to and from the client.

When automatic sessions are activated, you can access all session data related to the client who called the function from inside function’s OnExecute event.

Here is one example code:
with TRtcDataServer(Sender) do
  // I will store user’s name inside session’s “name” variable
  // and expect from the client to send me his username on each call
  // as function’s “name” parameter, which I will check against my
  // data stored inside the Session …

  if  Session.asString[’name’]='’ then
  else if Session.asString[’name’]<>Param[’name’] then
    raise Exception.Create(’You are using someone else’’s Session ID’);

  // I will use another Session variable, which I will name “visits”
  // and use as a counter to count how often user has called this
  // or any other remote functions.


  // I will now return the result as a record with 2 values.
  // “Hello” as a string, to say “hello” to my user and
  // “count” as integer to return the number of visits

  with Result.newRecord do
    asString[’hello’]:=’Hello ‘+Session.asString[’name’]+’!';

Please note that this is only a dumb example of using Session data and returning a record from a remote function. You can use any data types and store as much data in your session objects as you want.

The same goes for sending a result to the calling client. Session object is comparable to a Record, holding any number of values of any type. The Result object is comparable to a single object and can be filled with anything from a single value to any complex structure.

You can combine all data types and store and/or send ANY structure you want inside session and inside Result object.