shamblett / mqtt_client

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

The broker is not responding to the connection request message correctly The return code is MqttConnectReturnCode.identifierRejected #356

Closed yh4922 closed 2 years ago

yh4922 commented 2 years ago
code sample ```dart import 'package:mqtt_client/mqtt_client.dart'; import 'package:mqtt_client/mqtt_server_client.dart'; void mqttInit(String server, String username, String password) async { // Append a timestamp to the ClientID to prevent duplicate connections String clientId = 'pigpad_'; clientId += DateTime.now().millisecondsSinceEpoch.toString(); print('server: $server'); print('username: $username'); print('password: $password'); print('clientId: $clientId'); print('**************************'); final client = MqttServerClient(server, clientId); client.logging(on: true); client.setProtocolV311(); client.secure = true; client.port = 8883; client.keepAlivePeriod = 20; client.onDisconnected = () { print('Disconnected'); }; client.onConnected = () { print('Connected'); }; client.onSubscribed = (String topic) { print('Subscribed: $topic'); }; client.pongCallback = () { print('pong'); }; client.onBadCertificate = (dynamic a) => true; final connMess = MqttConnectMessage() .withClientIdentifier(clientId) .withWillTopic('willtopic') .withWillMessage('My Will message') .startClean() .withWillQos(MqttQos.atLeastOnce); client.connectionMessage = connMess; try { await client.connect(username, password); print('Connection successful'); } on Exception catch (e) { print('Connection failed - $e'); client.disconnect(); } } ```
output ```bash flutter: server: iot-device.wens.com.cn flutter: username: 61c039b231b95a797fc5eb1d_62231e5eaf26d3b9f3491219 flutter: password: 08915b24f1b28bc915ec7d6e9531e8e0 flutter: clientId: pigpad_1646475545342 flutter: ************************** flutter: 4-2022-03-05 18:19:05.343679 -- Authenticating with username '{61c039b231b95a797fc5eb1d_62231e5eaf26d3b9f3491219}' and password '{08915b24f1b28bc915ec7d6e9531e8e0}' flutter: 4-2022-03-05 18:19:05.343679 -- MqttClient::checkCredentials - Username length (49) exceeds the max recommended in the MQTT spec. flutter: 4-2022-03-05 18:19:05.343679 -- MqttClient::checkCredentials - Password length (32) exceeds the max recommended in the MQTT spec. flutter: 4-2022-03-05 18:19:05.344679 -- MqttClient::connect - keep alive is enabled with a value of 20 seconds flutter: 4-2022-03-05 18:19:05.344679 -- MqttConnectionKeepAlive:: Initialised with a keep alive value of 20 seconds flutter: 4-2022-03-05 18:19:05.344679 -- MqttConnectionKeepAlive:: Disconnect on no ping response is disabled flutter: 4-2022-03-05 18:19:05.344679 -- MqttConnectionHandlerBase::connect - server iot-device.wens.com.cn, port 8883 flutter: 4-2022-03-05 18:19:05.344679 -- SynchronousMqttServerConnectionHandler::internalConnect entered flutter: 4-2022-03-05 18:19:05.344679 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false flutter: 4-2022-03-05 18:19:05.344679 -- SynchronousMqttServerConnectionHandler::internalConnect - secure selected flutter: 4-2022-03-05 18:19:05.344679 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect flutter: 4-2022-03-05 18:19:05.345677 -- MqttSecureConnection::connect - entered flutter: 4-2022-03-05 18:19:05.390766 -- MqttSecureConnection::connect - securing socket flutter: 4-2022-03-05 18:19:05.390766 -- MqttSecureConnection::connect - start listening flutter: 4-2022-03-05 18:19:05.390766 -- MqttServerConnection::_startListening flutter: 4-2022-03-05 18:19:05.391767 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete flutter: 4-2022-03-05 18:19:05.391767 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message flutter: 4-2022-03-05 18:19:05.391767 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0 Connect Variable Header: ProtocolName=MQTT, ProtocolVersion=4, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=true, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=20 MqttConnectPayload - client identifier is : pigpad_1646475545342 flutter: 4-2022-03-05 18:19:05.394766 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none flutter: 4-2022-03-05 18:19:05.467510 -- MqttConnection::_onData flutter: 4-2022-03-05 18:19:05.467510 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2 Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.identifierRejected} flutter: 4-2022-03-05 18:19:05.468511 -- MqttServerConnection::_onData - message available event fired flutter: 4-2022-03-05 18:19:05.469512 -- MqttConnectionHandlerBase::_connectAckProcessor flutter: 4-2022-03-05 18:19:05.469512 -- MqttConnectionHandlerBase::_connectAckProcessor connection rejected flutter: 4-2022-03-05 18:19:05.469512 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered flutter: 4-2022-03-05 18:19:05.470507 -- MqttConnectionHandlerBase:: cancelling connect timer flutter: 4-2022-03-05 18:19:05.470507 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code of identifierRejected and a disconnection origin of none flutter: 4-2022-03-05 18:19:05.470507 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 1, auto reconnect in progress false flutter: 4-2022-03-05 18:19:05.470507 -- SynchronousMqttServerConnectionHandler::internalConnect - secure selected flutter: 4-2022-03-05 18:19:05.470507 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect flutter: 4-2022-03-05 18:19:05.470507 -- MqttSecureConnection::connect - entered flutter: 4-2022-03-05 18:19:05.473522 -- MqttConnectionBase::_onDone - calling disconnected callback flutter: 4-2022-03-05 18:19:05.518434 -- MqttSecureConnection::connect - securing socket flutter: 4-2022-03-05 18:19:05.518434 -- MqttSecureConnection::connect - start listening flutter: 4-2022-03-05 18:19:05.519434 -- MqttServerConnection::_startListening flutter: 4-2022-03-05 18:19:05.519434 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete flutter: 4-2022-03-05 18:19:05.519434 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message flutter: 4-2022-03-05 18:19:05.519434 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 145 Connect Variable Header: ProtocolName=MQTT, ProtocolVersion=4, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=true, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=20 MqttConnectPayload - client identifier is : pigpad_1646475545342 flutter: 4-2022-03-05 18:19:05.522435 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none flutter: 4-2022-03-05 18:19:05.583432 -- MqttConnection::_onData flutter: 4-2022-03-05 18:19:05.583432 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2 Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.identifierRejected} flutter: 4-2022-03-05 18:19:05.585438 -- MqttServerConnection::_onData - message available event fired flutter: 4-2022-03-05 18:19:05.586433 -- MqttConnectionHandlerBase::_connectAckProcessor flutter: 4-2022-03-05 18:19:05.586433 -- MqttConnectionHandlerBase::_connectAckProcessor connection rejected flutter: 4-2022-03-05 18:19:05.586433 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered flutter: 4-2022-03-05 18:19:05.586433 -- MqttConnectionHandlerBase:: cancelling connect timer flutter: 4-2022-03-05 18:19:05.586433 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code of identifierRejected and a disconnection origin of none flutter: 4-2022-03-05 18:19:05.586433 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false flutter: 4-2022-03-05 18:19:05.586433 -- SynchronousMqttServerConnectionHandler::internalConnect - secure selected flutter: 4-2022-03-05 18:19:05.586433 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect flutter: 4-2022-03-05 18:19:05.586433 -- MqttSecureConnection::connect - entered flutter: 4-2022-03-05 18:19:05.592434 -- MqttConnectionBase::_onDone - calling disconnected callback flutter: 4-2022-03-05 18:19:05.634444 -- MqttSecureConnection::connect - securing socket flutter: 4-2022-03-05 18:19:05.634444 -- MqttSecureConnection::connect - start listening flutter: 4-2022-03-05 18:19:05.634444 -- MqttServerConnection::_startListening flutter: 4-2022-03-05 18:19:05.634444 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete flutter: 4-2022-03-05 18:19:05.634444 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message flutter: 4-2022-03-05 18:19:05.634444 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 145 Connect Variable Header: ProtocolName=MQTT, ProtocolVersion=4, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=true, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=20 MqttConnectPayload - client identifier is : pigpad_1646475545342 flutter: 4-2022-03-05 18:19:05.635449 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none flutter: 4-2022-03-05 18:19:05.701446 -- MqttConnection::_onData flutter: 4-2022-03-05 18:19:05.701446 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2 Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.identifierRejected} flutter: 4-2022-03-05 18:19:05.701446 -- MqttServerConnection::_onData - message available event fired flutter: 4-2022-03-05 18:19:05.701446 -- MqttConnectionHandlerBase::_connectAckProcessor flutter: 4-2022-03-05 18:19:05.701446 -- MqttConnectionHandlerBase::_connectAckProcessor connection rejected flutter: 4-2022-03-05 18:19:05.701446 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered flutter: 4-2022-03-05 18:19:05.701446 -- MqttConnectionHandlerBase:: cancelling connect timer flutter: 4-2022-03-05 18:19:05.702448 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code of identifierRejected and a disconnection origin of none flutter: 4-2022-03-05 18:19:05.702448 -- SynchronousMqttServerConnectionHandler::internalConnect failed flutter: Connection failed - mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message correctly The return code is MqttConnectReturnCode.identifierRejected flutter: 4-2022-03-05 18:19:05.703450 -- MqttConnectionHandlerBase::disconnect - entered flutter: 4-2022-03-05 18:19:05.703450 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered flutter: 4-2022-03-05 18:19:05.703450 -- MqttConnectionKeepAlive::stop - stopping keep alive flutter: Disconnected ```
flutter doctor -v ```bash PS D:\Project> flutter doctor -v Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source! [√] Flutter (Channel stable, 2.10.1, on Microsoft Windows [Version 10.0.19044.1415], locale zh-CN) • Flutter version 2.10.1 at D:\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision db747aa133 (3 weeks ago), 2022-02-09 13:57:35 -0600 • Engine revision ab46186b24 • Dart version 2.16.1 • DevTools version 2.9.2 • Pub download mirror https://pub.flutter-io.cn • Flutter download mirror https://storage.flutter-io.cn [√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) • Android SDK at C:\Users\13710\AppData\Local\Android\sdk • Platform android-32, build-tools 32.1.0-rc1 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6842174) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop for Windows (Visual Studio 生成工具 2019 16.11.9) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools • Visual Studio 生成工具 2019 version 16.11.32106.194 • Windows 10 SDK version 10.0.19041.0 [√] Android Studio (version 4.2) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6842174) [√] VS Code (version 1.65.0) • VS Code at C:\Users\13710\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.36.0 [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19044.1415] • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.102 • Edge (web) • edge • web-javascript • Microsoft Edge 98.0.1108.50 ! Device ac107f92 is not authorized. You might need to check your device for an authorization dialog. [!] HTTP Host Availability X HTTP host https://maven.google.com/ is not reachable. Reason: An error occurred while checking the HTTP host: ! Doctor found issues in 1 category. ```

