OnMQTTBeforeConnect 이벤트에는 aCleanSession이라는 매개변수가 있습니다. 이 매개변수의 값이 True이면 클라이언트가 새 세션을 시작하려고 함을 의미하므로, 서버에 저장된 세션이 있으면 이를 폐기해야 합니다. 따라서 OnMQTTConnect 이벤트가 발생하면 aSession 매개변수는 false가 됩니다. 이 매개변수의 값이 False이고 이 클라이언트 식별자와 연결된 세션이 있으면 서버는 기존 세션이 있는 상태에서 클라이언트와의 통신을 재개해야 합니다.
따라서 클라이언트에 예기치 않은 연결 해제가 발생했고, 연결이 끊긴 위치에서 세션을 복구하려는 경우, OnMQTTBeforeConnect에서 aCleanSession = True와 aClientIdentifier = Client ID of Session을 설정하십시오.
성공적인 연결이 이루어지면 OnMQTTConnect 이벤트에서 Session 매개변수의 값을 확인하십시오.
Session = true는 세션이 재개되었음을 의미합니다.
Session = false는 새 세션임을 의미합니다.
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;