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

Mqtt not connecting immediately? #523

Open vadlani9 opened 1 year ago

vadlani9 commented 1 year ago

Mqtt is not connecting immediately, it's taking upto 6-7 sec's of time to connect in iOS swift. Is it common in iOS or is there any issue in my development.

`func connectMqtt(mqttProfile: MQTTConfiguration, handler: @escaping (Result<String,MQTTError>)->()){
let clientID =  String(ProcessInfo().processIdentifier)
mqttReference = CocoaMQTT(clientID: clientID, host: mqttProfile.baseURL, port: UInt16(mqttProfile.port))
mqttReference?.username = mqttProfile.userName
mqttReference?.password = mqttProfile.password        

DispatchQueue.global(qos: .userInitiated).async { [self] in
    //mqttReference?.logLevel = .debug
    mqttReference?.enableSSL = true
    mqttReference?.sslSettings = [
        GCDAsyncSocketManuallyEvaluateTrust: NSNumber(booleanLiteral: true),
        GCDAsyncSocketUseCFStreamForTLS: NSNumber(booleanLiteral: false),
        kCFStreamSSLPeerName as String: "" as NSString,
        GCDAsyncSocketSSLProtocolVersionMin: NSNumber(integerLiteral: Int(SSLProtocol.tlsProtocol1.rawValue)),
        GCDAsyncSocketSSLProtocolVersionMax: NSNumber(integerLiteral: Int(SSLProtocol.tlsProtocol12.rawValue)),
    ]

    ///mqttReference?.keepAlive = 300
    ///mqttReference?.cleanSession = false
    mqttReference?.autoReconnect = true
    mqttReference?.backgroundOnSocket = mqttProfile.allowBackgroundMQTT
    mqttReference?.delegate = self
    do {
        if((mqttReference?.connect())!){
            mqttReference?.didConnectAck = { mqtt, ack in
                switch ack {
                case .accept:
                    handler(.success("Connected"))
                    self.isMqttConncted = true
                    privateVariables.shared.mqttEnabled = true
                    BlazeClient.shared.mqttConnected = true
                    self.didReciveMqttMessage(.init(topic: "connectivity", string: "connected"), 1)
                default:
                    handler(.failure(.badURL))
                    self.isMqttConncted = false
                    privateVariables.shared.mqttEnabled = false
                    self.didReciveMqttMessage(.init(topic: "connectivity", string: "disconnected"), 0)
                }
            }
        }
    }
}
}`

Delegate:

`internal func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
switch ack {
case .accept:
    SDKdebug().debug(value:"mqtt connected")
    self.isMqttConncted = true
    privateVariables.shared.mqttEnabled = true
    BlazeClient.shared.mqttConnected = true
    self.didReciveMqttMessage(.init(topic: "connectivity", string: "connected"), 1)
default:

    SDKdebug().debug(value:ack.description)

    self.isMqttConncted = false
    privateVariables.shared.mqttEnabled = false
    BlazeClient.shared.mqttConnected = false

    DispatchQueue.main.async {
        self.startToMQTT { (sttaus) in

        }
    }

}
}`
leeway1208 commented 1 year ago

Hi. It is also related to the responding speed of the broker server.

vadlani9 commented 1 year ago

Which is working fine in android, this delay happening only in iOS.

leeway1208 commented 1 year ago

I think you can write some time output in the code to detect where it takes the longest time.😄