RTC Forums

Subscription => Support => Topic started by: hannesgw on January 08, 2013, 05:52:29 PM



Title: TObject & Sessions
Post by: hannesgw 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;

 


Title: Re: TObject & Sessions
Post by: D.Tkalcec (RTC) 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


Title: Re: TObject & Sessions
Post by: hannesgw on January 08, 2013, 07:47:02 PM
Thanks, Danijel.  ;D