Delphi-TMQTT2 icon indicating copy to clipboard operation
Delphi-TMQTT2 copied to clipboard

Setting username/password doesn't update flags for username/password

Open msedv opened this issue 7 years ago • 3 comments

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.

msedv avatar May 28 '17 13:05 msedv

Will look at this, thanks for reporting.

jamiei avatar Sep 28 '17 07:09 jamiei

Hello. Was the problem fixed?

cayque10 avatar Oct 20 '20 17:10 cayque10

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

daotdia avatar Oct 23 '23 08:10 daotdia