initializeMQTTClient method
void
initializeMQTTClient({})
Implementation
void initializeMQTTClient({
bool logging = kDebugMode,
String? hostConfig,
int? portConfig,
bool? useWebSocket,
int? keepAlivePeriod,
bool? autoReconnect,
bool setProtocolV311 = true,
}) {
String host =
hostConfig ?? (appMqttUrl != null ? 'wss://$appMqttUrl/mqtt' : '');
int port = portConfig ?? appMqttPort ?? 80;
client = MqttServerClient(host, _identifier)
..autoReconnect = autoReconnect ?? true
..port = port
..keepAlivePeriod = keepAlivePeriod ?? 200
..useWebSocket = useWebSocket ?? true
..logging(on: logging)
/// Add the successful connection callback
..onConnected = onConnected
..onDisconnected = onDisconnected
..onAutoReconnected = onReConnected
..onSubscribed = onSubscribed;
if (setProtocolV311) {
client!.setProtocolV311();
}
if (useWebSocket ?? true) {
client!.websocketProtocols = MqttClientConstants.protocolsSingleDefault;
} else {
client!.secure = true;
}
final MqttConnectMessage connMess = MqttConnectMessage()
.withClientIdentifier(_identifier)
// .withWillTopic(
// 'willtopic') // If you set this you must set a will message
// .withWillMessage('My Will message')
// .startClean() // Non persistent session for testing
.withWillQos(MqttQos.atMostOnce);
if (_allowPrintLog) print('MQTT::Mosquitto client connecting....');
client!.connectionMessage = connMess;
}