connect method
Performs a connect to the message broker with an optional username and password for the purposes of authentication. If a username and password are supplied these will override any previously set in a supplied connection message so if you supply your own connection message and use the authenticateAs method to set these parameters do not set them again here.
Implementation
@override
Future<MqttClientConnectionStatus?> connect(
[String? username, String? password]) async {
instantiationCorrect = true;
clientEventBus = events.EventBus();
clientEventBus
?.on<DisconnectOnNoPingResponse>()
.listen(disconnectOnNoPingResponse);
clientEventBus
?.on<DisconnectOnNoMessageSent>()
.listen(disconnectOnNoMessageSent);
final connectionHandler = SynchronousMqttServerConnectionHandler(
clientEventBus,
maxConnectionAttempts: maxConnectionAttempts,
reconnectTimePeriod: connectTimeoutPeriod,
socketOptions: socketOptions);
if (useWebSocket) {
connectionHandler.secure = false;
connectionHandler.useWebSocket = true;
connectionHandler.useAlternateWebSocketImplementation =
useAlternateWebSocketImplementation;
if (connectionHandler.useAlternateWebSocketImplementation) {
connectionHandler.securityContext = securityContext;
}
if (websocketProtocolString != null) {
connectionHandler.websocketProtocols = websocketProtocolString;
}
}
if (secure) {
connectionHandler.secure = true;
connectionHandler.useWebSocket = false;
connectionHandler.useAlternateWebSocketImplementation = false;
connectionHandler.securityContext = securityContext;
connectionHandler.onBadCertificate =
onBadCertificate as bool Function(Object certificate)?;
}
this.connectionHandler = connectionHandler;
return await super.connect(username, password);
}