shamblett / mqtt_client

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

MqttBrowserClient cannot connect to the local Broker Flutter #365

Closed shwezhu closed 2 years ago

shwezhu commented 2 years ago

The broker in running on my PC is mosquitto. and when I use pure dart code without flutter, I use
final client = MqttServerClient('192.168.1.101', ''); which worked fine. i want to say is this issues about the port?(my broker doesn't support 8080?) BUT I tried 8080, or 1883, or 8081, and all of them didn't work.

Here is some of my code:

class _MonitorPageState extends State {
  String topic = 'greenhouse/alarm';
  final client = MqttBrowserClient('ws://192.168.1.101', '');
  List<MyImage> images = [];
  var imageWidgets = <Widget>[
    const Text("hello"),
  ];

  @override
  initState() {
    super.initState();
    client.setProtocolV311();
    client.logging(on: true);
    client.keepAlivePeriod = 20;
    client.port = 8080;
    client.websocketProtocols = MqttClientConstants.protocolsSingleDefault;
  }

// when I click a button, this function wiil be called
  void _connectMqtt() async{
    await client.connect();
    if (client.connectionStatus!.state == MqttConnectionState.connected) {
      showAlertDialog(context, 'warning', 'connect success');
    }

    client.subscribe(topic, MqttQos.atMostOnce);
    client.updates!.listen(_onMessage);
  }
}

here is the log:

1-2022-03-27 09:58:47.776 -- MqttClient::connect - keep alive is enabled with a value of 20 seconds
1-2022-03-27 09:58:47.776 -- MqttConnectionKeepAlive:: Initialised with a keep alive value of 20 seconds
1-2022-03-27 09:58:47.776 -- MqttConnectionKeepAlive:: Disconnect on no ping response is disabled
1-2022-03-27 09:58:47.777 -- MqttConnectionHandlerBase::connect - server ws://192.168.1.101, port 8080
1-2022-03-27 09:58:47.777 -- SynchronousMqttBrowserConnectionHandler::internalConnect entered
1-2022-03-27 09:58:47.777 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false
1-2022-03-27 09:58:47.778 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2022-03-27 09:58:47.778 -- MqttBrowserWsConnection::connect - entered
1-2022-03-27 09:58:47.778 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.1.101:8080
1-2022-03-27 09:58:47.779 -- MqttBrowserWsConnection::connect - connection is waiting
1-2022-03-27 09:58:50.000 -- MqttBrowserWsConnection::connect - websocket has erred
1-2022-03-27 09:58:50.001 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2022-03-27 09:58:50.001 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2022-03-27 09:58:50.001 -- 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.atMostOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=20
MqttConnectPayload - client identifier is : 

1-2022-03-27 09:58:50.002 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-03-27 09:58:55.060 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-03-27 09:58:55.061 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 1, auto reconnect in progress false
1-2022-03-27 09:58:55.061 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2022-03-27 09:58:55.061 -- MqttBrowserWsConnection::connect - entered
1-2022-03-27 09:58:55.061 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.1.101:8080
1-2022-03-27 09:58:55.062 -- MqttBrowserWsConnection::connect - connection is waiting
1-2022-03-27 09:58:57.226 -- MqttBrowserWsConnection::connect - websocket has erred
1-2022-03-27 09:58:57.226 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2022-03-27 09:58:57.226 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2022-03-27 09:58:57.227 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 12
Connect Variable Header: ProtocolName=MQTT, ProtocolVersion=4, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=20
MqttConnectPayload - client identifier is : 

1-2022-03-27 09:58:57.227 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-03-27 09:59:02.258 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-03-27 09:59:02.258 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false
1-2022-03-27 09:59:02.258 -- SynchronousMqttBrowserConnectionHandler::internalConnect - calling connect
1-2022-03-27 09:59:02.258 -- MqttBrowserWsConnection::connect - entered
1-2022-03-27 09:59:02.258 -- MqttBrowserWsConnection::connect -  WS URL is ws://192.168.1.101:8080
1-2022-03-27 09:59:02.259 -- MqttBrowserWsConnection::connect - connection is waiting
1-2022-03-27 09:59:04.583 -- MqttBrowserWsConnection::connect - websocket has erred
1-2022-03-27 09:59:04.584 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
1-2022-03-27 09:59:04.584 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
1-2022-03-27 09:59:04.584 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 12
Connect Variable Header: ProtocolName=MQTT, ProtocolVersion=4, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=20
MqttConnectPayload - client identifier is : 

1-2022-03-27 09:59:04.586 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-03-27 09:59:07.783 -- MqttConnectionKeepAlive::pingRequired
1-2022-03-27 09:59:07.809 -- MqttConnectionKeepAlive::pingRequired - NOT sending ping - not connected
1-2022-03-27 09:59:07.809 -- MqttConnectionKeepAlive::pingRequired - restarting ping timer
1-2022-03-27 09:59:09.595 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
1-2022-03-27 09:59:09.596 -- SynchronousMqttBrowserConnectionHandler::internalConnect failed
Error: mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message (Missing Connection Acknowledgement?
    at Object.throw_ [as throw] (http://localhost:61363/dart_sdk.js:5386:11)
    at mqtt_browser_client.SynchronousMqttBrowserConnectionHandler.new.internalConnect (http://localhost:61363/packages/mqtt_client/mqtt_browser_client.dart.lib.js:162:25)
shwezhu commented 2 years ago

Sorry, I got wrong port, my mosquitt server listen 9003 port

https://stackoverflow.com/a/34416746/16317008