TsgcWSPClient_MQTTEvents › OnMQTTConnect

OnMQTTConnect Event

Fires after CONNACK is received; reports the Session flag, ReasonCode and MQTT 5 ConnectProperties.

Syntax

property OnMQTTConnect: TsgcWSMQTTConnectEvent;
// TsgcWSMQTTConnectEvent = procedure(Connection: TsgcWSConnection; const Session: Boolean; const ReasonCode: Integer; const ReasonName: String; const ConnectProperties: TsgcWSMQTTCONNACKProperties) of object

Default Value

Remarks

Raised when the broker acknowledges the CONNECT with a CONNACK. This is the first moment at which the MQTT session is fully usable — it is safe to call Subscribe / Publish from inside this handler. Parameters:

Example

procedure TForm1.MQTTConnect(Connection: TsgcWSConnection;
  const Session: Boolean; const ReasonCode: Integer;
  const ReasonName: String;
  const ConnectProperties: TsgcWSMQTTCONNACKProperties);
begin
  if ReasonCode <> 0 then
  begin
    Memo1.Lines.Add('Connect rejected: ' + ReasonName);
    Exit;
  end;

  Memo1.Lines.Add(Format('Connected. Session=%s MaxQoS=%d',
    [BoolToStr(Session, True), ConnectProperties.MaximumQoS]));

  // now it is safe to subscribe and publish
  MQTT.Subscribe('sensors/#', mtqsAtLeastOnce);
end;

Back to Events