connect method
Implementation
@override
Future<bool> connect() async {
Log().debug('MQTT:: Connecting to $url on port ${client.port}');
/// Create a connection message to use or use the default one. The default one sets the
/// client identifier, any supplied username/password, the default keepalive interval(60s)
/// and clean session, an example of a specific one below.
final connMess = MqttConnectMessage();
connMess.withClientIdentifier(identifier);
connMess.startClean();
if (username != null) connMess.authenticateAs(username, password);
client.connectionMessage = connMess;
client.keepAlivePeriod = keepalive;
/// Connect the client, any errors here are communicated by raising of the appropriate exception. Note
/// in some circumstances the source will just disconnect us, see the spec about this, we however will
/// never send malformed messages.
try {
await client.connect();
} on Exception catch (e) {
Log().debug(
'MQTT:: Error Connecting to $url on port ${client.port}. Status is $e');
client.disconnect();
}
if (client.connectionStatus!.state == MqttConnectionState.connected) {
Log().debug('MQTT:: Client Connected');
} else {
Log().debug(
'MQTT:: Error Connecting to $url on port ${client.port}. Status is ${client.connectionStatus}');
client.disconnect();
return false;
}
return true;
}