shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

Please help on why can not connect to broker #70

Closed OpenJarvisAI closed 6 months ago

OpenJarvisAI commented 9 months ago

Hi, this is my code:

 _client =
        MqttServerClient.withPort(host, cid, 1883, maxConnectionAttempts: 3);
    kLog(
        '!!!! ------------> [conn] connect with $host, $port, $username, $password, ${_clientId} $_client');

    _client!.logging(on: true);
    // _client!.logging(on: false);
    _client!.onConnected = onConnected;
    _client!.onDisconnected = onDisconnected;
    _client!.onUnsubscribed = onUnsubscribed;
    _client!.onSubscribed = onSubscribed;
    _client!.onSubscribeFail = onSubscribeFail;
    _client!.onAutoReconnect = onAutoReconnect;
    _client!.pongCallback = pong;
    _client!.keepAlivePeriod = 60 * 3;
    _client!.autoReconnect = false;
    _client!.useWebSocket = useWebsocket;

    final connMessage = MqttConnectMessage()
        .authenticateAs(username, password)
        // .withWillTopic('lastwills')
        // .withWillMessage('Will message')
        .withClientIdentifier(cid);
    // .withProtocolName("MQTT")
    // .withProtocolVersion(4)
    // .withSessionExpiryInterval(interval)
    // .withWillQos(MqttQos.atLeastOnce);
    _client!.connectionMessage = connMessage;
    try {
      //_messagesController =  StreamController.broadcast();
      _broadcastConnectionState();
      await _client!.connect();
      _broadcastConnectionState();
      // only after connected?
      _subscribeToArchivesTopics();
      _listenAndFilter();

      return true;
    } catch (e) {
      kLog('Exception: $e');
      if (_client != null) _client!.disconnect();
      return false;
    }

Now, the connection ends up 3 maxium failure connection, then disconnected.

Every try tells my different reason:


MessageType = MqttMessageType.connectAck Duplicate = false Retain = false Qos = atMostOnce Size = 6
I/flutter (20758): Session Present = true
I/flutter (20758): Connect Reason Code = success
I/flutter (20758): Session Expiry Interval = 0
I/flutter (20758): Receive Maximum = 1024
I/flutter (20758): Maximum QoS = 2
I/flutter (20758): Retain Available = false
I/flutter (20758): Maximum Packet Size = 0
I/flutter (20758): Assigned client Identifier = null
I/flutter (20758): Topic Alias Maximum = 0
I/flutter (20758): Reason String = null
I/flutter (20758): Wildcard Subscription Available = true
I/flutter (20758): Subscription Identifiers Available = true
I/flutter (20758): Shared Subscription Available = true
I/flutter (20758): broker Keep Alive = 0
I/flutter (20758): Response Information = null
I/flutter (20758): broker Reference = null
I/flutter (20758): Authentication Method = null
I/flutter (20758): Properties = Identifier : receiveMaximum, value : 1024
I/flutter (20758):
I/flutter (20758): 2023-11-20 22:38:01.838114 -- MqttServerConnection::_onData - Message Received Ended <<<
I/flutter (20758): 2023-11-20 22:38:01.838530 -- MqttServerConnection::_onData - Message Received Started <<<
I/flutter (20758): 2023-11-20 22:38:01.838665 -- MqttServerConnection::_ondata - adding incoming data, data length is 23, message stream length is 0, message stream position is 0
I/flutter (20758): 2023-11-20 22:38:01.838777 -- MqttServerConnection::_ondata - added incoming data message stream length is 23, message stream position is 0
I/flutter (20758): 2023-11-20 22:38:01.839262 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 224
I/flutter (20758): 2023-11-20 22:38:01.840691 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.disconnect
I/flutter (20758): MessageType = MqttMessageType.disconnect Duplicate = false Retain = false Qos = atMostOnce Size = 21
I/flutter (20758):
I/flutter (20758): Reason Code  = sessionTakenOver
I/flutter (20758): Properties = Identifier : reasonString, value : session takeover
I/flutter (20758): 2023-11-20 22:38:01.841056 -- MqttServerConnection::_onData - Message Received Ended <<<
I/flutter (20758): 2023-11-20 22:38:01.843045 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (20758): 2023-11-20 22:38:01.843274 -- MqttClient::internalDisconnect - not invoking disconnect, no connection handler
I/flutter (20758): 2023-11-20 22:38:06.835011 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
I/flutter (20758): 2023-11-20 22:38:06.835491 -- MqttSynchronousServerConnectionHandler::internalConnect failed
I/flutter (20758): [KLOG] 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?

