shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

Use will() cannot CONNECT #52

Closed feimenggo closed 1 year ago

feimenggo commented 1 year ago

I can't connect when I use will(). The broker is not responding to the connection request message (Missing Connection Acknowledgement?

shamblett commented 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]. 
feimenggo commented 1 year ago

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);
shamblett commented 1 year ago

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.

aheadintime commented 1 year ago

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!

shamblett commented 1 year ago

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.

feimenggo commented 1 year ago

Ok, thank you.

shamblett commented 1 year ago

Package updated, this will be in the next release

shamblett commented 1 year ago

Package re published at version 3.4.0