flightonary / Moscapsule

MQTT Client for iOS written in Swift
MIT License
273 stars 69 forks source link

After SUBSCRIBING to topic connection getting disconnect #72

Open shantharsk opened 6 years ago

shantharsk commented 6 years ago

Hi I'm trying to connect Connect to my MQTT server with SSL Certificate authentication it worked fine. After I subcribed the Topic the connection getting disconnected. This is my code moscapsule_init() **let mqttConfig = MQTTConfig(clientId: "shantha", host: "aws.amazonaws.com", port:poer, keepAlive: 60) let path = Bundle.main.path(forResource: "ca", ofType: "pem") let path1 = Bundle.main.path(forResource: "client", ofType: "crt") let key = Bundle.main.path(forResource: "client", ofType: "key")

    mqttConfig.mqttServerCert = MQTTServerCert(cafile: path, capath: nil)
    mqttConfig.mqttClientCert = MQTTClientCert(certfile: path1!, keyfile: key!, keyfile_passwd: nil)
    mqttConfig.mqttTlsOpts = MQTTTlsOpts(tls_insecure: false, cert_reqs: CertReqs.ssl_verify_peer, tls_version: "tlsv1", ciphers: nil)

    mqttConfig.onConnectCallback = { returnCode in
        if returnCode == ReturnCode.success {
            print("connected")
            // something to do in case of successful connection
        }
        else {
            // error handling for connection failure
        }
    }

            mqttConfig.onMessageCallback = { mqttMessage in
                if mqttMessage.topic == "topic" {
                    NSLog("MQTT Message received: payload=\(mqttMessage.payloadString)")
                } else  {
                    // something to do in case of other topics
                }
            }

    let mqttClient = MQTT.newConnection(mqttConfig)

    mqttClient.connectTo(host: "aws.amazonaws.com", port: 7926, keepAlive: 60)
            let deadlineTime = DispatchTime.now() + .seconds(20)
            DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
               mqttClient.subscribe("topic", qos: 2, requestCompletion: nil)
            })**

Im geting connection keep disconnecting can please help me with this issue.

Thanks,

smartAkash commented 6 years ago

I am geting same issue connection keep disconnecting can please help me with this issue.