Closed quanganh050998 closed 1 week ago
You seem to be using the alternate web socket implementation -
mqttClient?.useAlternateWebSocketImplementation = true;
Don't do this, its for a specific usage only, usually with AWS brokers, it defaults to false, take this line out and see if that helps.
i removed mqttClient?.useAlternateWebSocketImplementation = true;
it return other error
flutter: \^[[38;5;196m│ WebSocketException: Connection to 'https://pricexxxxx.xxxxx.xxx.vn#' was not upgraded to websocket<…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ #0 EMQXManager.connect (package:test_example_mqtt/mqtt_manager.dart:73:16)<…>
flutter: \^[[38;5;196m│ #1 <asynchronous suspension><…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ ⛔ EMQXManager <= connect error<…>
I thinks it auto change 'wss://' -> 'https://'
The client doesn't automatically change anything, if its not your code then its the Dart/flutter runtime.
You say on your first post ' The default example works for me.', so just substitute your wss url(and change the port if needed) in the example and run it, see what happens, if it doesn't work then its at your broker end as you have already said it works with mosquito.
One thing to try is the websocketProtocolString field of the client, read API for this, you may have to set it to [].
I understand your point, but in my code, I see the host is set to 'wss://', whereas the error message indicates it is being converted to 'https://'. I'm curious about why that is happening. Below is my code:
Future<void> connect({bool isReInit = false}) async {
Config.configPs();
final serverURI = "wss://pricexxxxxx"; //Config.getPriceSocketUrl();
final clientId =
"${Config.getAppType()}_android_quanganh_${DateTime.now().millisecondsSinceEpoch}";
mqttClient = MqttServerClient.withPort(serverURI, clientId, 443);
mqttClient?.useWebSocket = true;
mqttClient?.port = 443;
mqttClient?.useAlternateWebSocketImplementation = false;
mqttClient?.websocketProtocolString = [];
mqttClient?.keepAlivePeriod = 60;
mqttClient?.onConnected = onConnected;
mqttClient?.onDisconnected = onDisconnected;
mqttClient?.pongCallback = pong;
final connMess = MqttConnectMessage()
.authenticateAs('xxxx', 'xxxx')
.withClientIdentifier(clientId)
.startClean();
mqttClient?.connectionMessage = connMess;
try {
Logger().i("$TAG => connect clientId: $clientId");
await mqttClient?.connect();
// print('EXAMPLE:data:$data');
if (mqttClient?.connectionStatus!.state ==
MqttConnectionState.connected) {
print('EXAMPLE::Mosquitto client connected');
} else {
/// Use status here rather than state if you also want the broker return code.
print(
'EXAMPLE::ERROR Mosquitto client connection failed - disconnecting, status is ${mqttClient?.connectionStatus}');
}
} catch (e) {
Logger().e("$TAG <= connect error", error: e);
disconnect();
}
}
Then message error:
WebSocketException: Connection to 'https://price-xxxxxxxx.xx#' was not upgraded to websocket<…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ #0 EMQXManager.connect (package:test_example_mqtt/mqtt_manager.dart:72:16)<…>
flutter: \^[[38;5;196m│ #1 <asynchronous suspension><…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ ⛔ EMQXManager <= connect error<…>
flutter: \^[[38;5;196m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;12m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;12m│ #0 EMQXManager.onDisconnected (package:test_example_mqtt/mqtt_manager.dart:181:14)<…>
flutter: \^[[38;5;12m│ #1 MqttClient._disconnect (package:mqtt_client/src/mqtt_client.dart:502:22)<…>
flutter: \^[[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;12m│ 💡 EMQXManager <= connectionLost<…>
This has been seen before and is nothing to do with the client, search the closed issues.
Try the example with your wss url and the protocol settings change I've suggested above, post back what happens.
Ultimately your only recourse will be to examine your broker logs to see exactly why it is refusing the connection, only it knows this, not the client.
I tried suggested above, but it only return error:
WebSocketException: Connection to 'https://price-xxxxxxxx.xx#' was not upgraded to websocket<…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ #0 EMQXManager.connect (package:test_example_mqtt/mqtt_manager.dart:72:16)<…>
flutter: \^[[38;5;196m│ #1 <asynchronous suspension><…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ ⛔ EMQXManager <= connect error<…>
flutter: \^[[38;5;196m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;12m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;12m│ #0 EMQXManager.onDisconnected (package:test_example_mqtt/mqtt_manager.dart:181:14)<…>
flutter: \^[[38;5;12m│ #1 MqttClient._disconnect (package:mqtt_client/src/mqtt_client.dart:502:22)<…>
flutter: \^[[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;12m│ 💡 EMQXManager <= connectionLost<…>
I think the reason for the exception converting the connect URL to 'https://xxxxxx#' might be because the web also uses the same connect link, and it return the old link
I'm not sure what that means or what that image is of. What is the 'old' link?
So are you saying the example fails with your wss URL? If so could you supply a client log please, without it I can't go any further.
I dont understand what u wrote. It console client, it is above, and if u want to log in web, sorry it is private project
As I've said above please run the example that worked with mosquito with your wss URL and post me a log from the client, i.e. turn on logging on the client with 'client.logging(on: true);'.
I'm only interested in what the client is seeing, not anything else you may be doing. You don't need any other code to simply run the mqtt_server_client_websocket_secure example with your wss URL.
It was log below:
2-2024-07-04 17:17:18.784281 -- MqttClient::connect - Connection timeout period is 5000 milliseconds
flutter: 2-2024-07-04 17:17:18.784917 -- MqttClient::connect - keep alive is enabled with a value of 60 seconds
flutter: 2-2024-07-04 17:17:18.785264 -- MqttConnectionKeepAlive:: Initialised with a keep alive value of 60 seconds
flutter: 2-2024-07-04 17:17:18.785434 -- MqttConnectionKeepAlive:: Disconnect on no ping response is disabled
flutter: 2-2024-07-04 17:17:18.785772 -- MqttConnectionHandlerBase::connect - server wss://price-streaming.vndirect.com.vn, port 443
flutter: 2-2024-07-04 17:17:18.785927 -- SynchronousMqttServerConnectionHandler::internalConnect entered
flutter: 2-2024-07-04 17:17:18.786113 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false
flutter: 2-2024-07-04 17:17:18.786287 -- SynchronousMqttServerConnectionHandler::internalConnect - websocket selected
flutter: 2-2024-07-04 17:17:18.786462 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect
flutter: 2-2024-07-04 17:17:18.787266 -- MqttWsConnection::connect - entered
flutter: 2-2024-07-04 17:17:18.787630 -- MqttWsConnection::connect - WS URL is wss://pricexxxx.xxxx.xx:443, protocols are [mqtt, mqttv3.1, mqttv3.11]
flutter: 2-2024-07-04 17:17:18.930968 -- MqttConnectionBase::_onError - calling disconnected callback
flutter: \^[[38;5;196m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;196m│ WebSocketException: Connection to 'https://price-streaming.vndirect.com.vn#' was not upgraded to websocket<…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ #0 EMQXManager.connect (package:test_example_mqtt/mqtt_manager.dart:73:16)<…>
flutter: \^[[38;5;196m│ #1 <asynchronous suspension><…>
flutter: \^[[38;5;196m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;196m│ ⛔ EMQXManager <= connect error<…>
flutter: \^[[38;5;196m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: 2-2024-07-04 17:17:18.933254 -- MqttConnectionHandlerBase::disconnect - entered
flutter: 2-2024-07-04 17:17:18.933417 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
flutter: 2-2024-07-04 17:17:18.934108 -- MqttConnectionKeepAlive::stop - stopping keep alive
flutter: \^[[38;5;12m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: \^[[38;5;12m│ #0 EMQXManager.onDisconnected (package:test_example_mqtt/mqtt_manager.dart:182:14)<…>
flutter: \^[[38;5;12m│ #1 MqttClient._disconnect (package:mqtt_client/src/mqtt_client.dart:502:22)<…>
flutter: \^[[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<…>
flutter: \^[[38;5;12m│ 💡 EMQXManager <= connectionLost<…>
flutter: \^[[38;5;12m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
flutter: 2-2024-07-04 17:17:59.202763 -- MqttConnectionKeepAlive::pingRequired
flutter: 2-2024-07-04 17:17:59.203867 -- MqttConnectionKeepAlive::pingRequired - NOT sending ping - not connected
flutter: 2-2024-07-04 17:17:59.204179 -- MqttConnectionKeepAlive::pingRequired - restarting ping timer
OK so have you tried changing the web socket protocol string I mentioned above? If none of this works you need to examine your broker logs to see why it is refusing the connection, the client can't tell you this.
Hello! First off, thank you for making such an epic library!
I'm having some issues when trying to use the MqttServerClient. The default example works for me.
Description:
I'm able to access my WSS server using another MQTT client, but for some reason it doesn't work when I'm using dart.
I had code project android, it works perfectly fine:
However, when I try to implement the same functionality using Flutter, I encounter issues with the WebSocket connection. Specifically, the following code results in an error:
Error:
The following error is encountered when trying to establish the WebSocket connection:
I dont find code check TLS in MqttServerClient in flutter. With code ios i have code to connect success: