shamblett / mqtt_client

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

Can not connect to server anymore after upgrade to mqtt5_client #492

Closed OpenJarvisAI closed 7 months ago

OpenJarvisAI commented 10 months ago
I/flutter (  321): 2023-11-18 16:48:10.116560 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (  321): 2023-11-18 16:48:10.116733 -- MqttSynchronousServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
I/flutter (  321): 2023-11-18 16:48:10.117275 -- MqttServerConnection::_startListening
I/flutter (  321): 2023-11-18 16:48:10.117481 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete
I/flutter (  321): 2023-11-18 16:48:10.117652 -- MqttSynchronousServerConnectionHandler::internalConnect sending connect message
I/flutter (  321): 2023-11-18 16:48:10.117754 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.connect
I/flutter (  321): MessageType = MqttMessageType.connect Duplicate = false Retain = false Qos = atMostOnce Size = 69
I/flutter (  321): ProtocolName = MQTT
I/flutter (  321): ProtocolVersion = 5
I/flutter (  321): ConnectFlags = CleanStart=false, WillFlag=false, WillQos=atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true
I/flutter (  321): KeepAlive = 0
I/flutter (  321): Properties = No properties setWill topic = lastwills
I/flutter (  321): User name = fanghezi
I/flutter (  321): Password = 1195889656
I/flutter (  321): 2023-11-18 16:48:10.118490 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (  321): 2023-11-18 16:48:10.118672 -- MqttSynchronousServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
I/flutter (  321): 2023-11-18 16:48:10.125188 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (  321): 2023-11-18 16:48:10.129472 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (  321): 2023-11-18 16:48:15.122576 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
I/flutter (  321): 2023-11-18 16:48:15.123005 -- MqttSynchronousServerConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false
I/flutter (  321): 2023-11-18 16:48:15.123199 -- MqttSynchronousServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (  321): 2023-11-18 16:48:15.123435 -- MqttSynchronousServerConnectionHandler::internalConnect - calling connect
I/flutter (  321): 2023-11-18 16:48:15.123786 -- MqttNormalConnection::connect- entered
I/flutter (  321): 2023-11-18 16:48:15.124650 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
I/flutter (  321): 2023-11-18 16:48:15.125026 -- MqttSynchronousServerConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false
I/flutter (  321): 2023-11-18 16:48:15.125245 -- MqttSynchronousServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (  321): 2023-11-18 16:48:15.125527 -- MqttSynchronousServerConnectionHandler::internalConnect - calling connect
I/flutter (  321): 2023-11-18 16:48:15.125735 -- MqttNormalConnection::connect- entered
I/flutter (  321): 2023-11-18 16:48:15.146021 -- MqttServerConnection::_startListening
I/flutter (  321): 2023-11-18 16:48:15.146316 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete
I/flutter (  321): 2023-11-18 16:48:15.146475 -- MqttSynchronousServerConnectionHandler::internalConnect sending connect message
I/flutter (  321): 2023-11-18 16:48:15.146584 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.connect
I/flutter (  321): MessageType = MqttMessageType.connect Duplicate = false Retain = false Qos = atMostOnce Size = 69
I/flutter (  321): ProtocolName = MQTT
I/flutter (  321): ProtocolVersion = 5
I/flutter (  321): ConnectFlags = CleanStart=false, WillFlag=false, WillQos=atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true
I/flutter (  321): KeepAlive = 0
I/flutter (  321): Properties = No properties setWill topic = lastwills
I/flutter (  321): User name = fanghezi
I/flutter (  321): Password = 1195889656
I/flutter (  321): 2023-11-18 16:48:15.147370 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (  321): 2023-11-18 16:48:15.147544 -- MqttSynchronousServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
I/flutter (  321): 2023-11-18 16:48:15.148244 -- MqttServerConnection::_startListening
I/flutter (  321): 2023-11-18 16:48:15.148462 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete
I/flutter (  321): 2023-11-18 16:48:15.148552 -- MqttSynchronousServerConnectionHandler::internalConnect sending connect message
I/flutter (  321): 2023-11-18 16:48:15.148639 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.connect
I/flutter (  321): MessageType = MqttMessageType.connect Duplicate = false Retain = false Qos = atMostOnce Size = 69
I/flutter (  321): ProtocolName = MQTT
I/flutter (  321): ProtocolVersion = 5
I/flutter (  321): ConnectFlags = CleanStart=false, WillFlag=false, WillQos=atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true
I/flutter (  321): KeepAlive = 0
I/flutter (  321): Properties = No properties setWill topic = lastwills
I/flutter (  321): User name = fanghezi
I/flutter (  321): Password = 1195889656
I/flutter (  321): 2023-11-18 16:48:15.149407 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (  321): 2023-11-18 16:48:15.149550 -- MqttSynchronousServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
I/flutter (  321): 2023-11-18 16:48:15.156127 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (  321): 2023-11-18 16:48:15.160091 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (  321): 2023-11-18 16:48:20.150892 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
I/flutter (  321): 2023-11-18 16:48:20.151349 -- MqttSynchronousServerConnectionHandler::internalConnect failed
I/flutter (  321): [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?
I/flutter (  321): 2023-11-18 16:48:20.156687 -- MqttConnectionHandlerBase::disconnect
I/flutter (  321): 2023-11-18 16:48:20.156925 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
I/flutter (  321): [KLOG] !!! got disconnected!! [Disconnected] Disconnected!!
I/flutter (  321): 2023-11-18 16:48:20.159809 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code of notSet and a disconnection origin of none
I/flutter (  321): 2023-11-18 16:48:20.159931 -- MqttSynchronousServerConnectionHandler::internalConnect failed
I/flutter (  321): [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?
I/flutter (  321): [AppData] conn.. ConnectionState.disconnected false

Please kindly help, here is the code for conenction:

  _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;
    _client!.autoReconnect = false;
    _client!.useWebSocket = useWebsocket;

    final connMessage = MqttConnectMessage()
        .authenticateAs(username, password)
        .withWillTopic('lastwills')
        // .withWillMessage('Will message')
        .withClientIdentifier(cid)
        // .withProtocolName("MQTT")
        // .withProtocolVersion(4)
        .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;
    }
shamblett commented 10 months ago

This is your error -

2023-11-18 16:48:20.151349 -- MqttSynchronousServerConnectionHandler::internalConnect failed
I/flutter (  321): [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?
I/flutter (  321): 2023-11-18 16:48:20.156687 -- MqttConnectionHandlerBase::disconnect

Missing connection acknowledge so there is something probably still wrong with your connect message.

I see you are still using will topic and will qos, I think I advised you to get rid of these while you are trying to get things working, try -

 final connMessage = MqttConnectMessage()
        .authenticateAs(username, password)
         .withClientIdentifier(cid);

for now.

OpenJarvisAI commented 10 months ago

Hello, sir, it's not work.

Log:

I/flutter (18028): 2023-11-20 22:05:42.401565 -- MqttServerConnection::_onData - Message Received Started <<<
I/flutter (18028): 2023-11-20 22:05:42.401848 -- MqttServerConnection::_ondata - adding incoming data, data length is 31, message stream length is 0, message stream position is 0
I/flutter (18028): 2023-11-20 22:05:42.402100 -- MqttServerConnection::_ondata - added incoming data message stream length is 31, message stream position is 0
I/flutter (18028): 2023-11-20 22:05:42.402556 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 32
I/flutter (18028): 2023-11-20 22:05:42.409961 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.connectAck
I/flutter (18028): MessageType = MqttMessageType.connectAck Duplicate = false Retain = false Qos = atMostOnce Size = 6
I/flutter (18028): Session Present = false
I/flutter (18028): Connect Reason Code = success
I/flutter (18028): Session Expiry Interval = 0
I/flutter (18028): Receive Maximum = 1024
I/flutter (18028): Maximum QoS = 2
I/flutter (18028): Retain Available = false
I/flutter (18028): Maximum Packet Size = 0
I/flutter (18028): Assigned client Identifier = null
I/flutter (18028): Topic Alias Maximum = 0
I/flutter (18028): Reason String = null
I/flutter (18028): Wildcard Subscription Available = true
I/flutter (18028): Subscription Identifiers Available = true
I/flutter (18028): Shared Subscription Available = true
I/flutter (18028): broker Keep Alive = 0
I/flutter (18028): Response Information = null
I/flutter (18028): broker Reference = null
I/flutter (18028): Authentication Method = null
I/flutter (18028): Properties = Identifier : receiveMaximum, value : 1024
I/flutter (18028):
I/flutter (18028): 2023-11-20 22:05:42.412109 -- MqttServerConnection::_onData - message available event fired
I/flutter (18028): 2023-11-20 22:05:42.412220 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 224
I/flutter (18028): 2023-11-20 22:05:42.414941 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.disconnect
I/flutter (18028): MessageType = MqttMessageType.disconnect Duplicate = false Retain = false Qos = atMostOnce Size = 21
I/flutter (18028):
I/flutter (18028): Reason Code  = sessionTakenOver
I/flutter (18028): Properties = Identifier : reasonString, value : session takeover
I/flutter (18028): 2023-11-20 22:05:42.415811 -- MqttServerConnection::_onData - message available event fired
I/flutter (18028): 2023-11-20 22:05:42.415887 -- MqttServerConnection::_onData - Message Received Ended <<<
I/flutter (18028): 2023-11-20 22:05:42.417283 -- MqttConnectionHandlerBase::_connectAckProcessor
I/flutter (18028): 2023-11-20 22:05:42.417632 -- MqttConnectionHandlerBase::_connectAckProcessor - state = connected
I/flutter (18028): [KLOG] [onConnected] Connected, is this connected? Instance of 'MqttServerClient'
I/flutter (18028): [KLOG] client connected!

-------- Imediately:

I/flutter (18028): 2023-11-20 22:05:42.418071 -- MqttConnectionHandlerBase:: cancelling connect timer
I/flutter (18028): 2023-11-20 22:05:42.418790 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.disconnect
I/flutter (18028): 2023-11-20 22:05:42.418915 -- MqttConnectionHandlerBase::messageAvailable - WARN - no registered callback for this message type
I/flutter (18028): [AppData] conn.. ConnectionState.connected true
I/flutter (18028): [AppData] joining my personal events with local user addr
I/flutter (18028): 2023-11-20 22:05:42.424123 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 1
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = personalevents/usrItug3Lj2c5, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.426236 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: personalevents/usrItug3Lj2c5
I/flutter (18028): 2023-11-20 22:05:42.426645 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 2
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = archivesmessages/usrItug3Lj2c5, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.427054 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: archivesmessages/usrItug3Lj2c5
I/flutter (18028): 2023-11-20 22:05:42.428031 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is connected with return code of success and a disconnection origin of none
I/flutter (18028): 2023-11-20 22:05:42.428152 -- MqttSynchronousServerConnectionHandler::internalConnect exited with state Connection status is connected with return code of success and a disconnection origin of none
I/flutter (18028): [KLOG] client connected!
I/flutter (18028): [KLOG] start subscribeArchivesTopic, Instance of 'MqttServerClient'
I/flutter (18028): 2023-11-20 22:05:42.428929 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 3
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = archivesrooms/android_client_iDxic_usrItug3Lj2c5, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.429327 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): 2023-11-20 22:05:42.429491 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 4
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = archivesmessages/android_client_iDxic_usrItug3Lj2c5, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.429918 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): 2023-11-20 22:05:42.430215 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 5
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = personalmyid/android_client_iDxic_usrItug3Lj2c5, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.430657 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [client subscribe archives] just after connect
I/flutter (18028): [AppData] conn.. ConnectionState.connected true
I/flutter (18028): [AppData] joining my personal events with local user addr
I/flutter (18028): 2023-11-20 22:05:42.435136 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 6
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = messages/usrItug3Lj2c5usrItug3Lj2c5, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.435956 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: messages/usrItug3Lj2c5usrItug3Lj2c5
I/flutter (18028): 2023-11-20 22:05:42.436286 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 7
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = events/usrItug3Lj2c5usrItug3Lj2c5, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.437147 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: events/usrItug3Lj2c5usrItug3Lj2c5
I/flutter (18028): 2023-11-20 22:05:42.437483 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 8
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = messages/6924b459-c072-49cc-9b5a-5870c748bc62, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.437951 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: messages/6924b459-c072-49cc-9b5a-5870c748bc62
I/flutter (18028): 2023-11-20 22:05:42.438183 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 9
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = events/6924b459-c072-49cc-9b5a-5870c748bc62, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.438480 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: events/6924b459-c072-49cc-9b5a-5870c748bc62
I/flutter (18028): 2023-11-20 22:05:42.438670 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 10
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = messages/3e858f88-6f86-4545-8191-fff3b0d1b4bc, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.439206 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: messages/3e858f88-6f86-4545-8191-fff3b0d1b4bc
I/flutter (18028): 2023-11-20 22:05:42.439513 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 11
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = events/3e858f88-6f86-4545-8191-fff3b0d1b4bc, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.439935 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: events/3e858f88-6f86-4545-8191-fff3b0d1b4bc
I/flutter (18028): 2023-11-20 22:05:42.440165 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 12
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = messages/51a7b1b4-d3fe-42f2-a233-4212602a005d, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.440511 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: messages/51a7b1b4-d3fe-42f2-a233-4212602a005d
I/flutter (18028): 2023-11-20 22:05:42.440753 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 13
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = events/51a7b1b4-d3fe-42f2-a233-4212602a005d, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.441066 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: events/51a7b1b4-d3fe-42f2-a233-4212602a005d
I/flutter (18028): 2023-11-20 22:05:42.441442 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 14
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = messages/6b9c6ad6-c31f-43fe-a751-23527d6ba79e, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.441824 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: messages/6b9c6ad6-c31f-43fe-a751-23527d6ba79e
I/flutter (18028): 2023-11-20 22:05:42.441993 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
I/flutter (18028): MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
I/flutter (18028): Message Identifier = 15
I/flutter (18028): Subscription identifier = 0
I/flutter (18028): Properties = No properties set
I/flutter (18028): Topic = events/6b9c6ad6-c31f-43fe-a751-23527d6ba79e, Option = Maximum Qos = atLeastOnce
I/flutter (18028): No Local = false
I/flutter (18028): Retain As Published = true
I/flutter (18028): Retain Handling = sendRetained
I/flutter (18028): 2023-11-20 22:05:42.442330 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter (18028): [KLOG] [JION ROOM] !!!! ******** join roomId: events/6b9c6ad6-c31f-43fe-a751-23527d6ba79e
I/flutter (18028): 2023-11-20 22:05:42.446003 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter (18028): [KLOG] !!! got disconnected!! [Disconnected] Disconnected!!
I/flutter (18028): [KLOG] [isConnected] _client is null!! not connected
I/flutter (18028): [KLOG] [isConnected] _client is null!! not connected
I/flutter (18028): [KLOG] [isConnected] _client is null!! not connected
I/flutter (18028): [KLOG] [isConnected] _client is null!! not connected
I/flutter (18028): [KLOG] [isConnected] _client is null!! not connected
I/flutter (18028): 2023-11-20 22:05:42.448891 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (18028): 2023-11-20 22:05:42.449011 -- MqttClient::internalDisconnect - not invoking disconnect, no connection handler
I/flutter (18028): [AppData] conn.. ConnectionState.disconnected false

