shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

MqttBrowserClient connection to a local Broker in flutter does not work #429

Closed Simeon5566 closed 1 year ago

Simeon5566 commented 1 year ago

I tried to connect to local Mosquitto Broker it has following config:

listener 1883
allow_anonymous true
listener 8080
protocol websockets

In flutter i tried to build a connection to the local Broker

MqttClient tmpClient = MqttBrowserClient('ws://${mqttConfig["address"]}/', 'testthat');
tmpClient.port = 8080;
tmpClient.websocketProtocols = MqttClientConstants.protocolsSingleDefault;
tmpClient.setProtocolV311(); 
tmpClient.logging(on: true);
tmpClient.autoReconnect = true;
tmpClient.keepAlivePeriod = 20;
tmpClient.connectTimeoutPeriod = 5000;
tmpClient.onConnected = onConnected;
tmpClient.onDisconnected = onDisconnected;
tmpClient.onSubscribed = onSubscribed;tmpClient.pongCallback = pong;
final connMess = MqttConnectMessage()
        .startClean()
        .withWillQos(MqttQos.atLeastOnce);
tmpClient.connectionMessage = connMess;
    try {
      await tmpClient.connect();
    } on Exception catch (e) {
      debugPrint(e.toString());
      tmpClient.disconnect();
    }

But getting the Following error message:

1-2022-12-19 14:14:27.743 -- MqttClient::connect - Connection timeout period is 5000 milliseconds
1-2022-12-19 14:14:27.746 -- MqttClient::connect - keep alive is enabled with a value of 20 seconds
1-2022-12-19 14:14:27.747 -- MqttConnectionKeepAlive:: Initialised with a keep alive value of 20 seconds
1-2022-12-19 14:14:27.747 -- MqttConnectionKeepAlive:: Disconnect on no ping response is disabled
1-2022-12-19 14:14:27.747 -- MqttConnectionHandlerBase::connect - server ws://192.168.0.190/, port 8080
1-2022-12-19 14:14:27.747 -- SynchronousMqttBrowserConnectionHandler::internalConnect entered
1-2022-12-19 14:14:27.748 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false
1-2022-12-19 14:14:27.748 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2022-12-19 14:14:27.748 -- MqttBrowserWsConnection::connect - entered
1-2022-12-19 14:14:27.749 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.0.190:8080/
1-2022-12-19 14:14:27.750 -- MqttBrowserWsConnection::connect - connection is waiting
1-2022-12-19 14:14:27.852 -- MqttBrowserWsConnection::connect - websocket is open
1-2022-12-19 14:14:27.853 -- MqttBrowserConnection::_startListening
1-2022-12-19 14:14:27.853 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2022-12-19 14:14:27.853 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2022-12-19 14:14:27.853 -- 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=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=20
MqttConnectPayload - client identifier is : testthat

1-2022-12-19 14:14:27.855 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-12-19 14:14:27.855 -- MqttBrowserConnection::_startListening - websocket is closed
1-2022-12-19 14:14:27.856 -- MqttConnectionBase::_onDone - calling disconnected callback
1-2022-12-19 14:14:32.882 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-12-19 14:14:32.882 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 1, auto reconnect in progress false
1-2022-12-19 14:14:32.882 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2022-12-19 14:14:32.882 -- MqttBrowserWsConnection::connect - entered
1-2022-12-19 14:14:32.882 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.0.190:8080/
1-2022-12-19 14:14:32.882 -- MqttBrowserWsConnection::connect - connection is waiting
1-2022-12-19 14:14:32.985 -- MqttBrowserWsConnection::connect - websocket is open
1-2022-12-19 14:14:32.985 -- MqttBrowserConnection::_startListening
1-2022-12-19 14:14:32.985 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2022-12-19 14:14:32.985 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2022-12-19 14:14:32.985 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 20
Connect Variable Header: ProtocolName=MQTT, ProtocolVersion=4, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=20
MqttConnectPayload - client identifier is : testthat

1-2022-12-19 14:14:32.986 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-12-19 14:14:32.986 -- MqttBrowserConnection::_startListening - websocket is closed
1-2022-12-19 14:14:32.986 -- MqttConnectionBase::_onDone - calling disconnected callback
1-2022-12-19 14:14:38.015 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-12-19 14:14:38.015 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false
1-2022-12-19 14:14:38.015 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2022-12-19 14:14:38.015 -- MqttBrowserWsConnection::connect - entered
1-2022-12-19 14:14:38.015 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.0.190:8080/
1-2022-12-19 14:14:38.016 -- MqttBrowserWsConnection::connect - connection is waiting
1-2022-12-19 14:14:38.118 -- MqttBrowserWsConnection::connect - websocket is open
1-2022-12-19 14:14:38.118 -- MqttBrowserConnection::_startListening
1-2022-12-19 14:14:38.118 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2022-12-19 14:14:38.118 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2022-12-19 14:14:38.118 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 20
Connect Variable Header: ProtocolName=MQTT, ProtocolVersion=4, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=20
MqttConnectPayload - client identifier is : testthat

1-2022-12-19 14:14:38.119 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-12-19 14:14:38.119 -- MqttBrowserConnection::_startListening - websocket is closed
1-2022-12-19 14:14:38.119 -- MqttConnectionBase::_onDone - calling disconnected callback
1-2022-12-19 14:14:43.153 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-12-19 14:14:43.153 -- SynchronousMqttBrowserConnectionHandler::internalConnect failed
mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message (Missing Connection Acknowledgement?
1-2022-12-19 14:14:43.155 -- MqttConnectionHandlerBase::disconnect - entered
1-2022-12-19 14:14:43.155 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
1-2022-12-19 14:14:43.155 -- MqttConnectionKeepAlive::stop - stopping keep alive
temiRobot1::onDisconnected client disconnecting from 192.168.0.190

Is there something that i am missing? this problem cost me now a lot of time and found no solution that solved it

Simeon5566 commented 1 year ago

problem solved forget to add an willMessage and willTopic

final connMess = MqttConnectMessage()
        .withClientIdentifier('testthat')
        .withWillTopic('test')
        .withWillMessage('hello there')
        .startClean()
        .withWillQos(MqttQos.atLeastOnce);