flightonary / Moscapsule

MQTT Client for iOS written in Swift
MIT License
271 stars 70 forks source link

How to receive mqttMessage in mqttConfig.onMessageCallback #31

Open kelly08385 opened 8 years ago

kelly08385 commented 8 years ago

My program can do PUBLISH, PUBRIC, PUBERL, PUBCOMP now The message print in Log is show below

MyProgram[5194:1180483] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q2, r0, m8, 'Test/6', ... (145 bytes)) 2016-04-22 09:42:55.109 MyProgram[5194:1180483] [MOSQUITTO] DEBUG Client cid sending PUBREC (Mid: 8) 2016-04-22 09:42:55.117 MyProgram[5194:1180483] [MOSQUITTO] DEBUG Client cid received PUBREL (Mid: 8) 2016-04-22 09:42:55.117 MyProgram[5194:1180483] [MOSQUITTO] DEBUG Client cid sending PUBCOMP (Mid: 8) 2016-04-22 09:43:25.627

But my program never do this block self.mqttConfig.onMessageCallback = { mqttMessage in NSLog(“MQTT Message received: payload=(mqttMessage.payloadString)") } So I can’t get the mqttMessage.

How to receive mqttMessage?

Thank you!

ps. Sorry for my poor English. If you have any question, please tell me.

My question is similar to this website. http://stackoverflow.com/questions/36289214/how-to-receive-message-of-subscribed-topic-with-moscapsule

flightonary commented 8 years ago

It appears to be no problem because client send PUBCOMP. Would you paste all of MOSQUITTO logs?

And would you try to publish a message after client send PUBCOMP? The thread created by mosquitto library is probably supended.

kelly08385 commented 8 years ago

I only want subscribe with topic, and get the mqttMessage

But I don’t know how to receive the mqttMessage.

I add self.mqttConfig.onMessageCallback = { mqttMessage in NSLog(“MQTT Message received: payload=(mqttMessage.payloadString)") } in my program but it never execute.

How to get the mqttMessage that in MQTT broker?

Does it have any example?

The Log is show below, it has ” Client cid received PUBLISH ”, so how the client get the message which was published?

2016-04-27 08:57:47.598 DropTV_2.0[299:15942] [MOSQUITTO] DEBUG Client cid sending CONNECT false 2016-04-27 08:57:47.602 DropTV_2.0[299:15942] [MOSQUITTO] DEBUG Client cid sending SUBSCRIBE (Mid: 1, Topic: #, QoS: 2) 2016-04-27 08:57:47.607 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received CONNACK 2016-04-27 08:57:47.615 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received SUBACK 2016-04-27 08:57:47.616 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q0, r1, m0, 'droptv2/ShowPgTag/94', ... (142 bytes)) 2016-04-27 08:57:47.616 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q2, r1, m1, 'droptv2/ShowPgTag/40', ... (175 bytes)) 2016-04-27 08:57:47.616 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBREC (Mid: 1) 2016-04-27 08:57:47.617 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q2, r1, m2, 'droptv2/ShowPgTag/41', ... (166 bytes)) 2016-04-27 08:57:47.617 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBREC (Mid: 2) 2016-04-27 08:57:47.617 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q2, r1, m3, 'droptv2/ShowPgTag/42', ... (145 bytes)) 2016-04-27 08:57:47.617 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBREC (Mid: 3) 2016-04-27 08:57:47.622 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q2, r1, m4, 'droptv2/ShowPgTag/10', ... (157 bytes)) 2016-04-27 08:57:47.623 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBREC (Mid: 4) 2016-04-27 08:57:47.623 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q0, r1, m0, 'droptv2/ShowPgTag/91', ... (146 bytes)) 2016-04-27 08:57:47.623 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q0, r1, m0, 'droptv2/ShowPgTag/90', ... (141 bytes)) 2016-04-27 08:57:47.623 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q0, r1, m0, 'droptv2/ShowPgTag/92', ... (145 bytes)) 2016-04-27 08:57:47.625 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q0, r1, m0, 'droptv2/ShowPgTag/93', ... (143 bytes)) 2016-04-27 08:57:47.625 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q2, r1, m5, 'ShowPgTag/8', ... (166 bytes)) 2016-04-27 08:57:47.625 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBREC (Mid: 5) 2016-04-27 08:57:47.627 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBLISH (d0, q0, r1, m0, 'topic123', ... (7 bytes)) 2016-04-27 08:57:47.636 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBREL (Mid: 1) 2016-04-27 08:57:47.636 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBCOMP (Mid: 1) 2016-04-27 08:57:47.639 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBREL (Mid: 2) 2016-04-27 08:57:47.675 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBCOMP (Mid: 2) 2016-04-27 08:57:47.676 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBREL (Mid: 3) 2016-04-27 08:57:47.676 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBCOMP (Mid: 3) 2016-04-27 08:57:47.676 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBREL (Mid: 4) 2016-04-27 08:57:47.676 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBCOMP (Mid: 4) 2016-04-27 08:57:47.689 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid received PUBREL (Mid: 5) 2016-04-27 08:57:47.713 DropTV_2.0[299:16008] [MOSQUITTO] DEBUG Client cid sending PUBCOMP (Mid: 5)

Thanks for your help

bjpetit commented 8 years ago

Kelly08385, Maybe you saw this already, but there is an example of subscribing with an onMessageCallback definition at the bottom of the README.md file.

One thing to note, I believe that the onMessageCallback needs to be set before the connection is made. If you are setting it after the connection, then your callback wont be used.

kelly08385 commented 8 years ago

I think it can work! Thank you very much! One more question. Can mqttMessage's type transfer to Dictionary or Json?

lokendradaphnis commented 7 years ago

2017-05-02 13:12:18.551 Beehive[5713:1641157] [MOSQUITTO] DEBUG Client 30 sending PINGREQ 2017-05-02 13:12:19.263 Beehive[5713:1641157] [MOSQUITTO] DEBUG Client 30 received PINGRESP 2017-05-02 13:13:19.269 Beehive[5713:1641157] [MOSQUITTO] DEBUG Client 30 sending PINGREQ 2017-05-02 13:13:24.081 Beehive[5713:1641157] [MOSQUITTO] DEBUG Client 30 received PINGRESP 2017-05-02 13:14:24.087 Beehive[5713:1641157] [MOSQUITTO] DEBUG Client 30 sending PINGREQ 2017-05-02 13:14:25.023 Beehive[5713:1641157] [MOSQUITTO] DEBUG Client 30 received PINGRESP

i got this message in log and i don't know what's going on and i am not getting any data from onMessageCallBack()

Someone Please help me out

dilipmishra907 commented 5 years ago

I am facing same issue onMessageCallback is not calling.please suggest me what I am doing wrong.

   let clientID = "50ea3d47-5b19-46dd-8a2e-f2a9462c518d"
    print("Client_ID", clientID)
    mqttConfig = MQTTConfig(clientId: clientID, host: defaultHost, port: 1883, keepAlive: 60)
    mqttConfig!.mqttAuthOpts = MQTTAuthOpts(username: "9e1186ff-e0f0-4e20-ba53-f6567042cfef", password: "12345678")

    let subscribe = "u/9e1186ff-e0f0-4e20-ba53-f6567042cfef/sub"
    self.mqttClient?.subscribe(subscribe, qos: 0)

    mqttConfig?.onConnectCallback = { returnCode in
    }
    mqttConfig.onPublishCallback = { messageId in
    }
    mqttConfig?.onMessageCallback = { mqttMessage in

    }