As you can see, it first connect to broker, but after that, disconnect without any reason.

(My network is OK, and this happens every time).

mine:

  _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;
shamblett commented 10 months ago

The first log snippet shoes -

I/flutter (18028): Reason Code  = sessionTakenOver
I/flutter (18028): Properties = Identifier : reasonString, value : session takeover

This is an MQTT5 reason code, not an MQTT 3 one as they don't exist in MQTT3, you have filed this on the wrong client. In the MQTT5 spec this means -

If the ClientID represents a Client already connected to the Server, the Server sends a DISCONNECT packet to the existing Client with Reason Code of 0x8E (Session taken over) as described in [section 4.13](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#S4_13_Errors) and MUST close the Network Connection of the existing Client

You are connecting twice with the same client id, why?

shamblett commented 10 months ago

Looks OK up to the point of disconnecting, is does this always happen after a certain time or randomly?

Note its no use showing me broker logs, its not my software so I don't know the details of what it means, you need to ask your broker supplier, not me.

OpenJarvisAI commented 9 months ago

@shamblett Very much apprecaited help. thanks for the reply.

Am using mqtt5 yes.

This is the code I simplfied to connect, please help take a look, it now have a lock, should not possible to connect twice.

@override
  Future<bool> connect(
      {required host,
      required String username,
      required String password,
      String? clientId,
      int? port}) async {
    if (!connectLock) {
      String cid = clientId ?? getClientId()!;
      _clientId = cid;
      if (useWebsocket) {
        host = "ws://" + host;
      }
      _client =
          // MqttServerClient.withPort(host, cid, 1883, maxConnectionAttempts: 3);
          MqttServerClient.withPort("test.mosquitto.org", 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;

      connectLock = true;
      try {
        //_messagesController =  StreamController.broadcast();
        _broadcastConnectionState();
        await _client!.connect();
        _broadcastConnectionState();
        // only after connected?
        _subscribeToArchivesTopics();
        _listenAndFilter();

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

        connectLock = false;
        return false;
      }
    } else {
      kLog("!!!!xxx connect is locked ! do not repeat connect");
      return false;
    }
  }

But now I got:

Does this open broker need authorize?

shamblett commented 9 months ago

Yes lookls as though you are not seeting the username/passwotd it needs.