HTTP 是一种无状态协议,客户端请求文件,服务器发送响应,连接随即关闭。会话允许您存储关于客户端的信息,例如可在客户端登录期间使用。会话可以在一段时间不活动后销毁,也可以在客户端注销后手动销毁。
TsgcWebSocketHTTPServer 中有一些属性用于在服务器组件中启用/禁用会话。最重要的属性有:
| 属性 | 描述 |
| SessionState | T这是必须启用的第一个属性,以便使用会话。如果不启用此属性,会话将无法工作 |
|
SessionTimeout |
此处必须设置大于零的值(以毫秒为单位),作为会话保持活跃的最长时间。 |
| AutoStartSession | 会话可以自动创建(AutoStartSession = true)或手动创建(AutoStartSession = false)。如果自动创建会话,服务器将使用 RemoteIP 作为唯一标识符检查是否有活跃的存储会话。 |
| SessionClass | 可选。服务器创建新会话时使用的类。将其设置为你自己的 TIdHTTPSession 后代,以在每个会话中存储你自己的数据。必须在服务器激活之前设置。 |
| SessionList | 保存活动会话的列表。读取它可按代码搜索、创建或移除会话。如果你需要完全控制会话的存储位置,也可以指定你自己的列表。必须在服务器激活之前指定。 |
TsgcWebSocketHTTPServer1.SessionState := True;
TsgcWebSocketHTTPServer1.SessionTimeout := 600000;
TsgcWebSocketHTTPServer1.AutoStartSession := False;
要创建新会话,必须创建一个唯一的新会话 ID。您可以使用任何值。示例:如果客户端正在验证身份,您可以使用 user + password + remoteip 作为会话 ID。
然后,搜索会话列表以检查其是否已存在。如果不存在,则创建一个新会话。
创建新会话时,将调用 OnSessionStart 事件;会话关闭时,将触发 OnSessionEnd 事件。
procedure OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
var
vID: String;
oSession: TIdHTTPSession;
begin
if ARequestInfo.Document = '/' then
AResponseInfo.ServeFile(AContext, 'yourpathhere\index.html')
else
begin
// check if user is valid
if not ((ARequestInfo.AuthUsername = 'user') and (ARequestInfo.AuthPassword = 'pass')) then
AResponseInfo.AuthRealm := 'Authenticate'
else
begin
// create a new session id with authentication data
vID := ARequestInfo.AuthUsername + '_' + ARequestInfo.AuthPassword + '_' + ARequestInfo.RemoteIP;
// search session
oSession := TsgcWebSocketHTTPServer1.SessionList.GetSession(vID, ARequestInfo.RemoteIP);
// create new session if not exists
if not Assigned(oSession) then
oSession := TsgcWebSocketHTTPServer1.SessionList.CreateSession(ARequestInfo.RemoteIP, vID);
AResponseInfo.ContentText := '<html><head></head><body>Authenticated</body></html>';
AResponseInfo.ResponseNo := 200;
end;
end;
end;
一旦会话存在,服务器就会将其附加到每个携带会话 cookie 的请求上。从 ARequestInfo.Session 中读取它;当请求没有会话时,该值为 nil。
procedure OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
begin
if Assigned(ARequestInfo.Session) then
begin
// Content is a TStrings you can use to store your own values
ARequestInfo.Session.Content.Values['visits'] :=
IntToStr(StrToIntDef(ARequestInfo.Session.Content.Values['visits'], 0) + 1);
AResponseInfo.ContentText := 'Session ' + ARequestInfo.Session.SessionID +
' visits: ' + ARequestInfo.Session.Content.Values['visits'];
end
else
AResponseInfo.ContentText := 'No session';
end;
如果你想在每个会话中保留自己的字段,而不是使用 Content 字符串列表,请创建 TIdHTTPSession 的派生类,并通过 SessionClass 属性告诉服务器使用它。请在服务器激活之前设置它。
服务器会继续处理其他一切:它生成唯一的会话 ID,发送会话 cookie,应用 SessionTimeout 并移除过期的会话。如果你想在会话创建时初始化自己的字段,请重写虚拟构造函数 CreateInitialized。
type
TMySession = class(TIdHTTPSession)
private
FUserName: String;
FLoginTime: TDateTime;
public
constructor CreateInitialized(AOwner: TIdHTTPCustomSessionList;
const SessionID, RemoteIP: string); override;
property UserName: String read FUserName write FUserName;
property LoginTime: TDateTime read FLoginTime write FLoginTime;
end;
constructor TMySession.CreateInitialized(AOwner: TIdHTTPCustomSessionList;
const SessionID, RemoteIP: string);
begin
inherited CreateInitialized(AOwner, SessionID, RemoteIP);
FLoginTime := Now;
end;
// configure the server before it starts
TsgcWebSocketHTTPServer1.SessionState := True;
TsgcWebSocketHTTPServer1.SessionTimeout := 600000;
TsgcWebSocketHTTPServer1.SessionClass := TMySession;
TsgcWebSocketHTTPServer1.Active := True;
// and read it back in any request
procedure OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
begin
if ARequestInfo.Session is TMySession then
AResponseInfo.ContentText := TMySession(ARequestInfo.Session).UserName;
end;
如果你需要完全控制会话的存储方式,例如将它们保存在数据库中或在多个服务器之间共享,请在服务器激活之前,将你自己的列表分配给 SessionList 属性。从 TIdHTTPDefaultSessionList 继承,并重写虚方法 CreateSession。服务器会在内部从 CreateUniqueSession 调用它,因此仍会为你生成唯一的会话 ID。
当你分配自己的 SessionList 时,SessionClass 属性不再适用,因为由你的列表决定创建哪个类。
type
TMySessionList = class(TIdHTTPDefaultSessionList)
public
function CreateSession(const RemoteIP, SessionID: string)
: TIdHTTPSession; override;
end;
function TMySessionList.CreateSession(const RemoteIP, SessionID: string)
: TIdHTTPSession;
begin
Result := TMySession.CreateInitialized(Self, SessionID, RemoteIP);
SessionList.Add(Result);
end;
// assign it before the server starts
TsgcWebSocketHTTPServer1.SessionList := TMySessionList.Create(nil);
TsgcWebSocketHTTPServer1.Active := True;
OnCreateSession 事件允许你自行返回一个会话实例,但它不会为其分配会话 ID。如果你用普通构造函数创建会话,会话 ID 和会话 cookie 都为空,在下一次请求时将永远无法再找到该会话。请改用 SessionClass,它更简单,并会为你处理好这一切。
此外,不要在 OnCreateSession 内调用 SessionList.CreateUniqueSession。该方法已将新会话添加到列表,而当你的处理程序返回时服务器又会再添加一次,因此同一会话在列表中出现了两次。