shamblett / mqtt_client

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

can't connect to websocket #118

Closed crnaosa closed 5 years ago

crnaosa commented 5 years ago

Hello, here is what we get in the logcat. And also tried with each protocol or with empty list. If you have any suggestion, it would be much appreciated.

Installing build/app/outputs/apk/app.apk... I/flutter (11263): EXAMPLE::Mosquitto client connecting.... I/flutter (11263): 2019-09-12 16:46:48.258728 -- SynchronousMqttConnectionHandler::internalConnect entered I/flutter (11263): 2019-09-12 16:46:48.262073 -- SynchronousMqttConnectionHandler::internalConnect - initiating connection try 0 I/flutter (11263): 2019-09-12 16:46:48.262276 -- SynchronousMqttConnectionHandler::internalConnect - websocket selected I/flutter (11263): 2019-09-12 16:46:48.277452 -- MqttWsConnection:: WS URL is wss://xxx.yyy.zzz.fff:1882, protocols are [mqtt] Syncing files to device Android SDK built for x86... I/flutter (11263): 2019-09-12 16:46:48.695407 -- MqttConnection::_onError - calling disconnected callback I/flutter (11263): EXAMPLE::client exception - SocketException: OS Error: Connection reset by peer, errno = 104, address = xxx.yyy.zzz.fff, port = 40916 I/flutter (11263): 2019-09-12 16:46:48.698178 -- SynchronousMqttConnectionHandler::disconnect I/flutter (11263): 2019-09-12 16:46:48.700524 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect I/flutter (11263): Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0 I/flutter (11263): 2019-09-12 16:46:48.700696 -- MqttConnectionHandler::sendMessage - not connected I/flutter (11263): EXAMPLE::OnDisconnected client callback - Client disconnection D/ (11263): HostConnection::get() New Host Connection established 0xaaef8540, tid 11284 Lost connection to device. Could not update files on device: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:57753/y-NdE-B8UMo=/

And here is part of our clients setting that's different than yours:

final MqttClient client = MqttClient('wss://xxx.yyy.zzz.fff', '');
client.useWebSocket = true;
client.port = 1882;  //( or whatever your WS port is)
client.websocketProtocols = MqttWsConnection.protocolsSingleDefault;

If you have any working of how it works with websockets it would be great :)

crnaosa commented 5 years ago

If I try with ws instead of wss, we get this

I/flutter (11525): EXAMPLE::Mosquitto client connecting.... I/flutter (11525): 2019-09-12 16:59:19.910144 -- SynchronousMqttConnectionHandler::internalConnect entered I/flutter (11525): 2019-09-12 16:59:19.917224 -- SynchronousMqttConnectionHandler::internalConnect - initiating connection try 0 I/flutter (11525): 2019-09-12 16:59:19.917453 -- SynchronousMqttConnectionHandler::internalConnect - websocket selected I/flutter (11525): 2019-09-12 16:59:19.952159 -- MqttWsConnection:: WS URL is ws://xxx.yyy.zzz.fff:1882, protocols are [mqtt] Syncing files to device Android SDK built for x86... I/flutter (11525): 2019-09-12 16:59:20.289330 -- MqttConnection::_onError - calling disconnected callback I/flutter (11525): EXAMPLE::client exception - HttpException: , uri = http://xxx.yyy.zzz.fff:1882 I/flutter (11525): 2019-09-12 16:59:20.290754 -- SynchronousMqttConnectionHandler::disconnect I/flutter (11525): 2019-09-12 16:59:20.291831 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect I/flutter (11525): Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0 I/flutter (11525): 2019-09-12 16:59:20.291884 -- MqttConnectionHandler::sendMessage - not connected I/flutter (11525): EXAMPLE::OnDisconnected client callback - Client disconnection D/ (11525): HostConnection::get() New Host Connection established 0xa9787100, tid 11548 Lost connection to device. Could not update files on device: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:58877/H89EMW0zz8k=/

shamblett commented 5 years ago

Looks as though the broker is not happy with the connection message, if you are using the client code trim the connection message down, the example shows

final MqttConnectMessage connMess = MqttConnectMessage() .withClientIdentifier('Mqtt_MyClientUniqueId') .keepAliveFor(20) // Must agree with the keep alive set above or not set .withWillTopic('willtopic') // If you set this you must set a will message .withWillMessage('My Will message') .startClean() // Non persistent session for testing .withWillQos(MqttQos.atLeastOnce);

Get rid of everything except the client id if you need this and try that, some brokers seem to be funny with these settings. If you still have no joy you will need to examine your broker logs to see why its rejecting the connection message.

crnaosa commented 5 years ago

Thank you for the response. We have solved the problem. It turned out it wasn't the websockets connection at all, altough we have to use ip address and port. Here's how we set it:

final MqttClient client = MqttClient('aaa.bbb.ccc.ddd', '');

Future<int> main() async {
  client.useWebSocket = false;
  client.port = 1882;