connect method

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

Connect

Implementation

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