Delphi 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.


――――――――――――――――

原文链接:https://blog.csdn.net/shuiying/article/details/9121505

相关阅读 >>

Delphi使用xmlhttp获取时间

Delphi 获取鼠标坐标大全方法

Delphi 让scrollbox响应鼠标的滚动消息

Delphi 10.3 处理csv tstreamreader 自带split用法

Delphi 2009 泛型容器单元(generics.collections)[3]: tstack<t>

Delphi 处理之文本文件

Delphi异常重启自身

Delphi insert 插入一个字符(串)

Delphi 获取windows任务栏的高度

Delphi firedac压缩和修复ms access数据库

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...