connect method
Future<bool>
connect({
- required dynamic host,
- required String username,
- required String password,
- String? clientId,
- int? port,
override
Implementation
@override
Future<bool> connect(
{required host,
required String username,
required String password,
String? clientId,
int? port}) async {
String cid = clientId ?? getClientId()!;
_clientId = cid;
_client = MqttServerClient.withPort(host, cid, port ?? 1883);
// _client = MqttServerClient.withPort('broker.emqx.io', resource, 1883
_client!.logging(on: true);
_client!.onConnected = onConnected;
_client!.onDisconnected = onDisconnected;
_client!.onUnsubscribed = onUnsubscribed;
_client!.onSubscribed = onSubscribed;
_client!.onSubscribeFail = onSubscribeFail;
_client!.onAutoReconnect = onAutoReconnect;
_client!.pongCallback = pong;
_client!.keepAlivePeriod = 60;
_client!.autoReconnect = true;
final connMessage = MqttConnectMessage()
.authenticateAs(username, password)
.withWillTopic('lastwills')
.withWillMessage('Will message')
.withWillQos(MqttQos.atLeastOnce);
_client!.connectionMessage = connMessage;
try {
//_messagesController = StreamController.broadcast();
_broadcastConnectionState();
await _client!.connect();
_broadcastConnectionState();
_listenAndFilter();
if (_client!.connectionStatus!.state == MqttConnectionState.connected) {
_subscribeToArchivesTopics();
}
return true;
} catch (e) {
debugPrint(e.toString());
_client!.disconnect();
return false;
}
}