TsgcWebSocketHTTPServerEvents › OnCreateSession

OnCreateSession Event

Fires when the HTTP server needs to create a new session so the application can supply a custom TIdHTTPSession instance.

Syntax

property OnCreateSession: TIdHTTPCreateSession;
// TIdHTTPCreateSession = procedure(ASender: TIdContext; var VHTTPSession: TIdHTTPSession) of object

Default Value

Remarks

OnCreateSession is called by the underlying Indy HTTP server when SessionState is True and a new session is about to be created, typically when a request arrives without a valid session cookie and AutoStartSession is True. If the handler leaves VHTTPSession as nil, the server creates the session itself.

This event is not the recommended way to use your own session class. When you return an instance, the server only adds it to the session list; it does not assign a session ID to it. A session created with a plain constructor therefore has an empty session ID and an empty session cookie, and it can never be found again on the next request. Use SessionClass instead, which creates your descendant and still generates the unique session ID, the cookie and the timeout handling for you.

Also, do not call SessionList.CreateUniqueSession inside this event. That method already adds the new session to the list, and the server adds it again when the handler returns, so the same session ends up twice in the list. See Sessions for the full picture. Use OnSessionStart to simply react when a session becomes active.

Example


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;

Back to Events