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('MqttNormalConnection::connect - 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::connect - socket options applied');
      }
      client = socket;
      readWrapper = ReadWrapper();
      messageStream = MqttByteBuffer(typed.Uint8Buffer());
      _startListening();
      completer.complete();
    }).catchError((e) {
      onError(e);
      completer.completeError(e);
    });
  } on SocketException catch (e) {
    final message =
        'MqttNormalConnection::connect - 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::Connect - The connection to the message '
        'broker {$server}:{$port} could not be made.';
    throw NoConnectionException(message);
  }
  return completer.future;
}