shamblett / mqtt_client

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

Lost connection #228

Closed PietroMB closed 3 years ago

PietroMB commented 3 years ago

Hi, I've an issues with this code, I use MQTT Explorer with the same credential and same port and it works, someone can help me?

Future<int> main() async {
  final client = MqttServerClient('192.168.1.2', 'sma');
  client.logging(on: true);
  client.keepAlivePeriod = 20;
  client.onDisconnected = onDisconnected;
  client.onSubscribed = onSubscribed;
  final connMess = MqttConnectMessage()
      .authenticateAs("myUser", "some-pass")
      .withClientIdentifier('sma')
      .keepAliveFor(20) // Must agree with the keep alive set above or not set
      .startClean() // Non persistent session for testing
      .withWillQos(MqttQos.atMostOnce);
  print('EXAMPLE::Mosquitto client connecting....');
  client.connectionMessage = connMess;

  try {
    await client.connect("myUser", "some-pass"); <-- Here debug mode generate problem after first step
  } on Exception catch (e) {
    print('######EXAMPLE::client exception - $e');
    client.disconnect();
  }
...

I/flutter ( 9336): EXAMPLE::Mosquitto client connecting.... I/flutter ( 9336): 2020-10-18 21:23:22.960963 -- Authenticating with username '{user}' and password '{some-password}' I/flutter ( 9336): 2020-10-18 21:23:22.974640 -- MqttClient::checkCredentials - Password length (13) exceeds the max recommended in the MQTT spec. I/flutter ( 9336): 2020-10-18 21:23:23.002520 -- MqttConnectionHandlerBase::connect - server 192.168.1.2, port 1883 I/flutter ( 9336): 2020-10-18 21:23:23.008130 -- SynchronousMqttServerConnectionHandler::internalConnect entered I/flutter ( 9336): 2020-10-18 21:23:23.010853 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false I/flutter ( 9336): 2020-10-18 21:23:23.012748 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected I/flutter ( 9336): 2020-10-18 21:23:23.014902 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect I/flutter ( 9336): 2020-10-18 21:23:23.017623 -- MqttNormalConnection::connect - entered I/flutter ( 9336): 2020-10-18 21:23:47.347764 -- MqttServerConnection::_startListening I/flutter ( 9336): 2020-10-18 21:23:47.356990 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete I/flutter ( 9336): 2020-10-18 21:23:47.357186 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message I/flutter ( 9336): 2020-10-18 21:23:47.362570 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect I/flutter ( 9336): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0 I/flutter ( 9336): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=20 I/flutter ( 9336): MqttConnectPayload - client identifier is : sma I/flutter ( 9336): 2020-10-18 21:23:47.388845 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none

I/flutter ( 9336): 2020-10-18 21:23:48.037851 -- MqttConnection::_onData I/flutter ( 9336): 2020-10-18 21:23:48.049942 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck I/flutter ( 9336): Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2 I/flutter ( 9336): Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.notAuthorized} I/flutter ( 9336): 2020-10-18 21:23:48.053084 -- MqttServerConnection::_onData - message available event fired I/flutter ( 9336): 2020-10-18 21:23:48.057168 -- MqttConnectionHandlerBase::_connectAckProcessor I/flutter ( 9336): 2020-10-18 21:23:48.057447 -- MqttConnectionHandlerBase::_connectAckProcessor connection rejected I/flutter ( 9336): 2020-10-18 21:23:48.057697 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered I/flutter ( 9336): 2020-10-18 21:23:48.057801 -- MqttConnectionHandlerBase:: cancelling connect timer I/flutter ( 9336): 2020-10-18 21:23:48.058776 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code of notAuthorized and a disconnection origin of none

I/flutter ( 9336): 2020-10-18 21:23:48.098643 -- SynchronousMqttServerConnectionHandler::internalConnect failed

shamblett commented 3 years ago

Your failing to connect because the broker is sending you a not authorized reply in the conn ack message. Although the code sample above does this -

wait client.connect("myUser", "some-pass");

From the log you are actually sending -

I/flutter ( 9336): 2020-10-18 21:23:22.960963 -- Authenticating with username '{user}' and password '{some-password}'

i.e. the password is 'some-password', not 'some-pass' and the user name is 'user', not 'myUser'.

Note you don't have to pass auth parameters on both the connect call and the authenticateAs method on the connect message, one or the other will do.

PietroMB commented 3 years ago

Sorry, I've not made a good job hiding my credential uniformly, ofc in the connection strings I've putted the correct ones.

I've tryed this way

  final connMess = MqttConnectMessage()
      //.authenticateAs("myUser", "some-password")
      .withClientIdentifier('sma')
      .keepAliveFor(20) // Must agree with the keep alive set above or not set
      .startClean() // Non persistent session for testing
      .withWillQos(MqttQos.atMostOnce);
  print('EXAMPLE::Mosquitto client connecting....');
  client.connectionMessage = connMess;

  try {
    await client.connect("myUser", "some-password");
  } on Exception catch (e) {
    print('######EXAMPLE::client exception - $e');
    client.disconnect();
  }

and this way, but same errors


Future<int> main() async {
  final client = MqttServerClient('192.168.1.2', 'sma');
  client.logging(on: true);
  client.keepAlivePeriod = 20;
  client.onDisconnected = onDisconnected;
  client.onSubscribed = onSubscribed;
  final connMess = MqttConnectMessage()
      .authenticateAs("myUser", "some-password")
      .withClientIdentifier('sma')
      .keepAliveFor(20) // Must agree with the keep alive set above or not set
      .startClean() // Non persistent session for testing
      .withWillQos(MqttQos.atMostOnce);
  print('EXAMPLE::Mosquitto client connecting....');
  client.connectionMessage = connMess;

  try {
    await client.connect();
  } on Exception catch (e) {
    print('######EXAMPLE::client exception - $e');
    client.disconnect();
  }
shamblett commented 3 years ago

OK, you are getting this in your conn ack message -

I/flutter ( 9336): 2020-10-18 21:23:48.058776 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code of notAuthorized and a disconnection origin of none

So your broker is not authorizing you, you need to look at your broker logs to see why this is, the client can't help you much more here.

PietroMB commented 3 years ago

This is the server log

1603100496: New connection from 192.168.1.7 on port 1883. [WARN] Not found myUser on local database 1603100497: Socket error on client , disconnecting. 1603100497: New connection from 192.168.1.7 on port 1883. 1603100497: Socket error on client , disconnecting. 1603100497: New connection from 192.168.1.7 on port 1883. 1603100497: Socket error on client , disconnecting.

I don't know why the warning, I uses this username in all IoT devices and they works

shamblett commented 3 years ago

Well, it doesn't like something, the user name is correct I assume as 'myUser', there's no trailing or leading whitespace we can't see is there? What broker are you using BTW?

PietroMB commented 3 years ago

No whitespace in username, I use Mosquitto broker as docker bridged

PietroMB commented 3 years ago

Oh no, I'm really really sorry, closing " I accidentally pressed a key in the password string,(my password was generated by a password manager) and I didn't notice the extra character in the review. Now it works