MqttMobile constructor

MqttMobile(
  1. String url,
  2. IMqttListener listener, {
  3. String? username,
  4. String? password,
})

Implementation

MqttMobile(this.url, this.listener, {this.username, this.password}) {
  Uri? uri = URI.parse(this.url);
  if (uri == null) return;
  var scheme = uri.scheme;
  var server = uri.host;
  var port = (uri.port == 443 || uri.port == 80) ? 1883 : uri.port;
  var url = server;

  /// Create Client
  client = MqttServerClient(url, identifier);

  /// Set logging on if needed, defaults to off
  client.logging(on: false);

  /// If you intend to use a keep alive value in your connect message that is not the default(60s)
  /// you must set it here
  client.keepAlivePeriod = keepalive;

  /// The ws port for Mosquitto is 8080, for wss it is 8081
  client.port = port;

  /// Add the unsolicited disconnection callback
  client.onDisconnected = _onDisconnected;

  /// Add the successful connection callback
  client.onConnected = _onConnected;

  client.useWebSocket = (scheme.toLowerCase().startsWith('ws'));

  /// Add a subscribed callback, there is also an unsubscribed callback if you need it.
  /// You can add these before connection or change them dynamically after connection if
  /// you wish. There is also an onSubscribeFail callback for failed subscriptions, these
  /// can fail either because you have tried to subscribe to an invalid topic or the source
  /// rejects the subscribe request.
  client.onSubscribed = _onSubscribed;
  client.onUnsubscribed = _onUnSubscribed;

  /// Set a ping received callback if needed, called whenever a ping response(pong) is received
  /// from the source.
  client.pongCallback = _onPong;
}