connectAuto method
Connect Auto
Implementation
@override
Future<MqttClientConnectionStatus?> connectAuto(String server, int port) {
final completer = Completer<MqttClientConnectionStatus?>();
MqttLogger.log('MqttSecureConnection::connectAuto - entered');
try {
SecureSocket.connect(server, port,
onBadCertificate: onBadCertificate, context: context)
.then((socket) {
MqttLogger.log('MqttSecureConnection::connectAuto - securing socket');
// Socket options
final applied = _applySocketOptions(socket, socketOptions);
if (applied) {
MqttLogger.log(
'MqttSecureConnection::connectAuto - socket options applied');
}
client = socket;
MqttLogger.log('MqttSecureConnection::connectAuto - start listening');
_startListening();
completer.complete();
}).catchError((e) {
onError(e);
completer.completeError(e);
});
} on SocketException catch (e) {
final message =
'MqttSecureConnection::connectAuto - The connection to the message broker '
'{$server}:{$port} could not be made. Error is ${e.toString()}';
completer.completeError(e);
throw NoConnectionException(message);
} on HandshakeException catch (e) {
final message =
'MqttSecureConnection::connectAuto - Handshake exception to the message broker '
'{$server}:{$port}. Error is ${e.toString()}';
completer.completeError(e);
throw NoConnectionException(message);
} on TlsException catch (e) {
final message =
'MqttSecureConnection::connectAuto - TLS exception raised on secure '
'connection. Error is ${e.toString()}';
throw NoConnectionException(message);
}
return completer.future;
}