RTC Forums
November 24, 2024, 08:31:40 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: TObject & Sessions  (Read 4395 times)
hannesgw
RTC Expired
*
Posts: 11


« on: January 08, 2013, 05:52:29 PM »

Hi if I want to use my own class while SessionsLive on ServerSide is there a possibility?
My Class like this:

type
  TMyClass = class
    FSQL: string;
    FError: string;
    FID: string;
    FQuery: TMyQuery;
    FConnection: TMyConnection;
  public
    function WriteToDB: string;
  end;



procedure Tdm.serverModuleSessionOpen(Sender: TRtcConnection);
begin
  with TRtcDataServer(Sender) do
  begin
    Session.asLinkedObject['myClass'] := TMyClass.Create;
    //Session.asOID['myClass'] := GetRtcObjectManager.FindOID(TMyClass.Create);
  end;
end;

procedure TdmSvrMain.smMainSessionClose(Sender: TRtcConnection);
var
  myClass: TMyClass;
begin
  with TRtcDataServer(Sender) do
  begin
    myClass := (Session.asLinkedObject['myClass'] AS TMyClass);
    //myClass := (GetRtcObjectManager.FindObject(Session.asOID['myClass']) AS TMyClass);

    FreeAndNil(myClass);
  end;
end;

 
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: January 08, 2013, 06:04:43 PM »

You can use Session.Obj[] and Session.Child[] (inherited from the TRtcInfo class) to store custom objects inside a Session. You will need to remove your Objects from the Session by setting the same properties to NIL and free the objects when they are no longer needed (for example, when the Session is closing). Objects assigned to "Session.Obj[]" and "Session.Child[]" will NOT be destroyed automatically by the Session, but the Session will be checked if these objects inherit from the "TRtcObject" class, in which case the "Kill" method will be called on the Object when it has to be destroyed (Session clean-up after close).

For example ...

type
  TMyClass = class
    FSQL: string;
    FError: string;
    FID: string;
    FQuery: TMyQuery;
    FConnection: TMyConnection;
  public
    function WriteToDB: string;
  end;

procedure Tdm.serverModuleSessionOpen(Sender: TRtcConnection);
var
  myObj: TMyClass;
begin
  with TRtcDataServer(Sender) do
  begin
    myObj := TMyClass.Create;
    Session.Obj['myClass'] := myObj;
  end;
end;

procedure TdmSvrMain.smMainSessionClose(Sender: TRtcConnection);
var
  myObj: TMyClass;
begin
  with TRtcDataServer(Sender) do
  begin
   if Session.Obj['myClass'] is TMyClass then
     begin
     myObj := TMyClass(Session.Obj['myClass'])
     // use your object

     Session.Obj['myClass']:=nil; // remove your object from the Session
     FreeAndNil(myObj); // destroy your object
    end;
  end;
end;

Best Regards,
Danijel Tkalcec
Logged
hannesgw
RTC Expired
*
Posts: 11


« Reply #2 on: January 08, 2013, 07:47:02 PM »

Thanks, Danijel.  Grin
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 16 queries.