shamblett / mqtt_client

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

WebSocket connection does not work #336

Closed MartinGeier closed 2 years ago

MartinGeier commented 2 years ago

I tried to run the mqtt_server_client_websocket.dart example connecting to test.mosquitto.org and or broker.hivemq.com but the result is the same:

flutter: EXAMPLE::Mosquitto client connecting.... [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: HttpException: Connection closed before full header was received, uri = http://test.mosquitto.org:8080

Flutter: 2.5.3 Dart: 2.14.4 target: Windows or Android both produce the same error

shamblett commented 2 years ago

The mqtt_server_client_websocket.dart example uses this as the url -

ws://test.mosquitto.org

You seem to be using -

http://test.mosquitto.org:8080

This will not work, a full client log would help with further diagnostics.

MartinGeier commented 2 years ago

Actually I did non change a single bit of the example code. The address I'm using is ws://test.mosquitto.org. I'm getting this same error on a bigger project I'm working on. To simplify debugging I created a new flutter project and used just the example code in main.dart. There is no other code. What I send is the full log I'm getting in the debug console. I don't understand why the URI starts with http:// in the log. Could it be the HTTP stack reporting it this way?

shamblett commented 2 years ago

I'm not flutter user so Im not sure what main.dart or the http stack is. As I've said I need a full client log, you need to turn on trace on the client.

MartinGeier commented 2 years ago

I put the example in a dart console application. This is the log:

`EXAMPLE::Mosquitto client connecting.... 1-2021-11-29 11:33:59.995459 -- MqttClient::connect - keep alive is enabled with a value of 20 seconds

1-2021-11-29 11:34:00.000459 -- MqttConnectionKeepAlive:: Initialised with a keep alive value of 20 seconds

1-2021-11-29 11:34:00.000459 -- MqttConnectionKeepAlive:: Disconnect on no ping response is disabled

1-2021-11-29 11:34:00.001459 -- MqttConnectionHandlerBase::connect - server ws://test.mosquitto.org, port 8080 1-2021-11-29 11:34:00.003493 -- SynchronousMqttServerConnectionHandler::internalConnect entered 1-2021-11-29 11:34:00.003493 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false 1-2021-11-29 11:34:00.004461 -- SynchronousMqttServerConnectionHandler::internalConnect - websocket selected 1-2021-11-29 11:34:00.004461 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect 1-2021-11-29 11:34:00.005474 -- MqttWsConnection::connect - entered 1-2021-11-29 11:34:00.008486 -- MqttWsConnection::connect - WS URL is ws://test.mosquitto.org:8080, protocols are [mqtt, mqttv3.1, mqttv3.11] 1-2021-11-29 11:34:05.098575 -- MqttConnectionBase::_onError - calling disconnected callback Unhandled exception: HttpException: Connection closed before full header was received, uri = http://test.mosquitto.org:8080`

shamblett commented 2 years ago

You are using the wrong headers for mosquito you try using the single default header not the multiple one take a look at the browser client example for the websocket header API.

MartinGeier commented 2 years ago

Thanks, that solved it.