shamblett / mqtt_client

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

Socket error on client <unknown> #333

Closed Abenstex closed 2 years ago

Abenstex commented 2 years ago

Hello everyone,

I've been struggling a lot to get mqtt up and running with Flutter. I've been using it with Golang and Javascript for a while, but currently I'm at a loss in Flutter. My code is the following:

_mqttClient = MqttBrowserClient(config.getConfigValue("mqttAddress")!, "AnansiUI");

    _mqttClient.port = config.getConfigValue("mqttPort")!;
    _mqttClient.logging(on: true);

    _mqttClient.keepAlivePeriod = 60;

    _mqttClient.keepAlivePeriod =
        config.getConfigValue("mqttKeepAliveSeconds")!;

    _mqttClient.onConnected = _onConnected;

    final connMess = MqttConnectMessage()
        .withClientIdentifier('AnansiUI')
        .keepAliveFor(60) // Must agree with the keep alive set above or not set
        .startClean() // Non persistent session for testing
        .withWillQos(MqttQos.atLeastOnce);
    print('EXAMPLE::Mosquitto client connecting....');
    _mqttClient.connectionMessage = connMess;

    try {
      await _mqttClient.connect();

    } on Exception catch (e) {
      print('EXAMPLE::client exception - $e');
      _mqttClient.disconnect();
    }

However I keep seeing this error message in the mosquitto log file: Socket error on client <unknown>, disconnecting. My mosquitto config is as follows:

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

#log_type error
#log_type warning
log_type all
log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d
allow_anonymous true
port 1883
listener 9001
protocol websockets

The debug output:

EXAMPLE::Mosquitto client connecting....
1-2021-11-15 12:29:26.826 -- MqttClient::connect - keep alive is enabled with a value of 120 seconds
1-2021-11-15 12:29:26.827 -- MqttConnectionKeepAlive:: Initialised with a keep alive value of 120 seconds
1-2021-11-15 12:29:26.827 -- MqttConnectionKeepAlive:: Disconnect on no ping response is disabled
1-2021-11-15 12:29:26.828 -- MqttConnectionHandlerBase::connect - server ws://192.168.2.43, port 9001
1-2021-11-15 12:29:26.828 -- SynchronousMqttBrowserConnectionHandler::internalConnect entered
1-2021-11-15 12:29:26.828 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false
1-2021-11-15 12:29:26.829 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2021-11-15 12:29:26.830 -- MqttBrowserWsConnection::connect - entered
1-2021-11-15 12:29:26.830 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.2.43:9001
1-2021-11-15 12:29:26.834 -- MqttBrowserWsConnection::connect - connection is waiting
1-2021-11-15 12:29:27.186 -- MqttBrowserWsConnection::connect - websocket has erred
1-2021-11-15 12:29:27.187 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2021-11-15 12:29:27.187 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2021-11-15 12:29:27.187 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=120
MqttConnectPayload - client identifier is : AnansiUI

1-2021-11-15 12:29:27.192 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-15 12:29:32.211 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-15 12:29:32.212 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 1, auto reconnect in progress false
1-2021-11-15 12:29:32.212 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2021-11-15 12:29:32.212 -- MqttBrowserWsConnection::connect - entered
1-2021-11-15 12:29:32.213 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.2.43:9001
1-2021-11-15 12:29:32.213 -- MqttBrowserWsConnection::connect - connection is waiting
1-2021-11-15 12:29:32.370 -- MqttBrowserWsConnection::connect - websocket has erred
1-2021-11-15 12:29:32.370 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2021-11-15 12:29:32.371 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2021-11-15 12:29:32.371 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 22
Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=120
MqttConnectPayload - client identifier is : AnansiUI

1-2021-11-15 12:29:32.373 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-15 12:29:37.391 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-15 12:29:37.391 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false
1-2021-11-15 12:29:37.392 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2021-11-15 12:29:37.392 -- MqttBrowserWsConnection::connect - entered
1-2021-11-15 12:29:37.392 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.2.43:9001
1-2021-11-15 12:29:37.394 -- MqttBrowserWsConnection::connect - connection is waiting
1-2021-11-15 12:29:37.597 -- MqttBrowserWsConnection::connect - websocket has erred
1-2021-11-15 12:29:37.597 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2021-11-15 12:29:37.597 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2021-11-15 12:29:37.597 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 22
Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=120
MqttConnectPayload - client identifier is : AnansiUI

1-2021-11-15 12:29:37.598 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-15 12:29:42.624 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-15 12:29:42.625 -- SynchronousMqttBrowserConnectionHandler::internalConnect failed
EXAMPLE::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?
1-2021-11-15 12:29:42.626 -- MqttConnectionHandlerBase::disconnect - entered
1-2021-11-15 12:29:42.626 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
1-2021-11-15 12:29:42.626 -- MqttConnectionKeepAlive::stop - stopping keep alive

What am I missing? Why is it client unknown even though I specified a client id?

Abenstex commented 2 years ago

BTW, I am getting the same debug output when I use 'ws://test.mosquitto.org' and port 8080 or 8081. So I don't think it has anything to do with my internal mosquitto setup especially because I can connect using other applications. I am using version 9.6.1 by the way.

shamblett commented 2 years ago

Run the at the mqtt_browser_client.dart example from the examples directory, this is known to work, see what that gives.

Abenstex commented 2 years ago

I ran the browser client example, but still got the same output:

1-2021-11-16 10:50:29.782 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-16 10:50:34.809 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-16 10:50:34.809 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false
1-2021-11-16 10:50:34.809 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2021-11-16 10:50:34.810 -- MqttBrowserWsConnection::connect - entered
1-2021-11-16 10:50:34.810 -- MqttBrowserWsConnection::connect -  WS URL is ws://test.mosquitto.org:8080
1-2021-11-16 10:50:34.810 -- MqttBrowserWsConnection::connect - connection is waiting
1-2021-11-16 10:50:34.885 -- MqttBrowserWsConnection::connect - websocket has erred
1-2021-11-16 10:50:34.885 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2021-11-16 10:50:34.885 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2021-11-16 10:50:34.885 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 63
Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=true, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=20
MqttConnectPayload - client identifier is : Mqtt_MyClientUniqueId

1-2021-11-16 10:50:34.886 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-16 10:50:39.891 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2021-11-16 10:50:39.892 -- SynchronousMqttBrowserConnectionHandler::internalConnect failed
EXAMPLE::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?
1-2021-11-16 10:50:39.895 -- MqttConnectionHandlerBase::disconnect - entered
1-2021-11-16 10:50:39.896 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
1-2021-11-16 10:50:39.897 -- MqttConnectionKeepAlive::stop - stopping keep alive
EXAMPLE::OnDisconnected client callback - Client disconnection
EXAMPLE::OnDisconnected callback is solicited, this is correct
EXAMPLE::ERROR Mosquitto client connection failed - disconnecting, status is Connection status is disconnected with return code of noneSpecified and a disconnection origin of solicited
EXAMPLE::OnDisconnected client callback - Client disconnection
EXAMPLE::OnDisconnected callback is solicited, this is correct

I thought that maybe the problem could have something with the browser setting, so I set the security level to "insecure" allowing all kind of connections, but that didn't solve the problem unfortunately.

shamblett commented 2 years ago

Try this -

client.websocketProtocols = MqttClientConstants.protocolsSingleDefault;

i.e manually set the WS headers the client sends to mosquito. If this works I'll update the example, I'm sure mosquito didn't need this at one time, other brokers do though.

Abenstex commented 2 years ago

Thank you! That solved the problem

shamblett commented 2 years ago

Browser client example updated, package re released at version 9.6.2.