emqx / CocoaMQTT

MQTT 5.0 client library for iOS and macOS written in Swift
https://www.emqx.com/en
Other
1.6k stars 419 forks source link

How to connect to Broker with TCP/TLS with port 8883? #492

Open lygialiem opened 1 year ago

lygialiem commented 1 year ago

Hi team.

Currently, I setup MQTT connection like this:

let mqtt5 = CocoaMQTT5(clientID: "some_clientID", host: "54.179.31.220", port: 8883)
mqtt5.username = userName mqtt5.password = password mqtt5.allowUntrustCACertificate = true mqtt5.enableSSL = true mqtt5.connect()

but i always get this error: "Socket closed by remote peer". Did i miss something?

Thank you and Best Regard

leeway1208 commented 1 year ago

Hi. Make sure your broker config is consistent with your writing code. Thanks.

JaylinYu commented 1 year ago

please find the corresponding TLS/SSL log from broker side, this usually caused by malformed certificate or the version of SSL lib mismatch

StarkGOD024 commented 1 year ago

I took several days to solve the set allowUntrustCACertificate = true, unable to connect the SSL problems, finally through the source code, found that need to implement the following method

// Optional ssl CocoaMQTTDelegate func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) { TRACE("trust: (trust)") /// Validate the server certificate /// /// Some custom validation... /// /// if validatePassed { /// completionHandler(true) /// } else { /// completionHandler(false) /// } completionHandler(true) }