shamblett / mqtt_client

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

MqttConnectPayload::Client id exceeds spec value of 23 #126

Closed ZionPi closed 4 years ago

ZionPi commented 4 years ago

My client id exceeds 23,how to handle client id exceeds 23?

ZionPi commented 4 years ago

I find this issue says it just logged, but I cannot connect to MQTT server when client id exceeds 23.

shamblett commented 4 years ago

It is just logged as it contravenes the MQTT spec, it doesn't change the client behavior in any way. If your saying you can't connect when the client id exceeds 23 chars but you can otherwise its the server that's rejecting the connection(do you get an error code returned here BTW). If its not connecting at all in either case its not your client id that's causing it.

ZionPi commented 4 years ago

It can connect to the Server when client id length equal or less than 23,and it just cannotconnect to server when client id exceeds 23.The server side maximum accept length is 64,which can be connected using some other language written mqtt client, say C#.I changed your source code maximum limit length from 23 to 64,still no luck.

shamblett commented 4 years ago

As said above this is just a warning, the code, doesn't enforce anything or change its behaviour so changing the limit won't help.

if (id.length > Constants.maxClientIdentifierLengthSpec) {
      MqttLogger.log(
          'MqttConnectPayload::Client id exceeds spec value of ${Constants.maxClientIdentifierLengthSpec}');
    }

You need to look at your server logs to see why the rejection is occurring for this client and not the others.

ZionPi commented 4 years ago

@shamblett For some reason ,I have no chance to access to the mqtt server log you mentioned above, but here I can provide client necessary information as below,

Item Content
clien name GID_HOME_APP@@@866554049565137
user name Signature|LTweqgKhnxCTkxdvhEXoXLp|sd
password bMew6ngonwN45is39Y52az1SQCg=

would you please help me to test it on your server to see if it can successfully log in ?

shamblett commented 4 years ago

Client log using the credentials above to connect to my local mosquitto broker :-

