shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

Version 6.0.0 has a big bug on connect to the broker #161

Closed Marcus-Ospicon closed 4 years ago

Marcus-Ospicon commented 4 years ago

I have just detected the problem when I try to connect to my mosquitto broker. It took a long waste time from me to debug. I have to change to previously version 5.6.3 and it worked fine.

`Future mqttSetup(String ip, int port, String username, String password) async { client = mqtt.MqttClient(ip, ''); client.port = port; client.logging(on: false); client.keepAlivePeriod = 30; client.onConnected = _onConnect; client.onDisconnected = _onDisconnect; final mqtt.MqttConnectMessage connMess = mqtt.MqttConnectMessage() .withClientIdentifier("Mobile android 1") .startClean() .keepAliveFor(30); client.connectionMessage = connMess; try { print("Start connecting to mqtt broker..."); await client.connect(username, password); } catch (e) { print(e); client.disconnect(); }

if (client.connectionStatus.state == mqtt.MqttConnectionState.connected) {
  setState(() {
    connectionState = client.connectionStatus.state;
    print("mqtt connect successful");
  });
} else {
  client.disconnect();
}

}

_onConnect() { print("mqtt connected"); }

_onDisconnect(){ print("mqtt disconnected"); }`

The result when use 5.6.3 version I/flutter (19396): Start connecting to mqtt broker... I/flutter (19396): mqtt disconnected I/flutter (19396): mqtt connected I/flutter (19396): mqtt connect successful

And the 6.0.0 I/flutter (19396): Start connecting to mqtt broker... I/flutter (19396): NoSuchMethodError: The setter 'onDisconnected=' was called on null. I/flutter (19396): Receiver: null I/flutter (19396): Tried calling: onDisconnected=Closure: () => void from Function 'internalDisconnect':. I/flutter (19396): mqtt disconnected I/flutter (19396): mqtt disconnected

apiep commented 4 years ago

That because MqttClass are act like an interface or abstract class now. You need to use either MqttServerClient or MqttBrowserClient for version 6