Delphi-TMQTT2
Delphi-TMQTT2 copied to clipboard
Setting username/password doesn't update flags for username/password
Your flag handling isn't working at all - only if the default values are "accidentally" correct.
Sample:
MQTT := TMQTT.Create(eIP.Text, StrToInt(ePort.Text)); MQTT.Username := eUsername.Text; MQTT.Password := ePassword.Text; MQTT.WillTopic := ''; // '/clients/will'; MQTT.WillMsg := ''; // 'Broker died!'; if MQTT.Connect then...
doesn't work: username and password are sent but not accepted since the corresponding connect flags are not set - and the flags for the will-messages not cleared.
Will look at this, thanks for reporting.
Hello. Was the problem fixed?
For solve the bug, only need to set the flags correctly. Change the comment code for below code:
if FisConnected then begin Msg := ConnectMessage; try Msg.Payload.Contents.Add(Self.FClientID); (Msg.VariableHeader as TMQTTConnectVarHeader).WillFlag := ord(hasWill); if hasWill then begin Msg.Payload.Contents.Add(Self.FWillTopic); Msg.Payload.Contents.Add(Self.FWillMsg); end;
if ((Length(FUsername) > 1) and (Length(FPassword) > 1)) then begin Msg.Payload.Contents.Add(FUsername); Msg.Payload.Contents.Add(FPassword); end; if WriteData(Msg.ToBytes) then Result := true else Result := false;
if FisConnected then
begin
Msg := ConnectMessage;
try
Msg.payload.Contents.Add(Self.FClientID);
if hasWill then
begin
(Msg.VariableHeader as TMQTTConnectVarHeader).WillFlag := 1;
Msg.payload.Contents.Add(Self.FWillTopic);
Msg.payload.Contents.Add(Self.FWillMsg);
end
else
begin
(Msg.VariableHeader as TMQTTConnectVarHeader).WillFlag := 0;
end;
// Aquí se verifican las credenciales y se activan los flags correspondientes.
if (Length(FUsername) > 0) then
begin
(Msg.VariableHeader as TMQTTConnectVarHeader).Username := 1;
Msg.payload.Contents.Add(FUsername);
end;
if (Length(FPassword) > 0) then
begin
(Msg.VariableHeader as TMQTTConnectVarHeader).Password := 1;
Msg.payload.Contents.Add(FPassword);
end;
if WriteData(Msg.ToBytes) then
Result := true
else
Result := false