I use MQTTX client to connect server successfully

shamblett commented 2 years ago

Your identifier is being rejected by the broker in its connect ack message -

Connect Variable Header: ProtocolName=MQTT, ProtocolVersion=4, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=true, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=20

MqttConnectPayload - client identifier is : pigpad_1646475545342

flutter: 4-2022-03-05 18:19:05.394766 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none

flutter: 4-2022-03-05 18:19:05.467510 -- MqttConnection::_onData

flutter: 4-2022-03-05 18:19:05.467510 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck

Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2

Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.identifierRejected}

You need to look at your broker logs/config to find out why.

yh4922 commented 2 years ago

@shamblett But I can connect normally on the MQTTX client using the same parameters

Can this problem be located on the server or client side from the log?

shamblett commented 2 years ago

Its the broker that's rejecting the client id you have supplied, only the broker can tell you why, not the client.

yh4922 commented 2 years ago

@shamblett I am not particularly familiar with the dart language, but I ran an example with nodejs on the same computer and can connect to the server normally

This puzzles me. You guess where the problem is.

Thank you very much for your answer.

shamblett commented 2 years ago

If you don't care what your client id is just use a simpler one for now, use 'pigpad' hard coded for instance.

tongxyj commented 1 year ago

If you don't care what your client id is just use a simpler one for now, use 'pigpad' hard coded for instance.

I replace my client id with 'pigpad' then I can connect to the broker