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

Client subscribe after ack results in Code=7 "Socket closed by remote peer" #264

Open Whoweez opened 5 years ago

Whoweez commented 5 years ago

Hi Everyone,

This is my class:

import Foundation
import CocoaMQTT

protocol didReceiveMessageDelegate {
    func setMessage(message: String)
}

class MQTT: CocoaMQTTDelegate {

    var mqttClient:CocoaMQTT?

    var delegate: didReceiveMessageDelegate?

    func connect() {
        mqttClient = CocoaMQTT(clientID: "iOS_DeviceAXASXAASX", host: "m16.cloudmqtt.com", port: #NUMBER)
        mqttClient!.username = "user"
        mqttClient!.password = "ssap"
        mqttClient!.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
        mqttClient!.keepAlive = 60
        mqttClient!.enableSSL = true
        mqttClient!.delegate = self
        mqttClient!.connect()
        print("Connecting...")
    }

    func disconnect() {
        mqttClient!.disconnect()
        print("Disconnected")
    }

    func publish(topic: String, message: String) {
        mqttClient!.publish(topic, withString: message)
    }

    func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
        print(ack)

        if ack == .accept {
            mqttClient!.publish("/somekindofTopicTopic", withString: "isoduagpiugsdvas")
            mqttClient!.subscribe("/anotherTopic", qos: CocoaMQTTQOS.qos1)
        }
    }

    func mqtt(_ mqtt: CocoaMQTT, didStateChangeTo state: CocoaMQTTConnState) {
        print("didStateChangeTo")
    }

    func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
        print("didPublishMessage")
    }

    func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
        print("didPublishAck")
    }

    func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) {
        print("didReceiveMessage")
    }

    func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topics: [String]) {
        print("didSubscribeTopic")
    }

    func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) {
        print("didUnsubscribeTopic")
    }

    func mqttDidPing(_ mqtt: CocoaMQTT) {
        print("mqttDidPing")
    }

    func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
        print("mqttDidReceivePong")
    }

    func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
        print(err)
    }
}
This is my controller
class ViewController: UIViewController, RuuviTagListener{
  let mqtt = MQTT()
  override func viewDidLoad() {
     mqtt.connect()
  }
}

This is the error:

didStateChangeTo accept didPublishMessage didStateChangeTo Optional(Error Domain=GCDAsyncSocketErrorDomain Code=7 "Socket closed by remote peer" UserInfo={NSLocalizedDescription=Socket closed by remote peer})

When I comment the line "mqttClient!.subscribe" the connection stays open and receive regular pongs...

Can you explain why this client cannot subscribe to any kind of topic?

Thank you :)

HJianBo commented 5 years ago

Hi, @Whoweez Which version to use?

We have fixed a disconnect error caused by sending a subscribe packet at the v1.2.1. See: https://github.com/emqtt/CocoaMQTT/pull/261

So, maybe this error is caused by the same reason.

Whoweez commented 5 years ago

HI @HJianBo,

Thanks for the reply - really appreciate it :)

I'm using: Using CocoaAsyncSocket (7.6.3) Using CocoaMQTT (1.2.1)

The problem here was that your example worked correctly, but my pod install in my project didn't work. So, I checked all other settings such as pod versions/XCode versions and settings and all was the same...

AND SUDDENLY, on the 24th May when I was about to give up, it started working - I wish I knew what happened so that I can contribute to my own problem - but unfortunately I cannot... I have no idea what sparked the libraries to work... (really sorry...)

At this point, the above versions seem to work as expected.

Thanks again :)

HJianBo commented 5 years ago

Thanks for your attention to this @Whoweez.

Let me know if have any question there.

AlokGroomefy commented 5 years ago

I am facing the same issue. Please tell how to fix it, I'm using: CocoaAsyncSocket (7.6.3) and CocoaMQTT (1.2.1). using the port 9001

Whoweez commented 5 years ago

Hi @AlokGroomefy My issue was I was subscribing to a MQTT port and not a websocket port. Perhaps look at your service port and make sure it can handle the respective service TLS host/port. Rgds, Wzz

HJianBo commented 5 years ago

@AlokGroomefy Hi, this thread issue has been fixed at v1.2.1. So your edition should work well. Plz paste the key code and runtime parameters to help me digging it