2019-11-18 08:22:15.478834 -- Authenticating with username '{Signature|LTAI4FqgKhnxCTkovhEXoXLp|post-cn-0pp1c8ab60y}' and password '{bMen4ngonwNS3is39Y89az1SQCg=}'
2019-11-18 08:22:15.481834 -- Username length (54) exceeds the max recommended in the MQTT spec. 
2019-11-18 08:22:15.481834 -- Password length (28) exceeds the max recommended in the MQTT spec. 
2019-11-18 08:22:15.492834 -- MqttConnectPayload::Client id exceeds spec value of 23
2019-11-18 08:22:15.494834 -- SynchronousMqttConnectionHandler::internalConnect entered
2019-11-18 08:22:15.494834 -- SynchronousMqttConnectionHandler::internalConnect - initiating connection try 0
2019-11-18 08:22:15.494834 -- SynchronousMqttConnectionHandler::internalConnect - insecure TCP selected
2019-11-18 08:22:15.511834 -- MqttConnection::_startListening
2019-11-18 08:22:15.513834 -- SynchronousMqttConnectionHandler::internalConnect sending connect message
2019-11-18 08:22:15.514834 -- MqttConnectionHandler::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=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
Instance of 'MqttConnectPayload'
2019-11-18 08:22:15.523834 -- SynchronousMqttConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code noneSpecified
2019-11-18 08:22:15.525834 -- MqttConnection::_onData
2019-11-18 08:22:15.529834 -- MqttConnection::_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.connectionAccepted}
2019-11-18 08:22:15.529834 -- MqttConnection::_onData - message processed
2019-11-18 08:22:15.531834 -- SynchronousMqttConnectionHandler::_connectAckProcessor
2019-11-18 08:22:15.531834 -- SynchronousMqttConnectionHandler::_connectAckProcessor - state = connected
2019-11-18 08:22:15.531834 -- SynchronousMqttConnectionHandler:: cancelling connect timer
2019-11-18 08:22:15.531834 -- SynchronousMqttConnectionHandler::internalConnect - post sleep, state = Connection status is connected with return code connectionAccepted
2019-11-18 08:22:15.531834 -- SynchronousMqttConnectionHandler::internalConnect exited with state Connection status is connected with return code connectionAccepted
EXAMPLE::client connected
EXAMPLE::Subscribing to the /hfp/v1/journey/ongoing/+/+/+/2550/2/# topic
2019-11-18 08:22:15.534834 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.subscribe
Header: MessageType = MqttMessageType.subscribe, Duplicate = false, Retain = false, Qos = MqttQos.atLeastOnce, Size = 0
Subscribe Variable Header: MessageIdentifier={1}
Payload: Subscription [{1}]
{{ Topic={/hfp/v1/journey/ongoing/+/+/+/2550/2/#}, Qos={MqttQos.atMostOnce} }}

2019-11-18 08:22:15.536834 -- MqttConnection::_onData
2019-11-18 08:22:15.537834 -- MqttConnection::_onData - message received MQTTMessage of type MqttMessageType.subscribeAck
Header: MessageType = MqttMessageType.subscribeAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 3
SubscribeAck Variable Header: MessageIdentifier={1}
Payload: Qos grants [{1}]
{{ Grant={MqttQos.atMostOnce} }}

2019-11-18 08:22:15.537834 -- MqttConnection::_onData - message processed
EXAMPLE::Subscription confirmed for topic /hfp/v1/journey/ongoing/+/+/+/2550/2/#
EXAMPLE::Disconnecting
2019-11-18 08:22:20.539324 -- SynchronousMqttConnectionHandler::disconnect
2019-11-18 08:22:20.540325 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect
Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
2019-11-18 08:22:20.540325 -- MqttConnectionHandler::sendMessage - not connected
EXAMPLE::OnDisconnected client callback - Client disconnection

Process finished with exit code -1

Seems OK, notice the warnings about client id, username and password don't stop it working.

ZionPi commented 4 years ago

Here is my client log using credentials as I've provided above:

I/flutter (22474): 2019-11-19 10:42:24.519971 -- MqttConnection::_startListening
I/flutter (22474): 2019-11-19 10:42:24.520442 -- SynchronousMqttConnectionHandler::internalConnect sending connect message
I/flutter (22474): 2019-11-19 10:42:24.520617 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (22474): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 130 
I/flutter (22474): 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=60
I/flutter (22474): Instance of 'MqttConnectPayload'
I/flutter (22474): 2019-11-19 10:42:24.521573 -- SynchronousMqttConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code noneSpecified
I/flutter (22474): 2019-11-19 10:42:24.549544 -- MqttConnection::_onDone - calling disconnected callback
I/flutter (22474): 2019-11-19 10:42:29.533007 -- SynchronousMqttConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code noneSpecified
I/flutter (22474): 2019-11-19 10:42:29.534110 -- SynchronousMqttConnectionHandler::internalConnect - initiating connection try 2
I/flutter (22474): 2019-11-19 10:42:29.534222 -- SynchronousMqttConnectionHandler::internalConnect - insecure TCP selected
I/flutter (22474): 2019-11-19 10:42:29.595799 -- MqttConnection::_startListening
I/flutter (22474): 2019-11-19 10:42:29.597199 -- SynchronousMqttConnectionHandler::internalConnect sending connect message
I/flutter (22474): 2019-11-19 10:42:29.597529 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (22474): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 130
I/flutter (22474): 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=60
I/flutter (22474): Instance of 'MqttConnectPayload'
I/flutter (22474): 2019-11-19 10:42:29.599598 -- SynchronousMqttConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code noneSpecified
I/flutter (22474): 2019-11-19 10:42:29.632953 -- MqttConnection::_onDone - calling disconnected callback
I/flutter (22474): 2019-11-19 10:42:34.607498 -- SynchronousMqttConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code noneSpecified
I/flutter (22474): 2019-11-19 10:42:34.607809 -- SynchronousMqttConnectionHandler::internalConnect failed
I/flutter (22474): TCL::client exception - mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message (Missing Connection Acknowledgement
I/flutter (22474): 2019-11-19 10:42:34.620403 -- SynchronousMqttConnectionHandler::disconnect
I/flutter (22474): 2019-11-19 10:42:34.621326 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect
I/flutter (22474): Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0

Cannot connect to the server when client id exceeds 23,it is OK when client 23 is equal or less than 23.I found your Connect Variable Header is ProtocolName=MQTT, ProtocolVersion=4 while mine is ProtocolName=MQIsdp, ProtocolVersion=3 ,does this nuance make the difference?

ZionPi commented 4 years ago

Yes! I nailed it.Well,I have to explicitly declare protocol name and protocol version while creating MqttConnectMessage using .withProtocolName("MQTT") and .withProtocolVersion(4) or it will use MQIsdp 3.1 by default , as this document says "MQTT 3.1 limits the client identifier length to 23 character, however in MQTT 3.1.1 the ClientId MUST be a UTF-8 encoded string and could have a length up to 65,535 character." ,and that is the difference between MQIsdp and MQTT as I've asked above.Thanks very much to @shamblett for being so kind to help me out, thank you.

teganscott commented 3 years ago

Thank you @ZionPi !! Had the same issue!!