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');
  try {
    SecureSocket.connect(server, port,
            onBadCertificate: onBadCertificate, context: context)
        .then((SecureSocket socket) {
      MqttLogger.log('MqttSecureConnection::connect - securing socket');
      client = socket;
      readWrapper = ReadWrapper();
      messageStream = MqttByteBuffer(typed.Uint8Buffer());
      MqttLogger.log('MqttSecureConnection::connect - start listening');
      _startListening();
      completer.complete();
    }).catchError((dynamic e) {
      onError(e);
      completer.completeError(e);
    });
  } on SocketException catch (e) {
    final message =
        'MqttSecureConnection::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::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;
}