emqx / CocoaMQTT

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

Can't connect mqtt over websocket via CocoaMQTT #517

Closed yusufonderd closed 1 year ago

yusufonderd commented 1 year ago
        let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
        let websocket = CocoaMQTTWebSocket(uri: url)
        let mqtt = CocoaMQTT(clientID: clientID,
                             host: "XXXXXXXXXXXXXX-ats.iot.us-east-1.amazonaws.com",
                             port: 443,
                             socket: websocket)
        mqtt.enableSSL = true
        mqtt.delegate = self
         _ = mqtt.connect()
 But i'm getting this errow as below:

NSURLErrorWebSocketHandshakeFailureReasonKey=0

yusufonderd commented 1 year ago

ok i fixed to issue. Here is the solution:


 let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier) + "\(randomInt)"
            let websocket = CocoaMQTTWebSocket(uri: url)
            websocket.shouldConnectWithURIOnly = true
            self.mqtt = CocoaMQTT(clientID: clientID,
                                 host: "",
                                 port: 0,
                                 socket: websocket)
            self.mqtt?.enableSSL = true
            self.mqtt?.autoReconnect = true
            self.mqtt?.allowUntrustCACertificate = true
            self.mqtt?.delegate = self
            _ = self.mqtt?.connect()