connect method

Future<MqttClientConnectionStatus> connect (
  1. [String username,
  2. String password]
)

Comon client connection method.

Implementation

Future<MqttClientConnectionStatus> connect(
    [String username, String password]) async {
  // Protect against an incorrect instantiation
  if (!instantiationCorrect) {
    throw IncorrectInstantiationException();
  }
  checkCredentials(username, password);
  // Set the authentication parameters in the connection
  // message if we have one.
  connectionMessage?.authenticateAs(username, password);

  // Do the connection
  if (websocketProtocolString != null) {
    connectionHandler.websocketProtocols = websocketProtocolString;
  }
  connectionHandler.onDisconnected = internalDisconnect;
  connectionHandler.onConnected = onConnected;
  connectionHandler.onAutoReconnect = onAutoReconnect;
  connectionHandler.onAutoReconnected = onAutoReconnected;

  publishingManager = PublishingManager(connectionHandler, clientEventBus);
  subscriptionsManager = SubscriptionsManager(
      connectionHandler, publishingManager, clientEventBus);
  subscriptionsManager.onSubscribed = onSubscribed;
  subscriptionsManager.onUnsubscribed = onUnsubscribed;
  subscriptionsManager.onSubscribeFail = onSubscribeFail;
  subscriptionsManager.resubscribeOnAutoReconnect =
      resubscribeOnAutoReconnect;
  keepAlive = MqttConnectionKeepAlive(connectionHandler, keepAlivePeriod);
  if (pongCallback != null) {
    keepAlive.pongCallback = pongCallback;
  }
  final connectMessage = getConnectMessage(username, password);
  return connectionHandler.connect(server, port, connectMessage);
}