connectAuto method

  1. @override
Future<MqttClientConnectionStatus?> connectAuto(
  1. String server,
  2. int port
)
override

Connect Auto

Implementation

@override
Future<MqttClientConnectionStatus?> connectAuto(String server, int port) {
  final completer = Completer<MqttClientConnectionStatus?>();
  MqttLogger.log('MqttNormalConnection::connectAuto - entered');
  try {
    // Connect and save the socket.
    Socket.connect(server, port).then((socket) {
      // Socket options
      final applied = _applySocketOptions(socket, socketOptions);
      if (applied) {
        MqttLogger.log(
            'MqttNormalConnection::connectAuto - socket options applied');
      }
      client = socket;
      _startListening();
      completer.complete();
    }).catchError((e) {
      onError(e);
      completer.completeError(e);
    });
  } on SocketException catch (e) {
    final message =
        'MqttNormalConnection::connectAuto - The connection to the message broker '
        '{$server}:{$port} could not be made. Error is ${e.toString()}';
    completer.completeError(e);
    throw NoConnectionException(message);
  } on Exception catch (e) {
    completer.completeError(e);
    final message =
        'MqttNormalConnection::ConnectAuto - The connection to the message '
        'broker {$server}:{$port} could not be made.';
    throw NoConnectionException(message);
  }
  return completer.future;
}