TsgcWSPClient_MQTT | Client MQTT Sessions

Clean Start

OnMQTTBeforeConnect event, there is a parameter called aCleanSession. If the value of this parameter is True, means that client want start a new session, so if server has any session stored, it must discard it. So, when OnMQTTConnect event is fired, aSession parameter will be false. If the value of this parameter is False and there is a session associated to this client identifier, the server must resume communications with the client on state with the existing session.

 

So, if client has an unexpected disconnection, and you want to recover the session where was disconnected, in OnMQTTBeforeConnect set aCleanSession = True and aClientIdentifier = Client ID of Session.

 

Session

Once successful connection, check OnMQTTConnect event, the value of Session parameter.

 

  Session = true, means session has been resumed.

  Session = false, means it's a new session.


 
procedure TfrmWebSocketClient.MQTTMQTTBeforeConnect(Connection: TsgcWSConnection; 
  var aCleanSession: Boolean; var aClientIdentifier: string);
begin
  aCleanSession := false;
  aClientIdentifier := 'previous client id';
end;
 
procedure OnMQTTConnect(Connection: TsgcWSConnection; const Session: Boolean; 
  const ReasonCode: Integer; const ReasonName: string; const ConnectProperties: TsgcWSMQTTCONNACKProperties);
begin
  if Session then
    WriteLn('Session resumed')
  else
    WriteLn('New Session');
end;