服务器发送的消息在 OnMQTTPublish 事件中接收。该事件包含以下参数:
Topic:与此消息关联的主题名称。
Text: 消息的文本内容。
PublishProperties:如果是 MQTT 5.0,这些是已发布消息的属性。
procedure OnMQTTPublish(Connection: TsgcWSConnection; aTopic, aText: string;
PublishProperties: TsgcWSMQTTPublishProperties);
begin
WriteLn('Topic: ' + aTopic + '. Message: ' + aText);
end;
OnMQTTPublishEx 事件通过 TsgcWSMQTTPublishData 对象以多种格式提供已发布的消息有效负载。此事件具有以下参数:
Topic:与此消息关联的主题名称。
Data:包含已发布消息的有效载荷。它具有以下属性:
Value:有效载荷(字符串形式)。
Bytes: 以 TBytes 形式表示的原始载荷。
Stream: 以 TMemoryStream 形式表示的原始载荷。
PublishProperties:如果是 MQTT 5.0,这些是已发布消息的属性。
procedure OnMQTTPublishEx(Connection: TsgcWSConnection; aTopic: string;
aData: TsgcWSMQTTPublishData; PublishProperties: TsgcWSMQTTPublishProperties);
begin
// read as string
WriteLn('Topic: ' + aTopic + '. Message: ' + aData.Value);
// read as bytes
WriteLn('Bytes Length: ' + IntToStr(Length(aData.Bytes)));
// read as stream
WriteLn('Stream Size: ' + IntToStr(aData.Stream.Size));
end;