Skip to content

Commit

Permalink
Issue559 (#560)
Browse files Browse the repository at this point in the history
* Issue 559

* Issue 559
  • Loading branch information
shamblett authored Aug 12, 2024
1 parent c50e39e commit 96c5135
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/src/mqtt_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,14 @@ class MqttClient {
? connectionHandler!.connectionStatus
: _connectionStatus;

/// The connection message to use to override the default
MqttConnectMessage? connectionMessage;
/// The connection message to use to override the default.
MqttConnectMessage? _connectionMessage;
MqttConnectMessage? get connectionMessage => _connectionMessage;
set connectionMessage(MqttConnectMessage? connMessage) {
_connectionMessage = connMessage;
_connectionMessage?.variableHeader?.protocolVersion = Protocol.version;
_connectionMessage?.variableHeader?.protocolName = Protocol.name;
}

/// Client disconnect callback, called on unsolicited disconnect.
/// This will not be called even if set if [autoReconnect} is set,instead
Expand Down Expand Up @@ -575,11 +581,15 @@ class MqttClient {
void setProtocolV31() {
Protocol.version = MqttClientConstants.mqttV31ProtocolVersion;
Protocol.name = MqttClientConstants.mqttV31ProtocolName;
connectionMessage?.withProtocolVersion(Protocol.version);
connectionMessage?.withProtocolName(Protocol.name);
}

/// Set the protocol version to V3.1.1
void setProtocolV311() {
Protocol.version = MqttClientConstants.mqttV311ProtocolVersion;
Protocol.name = MqttClientConstants.mqttV311ProtocolName;
connectionMessage?.withProtocolVersion(Protocol.version);
connectionMessage?.withProtocolName(Protocol.name);
}
}
29 changes: 29 additions & 0 deletions test/mqtt_client_base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,35 @@ void main() {
client.setProtocolV311();
expect(Protocol.version, MqttClientConstants.mqttV311ProtocolVersion);
expect(Protocol.name, MqttClientConstants.mqttV311ProtocolName);
Protocol.version = MqttClientConstants.mqttV31ProtocolVersion;
Protocol.name = MqttClientConstants.mqttV31ProtocolName;
});
test('Protocol set with connect message', () {
Protocol.version = MqttClientConstants.mqttV31ProtocolVersion;
Protocol.name = MqttClientConstants.mqttV31ProtocolName;
final client = MqttClient('localhost', 'abcd');
expect(Protocol.version, MqttClientConstants.mqttV31ProtocolVersion);
expect(Protocol.name, MqttClientConstants.mqttV31ProtocolName);
final connectionMessage = MqttConnectMessage();
client.connectionMessage = connectionMessage;
client.setProtocolV311();
expect(Protocol.version, MqttClientConstants.mqttV311ProtocolVersion);
expect(Protocol.name, MqttClientConstants.mqttV311ProtocolName);
expect(client.connectionMessage?.variableHeader?.protocolVersion,
MqttClientConstants.mqttV311ProtocolVersion);
expect(client.connectionMessage?.variableHeader?.protocolName,
MqttClientConstants.mqttV311ProtocolName);
});
test('Protocol set on connect message', () {
Protocol.version = MqttClientConstants.mqttV31ProtocolVersion;
Protocol.name = MqttClientConstants.mqttV31ProtocolName;
expect(Protocol.version, MqttClientConstants.mqttV31ProtocolVersion);
expect(Protocol.name, MqttClientConstants.mqttV31ProtocolName);
MqttConnectMessage()
..withProtocolName(MqttClientConstants.mqttV311ProtocolName)
..withProtocolVersion(MqttClientConstants.mqttV311ProtocolVersion);
expect(Protocol.version, MqttClientConstants.mqttV311ProtocolVersion);
expect(Protocol.name, MqttClientConstants.mqttV311ProtocolName);
});
test('Byte Buffer', () {
final uBuff = typed.Uint8Buffer(10);
Expand Down

0 comments on commit 96c5135

Please sign in to comment.