Lee Spratt
Lee Spratt

Reputation: 11

Cocoa MQTT - Socket closed by remote peer

I'm having major issues when I try connecting to my server with CocoaMQTT. I am running a Ubuntu server with the Mosquitto broker.

I have followed all the instructions given but I still can't find a solution.

I have used the standard Cocoa connection function.

private func createMQTTConnection() {
    let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
    mqtt = CocoaMQTT(clientID: clientID, host: "xxx.xx.xx.x", port: 1883)
    mqtt!.username = "root"
    mqtt!.password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    //mqtt.willMessage = CocoaMQTTWill(topic: "pi/pir", message: "dieout")
    mqtt!.keepAlive = 60
    mqtt!.delegate = self
    mqtt!.allowUntrustCACertificate = true
    mqtt!.connect()

This code is then called in the viewDidLoad

verride func viewDidLoad() {

    super.viewDidLoad()
    //establishes a connection 
    createMQTTConnection()
     // creates a message 
     mqtt!.publish("pi/pir", withString: "Iphone Connected")
    // Do any additional setup after loading the view.
}

I have all the other standard cocoaMQTT items

extension MQTT: CocoaMQTTDelegate {

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

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

}

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

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

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

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

func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
    print("mqttDidDisconnect: \(err?.localizedDescription ?? "")")
}

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

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

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

func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
    print("didReceive trust")
}

}

I then receive the following error;

didPublishMessage: and 2 mqttDidDisconnect: Socket closed by remote peer

Any help would be greatly appreciated. Thanks

Upvotes: 1

Views: 1530

Answers (2)

LaborEtArs
LaborEtArs

Reputation: 2033

While Stephens answer is right, the base problem might be something different: The 'Sandbox' on Apple devices.

By default outgoing network connections are not permitted.

You have to check the 'Outgoing Connections (Client)' flag in the settings for your app.

Select the app in Xcode, switch to 'Target' and open 'Signing & Capabilities'. Here you can mark the checkbox 'Network'/'Outgoing Connections (Client)'.

As the permission is asked after starting the app, you may have to restart the app for a second time afterwards (or implement code to handle changed permissions).

Upvotes: 0

Stephen
Stephen

Reputation: 739

Calling connect is an asynchronous call and you likely aren't connected at the time you're calling publish. Try moving your publish into the didConnect delegate method.

func mqtt(_ mqtt: CocoaMQTT, didConnect host: String, port: Int) {
    print("mqtt did connect to \(host):\(port)")
    mqtt.publish("pi/pir", withString: "Iphone Connected")
}

log:

mqtt did connect to 192.168.1.104:1883 
didPublishMessage: <CocoaMQTT.CocoaMQTTMessage: 0x1c0674100> and 2

Upvotes: 0

Related Questions