unigui的session【1】

目前是1394。

明白session如何使用管理,看demo Session List和SessionTimeout

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIRegClasses, uniGUIForm, uniGUIBaseClasses, uniButton,
  DB, DBClient, uniBasicGrid, uniDBGrid;

type
  TUniSessionInfoRec = record
    ASessionId : string;
    AIP : string;
    AMyVar : string;
    ALastTime: TDateTime;
  end;

  TMainForm = class(TUniForm)
    UniButton1: TUniButton;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ClientDataSet1SessionId: TStringField;
    ClientDataSet1IP: TStringField;
    ClientDataSet1LastTime: TDateTimeField;
    UniDBGrid1: TUniDBGrid;
    ClientDataSet1MyVar: TStringField;
    procedure UniButton1Click(Sender: TObject);
    procedure UniFormCreate(Sender: TObject);

    { Private declarations }
  public
    { Public declarations }
    ASessionList : array of TUniSessionInfoRec;
  end;

function MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  uniGUIVars, MainModule, uniGUIApplication, uniGUISessionManager,
  ServerModule;

function MainForm: TMainForm;
begin
  Result := TMainForm(UniMainModule.GetFormInstance(TMainForm));
end;

procedure TMainForm.UniButton1Click(Sender: TObject);
var
  S : TUniGUISessions;
  U : TUniGUISession;
  I : Integer;
begin
  S := UniServerModule.SessionManager.Sessions;
  S.Lock; // Using Lock on session manager should be used with extreme care.
          // We should keep the "Lock duration"  as short as we can. Session Manager stops working until lock is released.
          // Excessive use of lock/unlock or keeping the lock active for a long period will make uniGUI server slow/unresponsive.
  try
    SetLength(ASessionList, S.SessionList.Count);

    // Here we do a fast copy of list to another array for later use.
    for I := 0 to S.SessionList.Count - 1  do
    begin
      U := S.SessionList[I];
      ASessionList[I].ASessionId := U.SessionId;
      ASessionList[I].AIP := U.RemoteIP;
      ASessionList[I].ALastTime := U.LastTimeStamp;
      if U.UniMainModule <> nil then // Check mainModule availability. Some sessions may not have a MainModule instance
        ASessionList[I].AMyVar := (U.UniMainModule as TUniMainModule).FMyStringVar; // Access custom MainModule props
    end;
  finally
    S.Unlock; // ... and finally don't forget to release lock!
  end;

  ClientDataSet1.EmptyDataSet;
  for I := Low(ASessionList) to High(ASessionList) do
  begin
    ClientDataSet1.Append;
    ClientDataSet1.FieldByName('sessionid').AsString := ASessionList[I].ASessionId;
    ClientDataSet1.FieldByName('ip').AsString := ASessionList[I].AIP;
    ClientDataSet1.FieldByName('lasttime').AsDateTime := ASessionList[I].ALastTime;
    ClientDataSet1.FieldByName('myvar').AsString := ASessionList[I].AMyVar;
    ClientDataSet1.Post;
  end;

end;

procedure TMainForm.UniFormCreate(Sender: TObject);
begin
  UniMainModule.FMyStringVar := TimeToStr(Now);
end;

initialization
  RegisterAppFormClass(TMainForm);

end.
View Code

注意代码中的注释

 

转载于:https://www.cnblogs.com/usegear/p/8978502.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值