connect method

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

Connect

Implementation

@override
Future<MqttClientConnectionStatus?> connect(String server, int port) {
  final completer = Completer<MqttClientConnectionStatus?>();
  MqttLogger.log('MqttSecureConnection::connect - entered');
  try {
    SecureSocket.connect(server, port,
            onBadCertificate: onBadCertificate, context: context)
        .then((socket) {
      MqttLogger.log('MqttSecureConnection::connect - securing socket');
      // Socket options
      final applied = _applySocketOptions(socket, socketOptions);
      if (applied) {
        MqttLogger.log(
            'MqttSecureConnection::connect - socket options applied');
      }
      client = socket;
      readWrapper = ReadWrapper();
      messageStream = MqttByteBuffer(typed.Uint8Buffer());
      MqttLogger.log('MqttSecureConnection::connect - start listening');
      _startListening();
      completer.complete();
    }).catchError((e) {
      onError(e);
      completer.completeError(e);
    });
  } on SocketException catch (e) {
    final message =
        'MqttSecureConnection::connect - 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::connect - 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::TLS exception raised on secure '
        'connection. Error is ${e.toString()}';
    throw NoConnectionException(message);
  }
  return completer.future;
}