下面显示的是英文原文。
Server
The server reports host and port with MQTT 5 and the session binding: client id, clean start, keep-alive, expiry, last will, never credentials.
A broker registered with with_broker_labeled is a server of the document, under its label.
Its host is the host and port of the broker's URL.
A URL without a port reports the protocol's default: 1883 for mqtt://, 8883 for mqtts://.
The protocol is mqtt and the protocolVersion is 5, the only version the crate speaks.
The server binding describes the session the client opens.
clientId is the client id of MqttBroker::new, and maximumPacketSize is the packet limit, 1 MiB by default.
cleanSession, keepAlive and sessionExpiryInterval appear when the broker sets clean_start, keep_alive and session_expiry.
lastWill reports the last will's topic, quality of service and retain flag.
The will's payload is the content of a message, so it stays out.
The user name and password of credentials stay out too.
Example
The broker logs in, keeps its session for an hour and leaves a last will.
Its server in the document is mqtt.internal:1883, with the session below and no password.
{
"bindingVersion": "0.2.0",
"clientId": "telemetry-svc",
"cleanSession": false,
"keepAlive": 30,
"sessionExpiryInterval": 3600,
"maximumPacketSize": 1048576,
"lastWill": { "topic": "devices/svc/status", "qos": 1, "retain": true }
}
- Macros
- Manual
/// The production server, with a persistent session and a last will.
fn broker(password: &str) -> MqttBroker {
MqttBroker::new("mqtt://mqtt.internal:1883", "telemetry-svc")
.credentials("telemetry-svc", password)
.keep_alive(Duration::from_secs(30))
.clean_start(false)
.session_expiry(Duration::from_hours(1))
.last_will(
"devices/svc/status",
b"offline".to_vec(),
Qos::AtLeastOnce,
true,
)
}
/// The production server, with a persistent session and a last will.
fn broker(password: &str) -> MqttBroker {
MqttBroker::new("mqtt://mqtt.internal:1883", "telemetry-svc")
.credentials("telemetry-svc", password)
.keep_alive(Duration::from_secs(30))
.clean_start(false)
.session_expiry(Duration::from_hours(1))
.last_will(
"devices/svc/status",
b"offline".to_vec(),
Qos::AtLeastOnce,
true,
)
}