emqx / CocoaMQTT

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

How can create multiple instance for CocoaMQTT #345

Open sw-tt-rushabhpatel opened 4 years ago

sw-tt-rushabhpatel commented 4 years ago

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()
leeway1208 commented 3 years ago

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)")