CocoaMQTT
CocoaMQTT copied to clipboard
Mqtt not connecting immediately?
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
}
}
}
}`
Hi. It is also related to the responding speed of the broker server.
Which is working fine in android, this delay happening only in iOS.
I think you can write some time output in the code to detect where it takes the longest time.😄