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

server with the specified hostname could not be found #514

Closed sivasubm1 closed 1 year ago

sivasubm1 commented 1 year ago

I followed the exact code strategy of CocoaMqtt. I am trying to connect MQTT over WSS , but my url is appended with https instead of wss. So i am getting NSLocalizedDescription=A server with the specified hostname could not be found.}

emitted url - https://msgvip.s05590.us.mal-baro.com:58415/mqtt

expected url - wss://msgvip.s05590.us.mal-baro.com:58415/mqtt

pod installed - pod 'CocoaMQTT' pod 'CocoaMQTT/WebSockets'

code followed: import CocoaMQTT

class MqttManager: NSObject , ObservableObject {

var mqttClient: CocoaMQTT? var mqtt: CocoaMQTT5? var identifier: String! var host: String! var topic: String! var userName: String! var password: String!

func intializeMqtt(host: String, identifier: String, userName: String? = nil , password: String? = nil){

if mqttClient != nil { mqttClient = nil }

    self.identifier = identifier
    self.host = host
    self.userName = ""
    self.password = ""

    let clientID = "SUB2"
    let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
    websocket.enableSSL = true
    mqtt = CocoaMQTT5(clientID: clientID, host: host, port: 58415, socket: websocket)

 let connectProperties = MqttConnectProperties()
    connectProperties.topicAliasMaximum = 0
    connectProperties.sessionExpiryInterval = 0
    connectProperties.receiveMaximum = 100
    connectProperties.maximumPacketSize = 50
    mqtt!.connectProperties = connectProperties

    if let finalUserName = self.userName , let finalPassword = self.password {
        mqtt!.username = finalUserName
        mqtt!.password = finalPassword
    }

    mqtt!.delegate = self

    mqtt?.keepAlive = 60
    mqtt?.enableSSL = true
    mqtt?.cleanSession = false
    mqtt?.allowUntrustCACertificate = true

}

func connect() {
    if let success = mqtt?.connect(), success {

       print("Connection Success")
             } else {
                 print("Connection Failed")
    }
}

}

leeway1208 commented 1 year ago

Hi. To solve this problem, you could add breakpoint on connect in CocoaMQTTWebSocket.swift file.

Parameshvadivel commented 1 year ago

Did found any solution on this ?

leeway1208 commented 1 year ago

Did found any solution on this ?

Maybe this problem happens because the certificate used for SSL is not trusted.

JaylinYu commented 1 year ago

This is a TLS issue, not much for cocoamqtt could do