TsgcWebSocketHTTPServer › 事件 › OnCreateSession
当 HTTP 服务器需要创建新会话时触发,以便应用程序提供自定义 TIdHTTPSession 实例。
property OnCreateSession: TIdHTTPCreateSession;
// TIdHTTPCreateSession = procedure(ASender: TIdContext; var VHTTPSession: TIdHTTPSession) of object
—
当 SessionState 为 True 且即将创建新会话时,底层 Indy HTTP 服务器会调用 OnCreateSession,通常发生在请求到达时不带有效会话 cookie 且 AutoStartSession 为 True 的情况下。如果处理程序将 VHTTPSession 保持为 nil,服务器会自行创建会话。
此事件不是使用你自己的会话类的推荐方式。 当你返回一个实例时,服务器只会将其添加到会话列表;它不会为其分配会话 ID。因此,用普通构造函数创建的会话具有空的会话 ID 和空的会话 cookie,在下一个请求中永远无法再次找到它。请改用 SessionClass,它会创建你的后代,同时仍为你生成唯一的会话 ID、cookie 和超时处理。
此外,不要在此事件内调用 SessionList.CreateUniqueSession。该方法已将新会话添加到列表,而当处理程序返回时服务器又会再添加一次,因此同一会话在列表中出现了两次。完整情形参见 Sessions。若只是想在会话变为活动时作出反应,请使用 OnSessionStart。
procedure OnCreateSession(ASender: TIdContext; var VHTTPSession: TIdHTTPSession);
begin
// leave VHTTPSession as nil to let the server create the session
Log('new HTTP session allocated');
end;