shamblett / mqtt_client

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

Cannot connect to websocket with both BrowserClient and ServerClient #159

Closed apiep closed 4 years ago

apiep commented 4 years ago

Hi, I'm trying to use mqtt inside my dart package. I have tried to use ServerClient and BrowserClient without success, even though I can connect to my broker using mqtt.js and mqttbox (mqttbox configuration below).

This is my connection configuration:

 final connectionMessage = MqttConnectMessage() //
      .withWillTopic('u/guest-1002/s')
      .withWillMessage('0')
      .withWillRetain()
      .startClean();

  var brokerUrl = 'wss://mqtt.qiscus.com/mqtt';
  var client = MqttBrowserClient(brokerUrl, 'dart-sdk')
        ..logging(on: true)
        // ..useWebSocket = true
        ..port = 1336
        ..keepAlivePeriod = 10
        ..setProtocolV311()
        ..websocketProtocols = ['mqtt']
        ..connectionMessage = connectionMessage
      //
      ;

  await client.connect();

I have tried to change any configuration from protocols to use all 3, or just 'mqtt', but neither are working.

Here is the error from browser:

dart_sdk.js:22313 2020-03-13 20:12:56.019 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 0
dart_sdk.js:22313 2020-03-13 20:12:56.029 -- MqttBrowserWsConnection::connect -  WS URL is wss://mqtt.qiscus.com:1336/mqtt
dart_sdk.js:22313 2020-03-13 20:13:00.403 -- MqttBrowserWsConnection::connect - connection is waiting
dart_sdk.js:92313 WebSocket connection to 'wss://mqtt.qiscus.com:1336/mqtt' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
dart_sdk.js:22313 2020-03-13 20:13:21.439 -- MqttBrowserWsConnection::connect - websocket has errored
dart_sdk.js:22313 2020-03-13 20:13:21.443 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
dart_sdk.js:22313 2020-03-13 20:13:21.444 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
dart_sdk.js:22313 2020-03-13 20:13:21.445 -- MqttBrowserConnectionHandler::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=true, WillQos=MqttQos.atMostOnce, WillRetain=true, PasswordFlag=false, UserNameFlag=false, KeepAlive=0
MqttConnectPayload - client identifier is : 

dart_sdk.js:92337 WebSocket is already in CLOSING or CLOSED state.

Full log here

MQTTBox configuration: Screenshot_26

apiep commented 4 years ago

Connection issue are fixed now, it was because I put the wrong port number. I think I need some rest now 😣.

But, if I keep / add those connection message, it still did not want to connect.

With this error:

2020-03-13 20:44:01.668 -- SynchronousMqttBrowserConnectionHandler::internalConnect entered
20:44:01.670 dart_sdk.js:22313 2020-03-13 20:44:01.669 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 0
20:44:01.676 dart_sdk.js:22313 2020-03-13 20:44:01.676 -- MqttBrowserWsConnection::connect -  WS URL is wss://mqtt.qiscus.com:1886/mqtt
20:44:01.682 dart_sdk.js:22313 2020-03-13 20:44:01.681 -- MqttBrowserWsConnection::connect - connection is waiting
20:44:02.857 dart_sdk.js:22313 2020-03-13 20:44:02.857 -- MqttBrowserWsConnection::connect - websocket is open
20:44:02.858 dart_sdk.js:22313 2020-03-13 20:44:02.858 -- MqttBrowserConnection::_startListening
20:44:02.861 dart_sdk.js:22313 2020-03-13 20:44:02.860 -- SynchronousMqttBrowserConnectionHandler::internalConnect - connection complete
20:44:02.861 dart_sdk.js:22313 2020-03-13 20:44:02.861 -- SynchronousMqttBrowserConnectionHandler::internalConnect sending connect message
20:44:02.862 dart_sdk.js:22313 2020-03-13 20:44:02.862 -- MqttBrowserConnectionHandler::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=false, WillFlag=true, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false, KeepAlive=0
MqttConnectPayload - client identifier is : 

20:44:02.867 dart_sdk.js:22313 2020-03-13 20:44:02.867 -- SynchronousMqttBrowserConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code noneSpecified
20:44:03.011 dart_sdk.js:22313 2020-03-13 20:44:03.011 -- MqttBrowserConnection::_onData
20:44:03.012 dart_sdk.js:22313 2020-03-13 20:44:03.012 -- MqttBrowserConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck
Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2
Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.identifierRejected}

20:44:03.013 dart_sdk.js:22313 2020-03-13 20:44:03.013 -- MqttBrowserConnection::_onData - message processed
20:44:03.015 dart_sdk.js:22313 2020-03-13 20:44:03.015 -- SynchronousMqttBrowserConnectionHandler::_connectAckProcessor
20:44:03.015 dart_sdk.js:22313 2020-03-13 20:44:03.015 -- SynchronousMqttBrowserConnectionHandler::_connectAckProcessor connection rejected
20:44:03.016 dart_sdk.js:22313 2020-03-13 20:44:03.016 -- SynchronousMqttBrowserConnectionHandler:: cancelling connect timer
20:44:03.017 dart_sdk.js:22313 2020-03-13 20:44:03.016 -- SynchronousMqttBrowserConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code identifierRejected
20:44:03.017 dart_sdk.js:22313 2020-03-13 20:44:03.017 -- SynchronousMqttBrowserConnectionHandler::internalConnect - initiating connection try 1
20:44:03.017 dart_sdk.js:22313 2020-03-13 20:44:03.017 -- MqttBrowserWsConnection::connect -  WS URL is wss://mqtt.qiscus.com:1886/mqtt
20:44:03.019 dart_sdk.js:22313 2020-03-13 20:44:03.019 -- MqttBrowserWsConnection::connect - connection is waiting
20:44:03.020 dart_sdk.js:22313 2020-03-13 20:44:03.020 -- MqttBrowserConnection::_startListening - websocket is closed
20:44:03.020 dart_sdk.js:22313 2020-03-13 20:44:03.020 -- MqttBrowserConnection::_onDone - calling disconnected callback
20:44:03.021 dart_sdk.js:22313 2020-03-13 20:44:03.021 -- SynchronousMqttBrowserConnectionHandler::disconnect
20:44:03.021 dart_sdk.js:22313 2020-03-13 20:44:03.021 -- MqttBrowserConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect
Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0

20:44:03.022 dart_sdk.js:92337 Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
shamblett commented 4 years ago

Your getting this return code form your MQ broker

ReturnCode={MqttConnectReturnCode.identifierRejected}

its rejecting your client identifier for some reason.

apiep commented 4 years ago

Ah yes, seems like clientId provided in the constructor got overwriten by connectionMessage.