CocoaMQTT
CocoaMQTT copied to clipboard
How can create multiple instance for CocoaMQTT
How can we connect multiple devices using mqtt ? ``` mqtt = CocoaMQTT(clientID: mqttConfig.shared.clientID, host: mqttConfig.shared.host, port: UInt16(mqttConfig.shared.port)) mqtt!.username = mqttConfig.shared.username mqtt!.password = mqttConfig.shared.password mqtt!.keepAlive = 60 mqtt!.delegate = self mqtt!.enableSSL = true mqtt!.cleanSession = true mqtt!.connect()
Creates an empty array of CocoaMQTT called instanceArray. You can add elements to this array and create multiple instance.
let hostArray:[String] = ["host1", "host2", "host3"]
var instanceArray = [CocoaMQTT]()
for host in hostArray {
mqtt = CocoaMQTT(clientID: host + String(ProcessInfo().processIdentifier), host: host, port: 1883)
//...
instanceArray.append(mqtt!)
}
print("clientID = \(instanceArray[2].clientID)")