reauthenticate method

Future<MqttAuthenticateMessage> reauthenticate(
  1. MqttAuthenticateMessage msg, {
  2. int waitTimeInSeconds = MqttConstants.defaultReauthenticateTimeout,
})

Re-authenticate.

Sends the supplied authentication message and waits for the a response from the broker. Use this if you wish to re-authenticate without listening for authenticate messages. This method will wait a default 30 seconds unless another timeout value is specified.

If the re-authenticate times out an authenticate message is returned with the timeout indicator set.

Implementation

Future<MqttAuthenticateMessage> reauthenticate(
  MqttAuthenticateMessage msg, {
  int waitTimeInSeconds = MqttConstants.defaultReauthenticateTimeout,
}) {
  final completer = Completer<MqttAuthenticateMessage>();
  send(msg);
  MqttLogger.log(
    'MqttAuthenticationManager::reauthenticate - started, timeout is $waitTimeInSeconds',
  );
  final timeoutMsg = MqttAuthenticateMessage();
  timeoutMsg.timeout = true;
  late dynamic subscription;
  subscription = _authenticated.stream
      .timeout(
        Duration(seconds: waitTimeInSeconds),
        onTimeout: (_) {
          completer.complete(timeoutMsg);
        },
      )
      .listen((final rxMessage) {
        subscription.cancel();
        completer.complete(rxMessage);
      });
  return completer.future;
}