Closed nouhouari closed 1 month ago
You can't use secure working with websockets, the 'secure' setting is for server side sockets only, please read the clients API documentation.
There is no way of using a cert if you are using websockets, see the Dart API here. You can use the protocol and headers parameters if you need to, again read the clients API documentation.
You can't use secure working with websockets, the 'secure' setting is for server side sockets only, please read the clients API documentation.
There is no way of using a cert if you are using websockets, see the Dart API here. You can use the protocol and headers parameters if you need to, again read the clients API documentation.
I'm not using 'secure'.
From your code above -
SecurityContext securityContext = SecurityContext(withTrustedRoots: true);
securityContext.setTrustedCertificatesBytes(crtData.buffer.asInt8List());
client.securityContext = securityContext;
you're setting a security context whether or not you are setting the secure field, this will not work with websockets, see the Dart API for the SecurityContext class.
It's working now by applying these changes.
client = MqttServerClient(mqttURL, mqttClientId);
client.port = mqttPort;
client.useWebSocket = true;
client.autoReconnect = true;
client.logging(on: true);
SecurityContext securityContext = SecurityContext.defaultContext;
securityContext.setTrustedCertificatesBytes(mqttCertBytes);
client.onBadCertificate = onBadCertificate;
client.securityContext = securityContext;
// Connect to server
await connect(client);
I think the main change is:
SecurityContext securityContext = SecurityContext(withTrustedRoots: true);
I'm trying to connect to a secure websocket over wss to Mosquitto server and publish messages to a MQTT topic.
My server is working fine and I've checked with the mqttx command line.
In the flutter application, I'm using the same ca.crt to secure the connection. Here is my Dart code:
And the workaround to validate the self-signed certificate
Here are the logs of the flutter application running on Android 14 (API 34):
Note that the onBadCertificate is not called.
What am I doing wrong?
Thanks