Can anyone tell me why this happen?

OpenJarvisAI commented 9 months ago

Oh, I change to test.mosquiito.org server, still got same error:

 _client =
        // MqttServerClient.withPort(host, cid, 1883, maxConnectionAttempts: 3);
        MqttServerClient.withPort("test.mosquitto.org", cid, 1883, maxConnectionAttempts: 3);
49356 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (20758): 2023-11-20 22:49:22.549656 -- MqttSynchronousServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of notAuthorized and a disconnection origin of none
I/flutter (20758): 2023-11-20 22:49:22.896146 -- MqttServerConnection::_onData - Message Received Started <<<
I/flutter (20758): 2023-11-20 22:49:22.896568 -- MqttServerConnection::_ondata - adding incoming data, data length is 5, message stream length is 0, message stream position is 0
I/flutter (20758): 2023-11-20 22:49:22.896904 -- MqttServerConnection::_ondata - added incoming data message stream length is 5, message stream position is 0
I/flutter (20758): 2023-11-20 22:49:22.897433 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 32
I/flutter (20758): 2023-11-20 22:49:22.898125 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.connectAck
I/flutter (20758): MessageType = MqttMessageType.connectAck Duplicate = false Retain = false Qos = atMostOnce Size = 3
I/flutter (20758): Session Present = false
I/flutter (20758): Connect Reason Code = notAuthorized
I/flutter (20758): Session Expiry Interval = 0
I/flutter (20758): Receive Maximum = 65535
I/flutter (20758): Maximum QoS = 2
I/flutter (20758): Retain Available = false
I/flutter (20758): Maximum Packet Size = 0
I/flutter (20758): Assigned client Identifier = null
I/flutter (20758): Topic Alias Maximum = 0
I/flutter (20758): Reason String = null
I/flutter (20758): Wildcard Subscription Available = true
I/flutter (20758): Subscription Identifiers Available = true
I/flutter (20758): Shared Subscription Available = true
I/flutter (20758): broker Keep Alive = 0
I/flutter (20758): Response Information = null
I/flutter (20758): broker Reference = null
I/flutter (20758): Authentication Method = null
I/flutter (20758): Properties = No properties set
I/flutter (20758): 2023-11-20 22:49:22.899004 -- MqttServerConnection::_onData - message available event fired
I/flutter (20758): 2023-11-20 22:49:22.899375 -- MqttServerConnection::_onData - Message Received Ended <<<
I/flutter (20758): 2023-11-20 22:49:22.900013 -- MqttConnectionHandlerBase::_connectAckProcessor
I/flutter (20758): 2023-11-20 22:49:22.900401 -- MqttConnectionHandlerBase::_connectAckProcessor connection rejected, reason code is notAuthorized
I/flutter (20758): 2023-11-20 22:49:22.900911 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
I/flutter (20758): 2023-11-20 22:49:22.901248 -- MqttConnectionHandlerBase:: cancelling connect timer
I/flutter (20758): 2023-11-20 22:49:22.902776 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (20758): 2023-11-20 22:49:22.903424 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code of notAuthorized and a disconnection origin of none
I/flutter (20758): 2023-11-20 22:49:22.903661 -- MqttSynchronousServerConnectionHandler::internalConnect failed
I/flutter (20758): [KLOG] Exception: mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message correctly The reason code is notAuthorized

I am using 1883 port, apparently this port is not authoried.

image
shamblett commented 9 months ago

The not authorized reason code is nothing to do with the port, why did you think that? Not authorized means your username or password or both are wrong, please read the MQTT5 spec.