跳到主要内容
本页尚未翻译

下面显示的是英文原文。

Acknowledgement

An ack sends PUBACK at QoS 1 and PUBREC at QoS 2; QoS 0 has no acknowledgement.

A message settles when the handler returns, and the quality of service of the delivery decides what that sends. At QoS 1 and 2 the server keeps a delivery until it is acknowledged, and the client finishes the QoS 2 handshake on its own once the server releases the message. A QoS 0 delivery has no acknowledgement packet, so settling it answers AckError::Unsupported, which the runtime logs as a warning. drop acknowledges the delivery too, because an acknowledgement is the only final answer MQTT has, and the server never sends the message again. retry asks for a negative acknowledgement, which MQTT does not have, and retry_after is the outcome that tries again later.

The acknowledgement goes to the runner, which sends it; a delivery settled after the broker shut down answers AckError::Broker, carrying MqttError::NotConnected.

Example​

A device agent applies the commands it knows and drops the rest; both answers settle the delivery.

/// The subscription reads at `QoS` 1, so returning from the handler acknowledges the delivery with
/// a PUBACK. A command the device does not know is dropped: on MQTT that acknowledges it as well,
/// and the server never sends it again.
#[subscriber(MqttTopic::new("devices/dev42/commands").qos(Qos::AtLeastOnce))]
async fn apply(command: &Command) -> HandlerOutcome {
match command.action.as_str() {
"reboot" | "reset" => HandlerOutcome::ack(),
_ => HandlerOutcome::drop(),
}
}

#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| b.include(apply))
}

QoS 0​

The subscription reads telemetry at QoS 0. The handler settles each delivery as usual, and nothing is sent to the server.

/// Telemetry at `QoS` 0: the next reading replaces a lost one, so the subscription asks for no
/// acknowledgement. The outcome still settles the delivery in the service; the runtime has no
/// packet to send for it and logs that it is unsupported.
#[subscriber(MqttTopic::new("sensors/temp").qos(Qos::AtMostOnce))]
async fn store(reading: &Reading) -> HandlerOutcome {
if reading.celsius < -40 {
return HandlerOutcome::drop();
}
HandlerOutcome::ack()
}

#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| b.include(store))
}