UniGui中如何监听Session的开始与结束

UNIGui中的UniServerModule模块有一个SessionManager(TUNIGuiSessionManger类)的属性,通过 TUNIGuiSessionManger类的相关属性可以来监听web会话的开始与结束。

如:

UniServerModule.SessionManager.Sessions.OnSessionStart := OnSessionStart;
UniServerModule.SessionManager.Sessions.OnSessionEnd := OnSessionEnd;

procedure TFrmMon.OnSessionStart(Sender: TIdHTTPSession);
var
  RemoteIp,SessionID:string;
begin
   RemoteIP:= TUniGUISession(Sender).UniApplication.RemoteAddress;
   SessionID:= TUniGUISession(Sender).SessionID;
   //根据ip和会话编号进一步处理。
end;

procedure TFrmMon.OnSessionEnd(Sender: TIdHTTPSession);
var
  RemoteIp,SessionID:string;
begin
   RemoteIP:= TUniGUISession(Sender).UniApplication.RemoteAddress;
   SessionID:= TUniGUISession(Sender).SessionID;
  //根据ip和会话编号进一步处理。   
end;

下面是一个完整的代码单元:

unit ServerModule;

interface

uses
  SysUtils, System.IOUtils,uniGUIServer, uniGUIMainModule, uniGUIApplication,UIdCustomHTTPServer,System.Classes;

type
  TUniServerModule = class(TUniGUIServerModule)
    procedure UniGUIServerModuleCreate(Sender: TObject);
  private
    { Private declarations }
  procedure OnSessionStart(Sender: TIdHTTPSession);
  procedure OnSessionEnd(Sender: TIdHTTPSession);
  protected
    procedure FirstInit; override;
  public
    { Public declarations }
  end;

function UniServerModule: TUniServerModule;

implementation

{$R *.dfm}

uses
  UniGUIVars, Main;

function UniServerModule: TUniServerModule;
begin
  Result:=TUniServerModule(UniGUIServerInstance);
end;

procedure TUniServerModule.FirstInit;
begin
  InitServerModule(Self);

end;

procedure TUniServerModule.OnSessionEnd(Sender: TIdHTTPSession);
var
  ls :TStringList;
begin
   //
   ls := TStringList.Create;

   if TFile.Exists(UniServerModule.StartPath+'sys.log') then
   begin
     ls.LoadFromFile(UniServerModule.StartPath+'sys.log');
   end ;
   ls.Add('go exit');
   ls.SaveToFile(UniServerModule.StartPath+'sys.log');
   ls.Free;
end;

procedure TUniServerModule.OnSessionStart(Sender: TIdHTTPSession);
var
  ls :TStringList;
begin
   //
   ls := TStringList.Create;

   if TFile.Exists(UniServerModule.StartPath+'sys.log') then
   begin
     ls.LoadFromFile(UniServerModule.StartPath+'sys.log');
   end   ;
   ls.Add('go enter');
   ls.SaveToFile(UniServerModule.StartPath+'sys.log');
   ls.Free;
end;

procedure TUniServerModule.UniGUIServerModuleCreate(Sender: TObject);
begin
  UniServerModule.SessionManager.Sessions.OnSessionStart := OnSessionStart;
  UniServerModule.SessionManager.Sessions.OnSessionEnd := OnSessionEnd;
end;

initialization
  RegisterServerModuleClass(TUniServerModule);
end.


你可能感兴趣的:(UniGui中如何监听Session的开始与结束)