简体   繁体   中英

MQTT Timeout when trying to connect to TLS enabled broker in unity3d

I am building an AR app in Unity and I need to connect to an MQTT broker with TLS protocol enabled. I am able to connect to non TLS enabled MQTT brokers successfully(In unity), and I can use the certificate the admin gave me with this program http://www.mqttfx.org/ to connect to the TLS enabled broker successfully. But when I attempt to connect to the TLS enabled server with the m2mqtt library for c# in Unity, I get a timeout exception and never receive a response from the broker. This is the code I am using to test the connection. I am just attempting to make a successful connection then disconnect.

X509Certificate certificate = X509Certificate.CreateFromCertFile("filePath");

// Connect to this broker address at port 1883 with tls enabled 
client = new MqttClient(brokerAddress, port, true, certificate);

// Generate a client id
string clientId = Guid.NewGuid().ToString(); 

// Connect to the client with credentials
byte code = client.Connect(clientId, username, password;

// Disconnect       
client.Disconnect ();

This exception occurs during connect:

MqttCommunicationException: An application exception has occurred.
uPLibrary.Networking.M2Mqtt.MqttClient.SendReceive (System.Byte[] msgBytes, Int32 timeout) (at Assets/MQTT/scripts/MqttClient.cs:953)
uPLibrary.Networking.M2Mqtt.MqttClient.SendReceive (uPLibrary.Networking.M2Mqtt.Messages.MqttMsgBase msg, Int32 timeout) (at Assets/MQTT/scripts/MqttClient.cs:976)
uPLibrary.Networking.M2Mqtt.MqttClient.SendReceive (uPLibrary.Networking.M2Mqtt.Messages.MqttMsgBase msg) (at Assets/MQTT/scripts/MqttClient.cs:964)
uPLibrary.Networking.M2Mqtt.MqttClient.Connect (System.String clientId, System.String username, System.String password, Boolean willRetain, Byte willQosLevel, Boolean willFlag, System.String willTopic, System.String willMessage, Boolean cleanSession, UInt16 keepAlivePeriod) (at Assets/MQTT/scripts/MqttClient.cs:456)
uPLibrary.Networking.M2Mqtt.MqttClient.Connect (System.String clientId, System.String username, System.String password, Boolean cleanSession, UInt16 keepAlivePeriod) (at Assets/MQTT/scripts/MqttClient.cs:395)
ConnectionManager.connectToMQTTBroker () (at Assets/Scripts/ConnectionManager.cs:55)

Any help would be greatly appreciated. I have been stuck on this for days.

M2Mqtt does not Support TLS_V2 , Because of what we cant use certificates with unity. This is due to .Net FrameWork Version Support . For Certificates we use TLSV_2 QOS Level for connecting which is supported from .Net 4 and above , But Unity has support for only .Net 3.5 Version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM