shamblett / mqtt_client

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

Error connecting on WSS - HandshakeException: Handshake error in client #70

Closed rfbasso closed 4 years ago

rfbasso commented 5 years ago

When try to connect to a WSS, I'm getting this exception:

I/flutter (24204): 2019-01-29 11:45:48.670052 -- Authenticating with username '{user}' and password '{pass}'
I/flutter (24204): 2019-01-29 11:45:48.710052 -- SynchronousMqttConnectionHandler::internalConnect entered
I/flutter (24204): 2019-01-29 11:45:48.710336 -- SynchronousMqttConnectionHandler::internalConnect - initiating connection try 0
I/flutter (24204): 2019-01-29 11:45:48.710813 -- SynchronousMqttConnectionHandler::internalConnect - websocket selected
I/flutter (24204): 2019-01-29 11:45:48.720252 -- MqttWsConnection:: WS URL is wss://ws.my_wss_url.com:8083, protocols are [mqtt, mqttv3.1, mqttv3.11]
I/flutter (24204): 2019-01-29 11:45:49.673285 -- MqttConnection::_onError - calling disconnected callback

I/flutter (24204): Client exception - HandshakeException: Handshake error in client (OS Error: 
I/flutter (24204):  CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:363))

I/flutter (24204): 2019-01-29 11:45:49.684245 -- SynchronousMqttConnectionHandler::disconnect
I/flutter (24204): 2019-01-29 11:45:49.692898 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect
I/flutter (24204): Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (24204): 2019-01-29 11:45:49.693252 -- MqttConnectionHandler::sendMessage - not connected

But the WSS connection on test.mosquitto.org works perfectly..

shamblett commented 5 years ago

Can't say I've seen this before with wss, like you say it works OK with mosquitto and if you look through other issues raised with other brokers using wss it works without any probs so it looks like your particular broker set up, or it may be something to do with your flutter installation. I don't use flutter so I'm no expert here, to rule this out you could just run the client in a server side Dart environment and see what that does, or find out what flutter is doing, there are quite a few futter devs that use the client.

rfbasso commented 5 years ago

According to this question, that can be a problem with the type of certificate (self signed). I think that would be great if there is a way to accept all types of certificate, like HttpClient..

shamblett commented 5 years ago

Ok, but don't forget that the security context class Dart uses for secure sockets doesn't apply to websockets, the dart.io websocket implementation here doesn't allow you to specify such things as far as I know, the API is not the same as HttpClient.

rfbasso commented 5 years ago

Searching for a solution, I 've considered what was said about my particular broker, and tried to change my certificate. The error change - and I can't figure out why. Looks lyke in some point, the URL protocol change from wss to https

I/flutter (27624): Mosquitto client connecting....
I/flutter (27624): 2019-02-01 12:13:53.849091 -- Authenticating with username '{user}' and password '{pass}'
I/flutter (27624): 2019-02-01 12:13:53.916486 -- SynchronousMqttConnectionHandler::internalConnect entered
I/flutter (27624): 2019-02-01 12:13:53.916841 -- SynchronousMqttConnectionHandler::internalConnect - initiating connection try 0
I/flutter (27624): 2019-02-01 12:13:53.917650 -- SynchronousMqttConnectionHandler::internalConnect - websocket selected

I/flutter (27624): 2019-02-01 12:13:53.932680 -- MqttWsConnection:: WS URL is wss://ws.my_wss_url.com:8083, protocols are [mqtt, mqttv3.1, mqttv3.11]

I/flutter (27624): 2019-02-01 12:13:56.017001 -- MqttConnection::_onError - calling disconnected callback

I/flutter (27624): Client exception - HttpException: Connection closed before full header was received, uri = https://ws.my_wss_url.com:8083

I/flutter (27624): 2019-02-01 12:13:56.024014 -- SynchronousMqttConnectionHandler::disconnect

When I try with the port unencrypted, without any certificate, the connection works perfectly. Maybe could be a problem with this certificate?

shamblett commented 5 years ago

Well yes, always a possibility, you may be better asking the Dart guys here, the client uses the API as exposed by Dart, the magic behind all this is performed in the Dart runtime, maybe it uses an underlying HttpClient socket and upgrades it somehow.

Also it may be worth trying your broker with another client, e.g. the paho or mosqiutto clients for instance, see what they think.

rfbasso commented 5 years ago

Well yes, always a possibility, you may be better asking the Dart guys here, the client uses the API as exposed by Dart, the magic behind all this is performed in the Dart runtime, maybe it uses an underlying HttpClient socket and upgrades it somehow.

Also it may be worth trying your broker with another client, e.g. the paho or mosqiutto clients for instance, see what they think.

I've tried with different clients, incluing Paho Mqtt Spy, and works perfectly, with the 2 certificates.

bigbirdhzp commented 5 years ago

Now I use https://www.cloudmqtt.com/ to create a simple mqtt project.Use the connection of websocket.In mqttBox, the connection is success image image But in flutter,it occurs this error: image I am a chinese developer. The flutter version is 1.8.1-pre23. In 1.7.x,it wouldn't happend,and your exampleis running success.But the when flutter version upgrade, your example can't run success.

shamblett commented 5 years ago

This lokos like a flutter error which version of the mqtt client are you using ?

bigbirdhzp commented 5 years ago

This lokos like a flutter error which version of the mqtt client are you using ?

Your thoughts are the same as mine。So I try to use the flutter branch of stable which the version is 1.7.8+hotfix.3。It didn't work.

bigbirdhzp commented 5 years ago

When you write this plugins,the flutter version is ?

bigbirdhzp commented 5 years ago

I have tried flutter version 1.6.0, 1.7.11, 1.7.10, 1.7.0, use your example ,it didn't connect successful.

shamblett commented 5 years ago

The client is a server side package which you can use in flutter of course. The example was supplied by another contributor I don't develop in flutter its rather dated now as I don't maintain it. I suggest you take the latest client and version of flutter and implement it from scratch without using the example and see how you go.

bigbirdhzp commented 5 years ago

The client is a server side package which you can use in flutter of course. The example was supplied by another contributor I don't develop in flutter its rather dated now as I don't maintain it. I suggest you take the latest client and version of flutter and implement it from scratch without using the example and see how you go.

Thanks you for your guidance! I'll try by myself.

bigbirdhzp commented 5 years ago

The client is a server side package which you can use in flutter of course. The example was supplied by another contributor I don't develop in flutter its rather dated now as I don't maintain it. I suggest you take the latest client and version of flutter and implement it from scratch without using the example and see how you go.

I use flutter clean in command, all problems are fixed.But the wss I haved connected never successful.

trickkiste commented 4 years ago

I am also having no luck connecting to wss:// with different versions of mqtt-spy including 1.0.1 b18.