YOUSEFUL / DELPHI / COMPONENTS
TAtlasMqttMessageEvent
The type of TAtlasMqttClient.OnMessage: one delivered message.
Description
TAtlasMqttMessageEvent = procedure(Sender: TObject; const Topic: string; const Payload: TBytes;
QoS: TMqttQoS; Retained: Boolean) of object;Sender is the client. Topic is the topic the message was published to, never a filter. Payload holds the raw bytes; decode text with TEncoding.UTF8.GetString. QoS is the quality it was delivered at. Retained is True when the broker replays a retained message to a new subscription. The handler runs on the main thread, or on the client's thread when SynchronizeEvents is False.
This type has no members of its own beyond those listed in its description.
Examples
Parse a temperature
A handler that keeps the latest reading from any topic ending in /temperature.
uses
System.SysUtils, Atlas.MqttCodec, Atlas.MqttClient;
type
TTemperatureView = class
Celsius: Double;
procedure ClientMessage(Sender: TObject; const Topic: string; const Payload: TBytes;
QoS: TMqttQoS; Retained: Boolean);
end;
procedure TTemperatureView.ClientMessage(Sender: TObject; const Topic: string; const Payload: TBytes;
QoS: TMqttQoS; Retained: Boolean);
begin
if Topic.EndsWith('/temperature') then
Celsius := StrToFloat(TEncoding.UTF8.GetString(Payload), TFormatSettings.Invariant);
end;
procedure AttachView(Client: TAtlasMqttClient; View: TTemperatureView);
begin
Client.OnMessage := View.ClientMessage;
end;