Closed feimenggo closed 1 year ago
You have to set up all the will properties if you set this flag, from the spec -
If the Will Flag is set to 1, the Will Properties, Will Topic, and Will Payload fields MUST be present in the Payload [MQTT-3.1.2-9].
I set up the will properties, still not working.
final message = MqttConnectMessage();
message.will();
message.withWillTopic('offline');
final builder = MqttPayloadBuilder();
builder.addUTF8String('{"message":"bye"}');
message.withWillPayload(builder.payload!);
message.withWillQos(MqttQos.exactlyOnce);
final properties = MqttWillProperties();
properties.willDelayInterval = 0;
properties.payloadFormatIndicator = true;
properties.messageExpiryInterval = 0;
properties.contentType = 'application/json';
properties.responseTopic = 'offline';
message.withWillProperties(properties);
OK, you need to check your broker logs to see why its still not happy with the connect message, the client can't help you any further here.
I set up the will properties, still not working.
final message = MqttConnectMessage(); message.will(); message.withWillTopic('offline'); final builder = MqttPayloadBuilder(); builder.addUTF8String('{"message":"bye"}'); message.withWillPayload(builder.payload!); message.withWillQos(MqttQos.exactlyOnce); final properties = MqttWillProperties(); properties.willDelayInterval = 0; properties.payloadFormatIndicator = true; properties.messageExpiryInterval = 0; properties.contentType = 'application/json'; properties.responseTopic = 'offline'; message.withWillProperties(properties);
The only problem with this code is that it doesn't encode Will Payload as "Binary Data"
From documentation there : https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901070
3.1.3.4 Will Payload
If the Will Flag is set to 1 the Will Payload is the next field in the Payload. The Will Payload defines the Application Message Payload that is to be published to the Will Topic as described in section 3.1.2.5. This field consists of Binary Data.
Binary Data is defined there https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901012
1.5.6 Binary Data
Binary Data is represented by a Two Byte Integer length which indicates the number of data bytes, followed by that number of bytes. Thus, the length of Binary Data is limited to the range of 0 to 65,535 Bytes.
try using
message.withWillPayload(MqttUtf8Encoding().toUtf8('{"message":"bye"}'));
instead of
final builder = MqttPayloadBuilder();
builder.addUTF8String('{"message":"bye"}');
message.withWillPayload(builder.payload!);
In the hope of being of help, greetings!
Yep, the payload builder is generic in nature, if you want a specific encoding of the payload or part of it then you need to do this yourself before adding it to the payload. That said adding a method to the payload builder specifically for adding will payloads wouldn't go amiss here, other people may be suffering from the same problem. I'll update the package.
Ok, thank you.
Package updated, this will be in the next release
Package re published at version 3.4.0
I can't connect when I use will(). The broker is not responding to the connection request message (Missing Connection Acknowledgement?