duanlan2003 2014-07-05 01:46
浏览 132
已采纳

如何在android中使用MQTT从php获取事件更新

I want my app to send a request to server every minute to see if there is any event happened so that it send a HTTP request to fetch new data.

I have a server running windows with WAMP and PHP.

I have already read Dale's post, and searched and read everywhere, but still I don't understand how it works!!!

I want PHP to check the database for events, then if there is a new event, It sends a message to broker that "foo happened for bar", then the client sends a ping to see if there are any new events, then based on the message from the broker, the client knows what data it should fetch from the server.

I want something exactly like Telegram's push updates.

Can you please tell me how to use mqtt very simple? All tutorials I've seen are so much hard to understand and so complicated.

And how to use Mosquitto for that. (I've started the Mosquitto service on hte server, but have no idea where to begin how to use it)

thanks.

  • 写回答

1条回答 默认 最新

  • dqc2017 2014-07-24 09:57
    关注

    MQTT is a publish/subscriber protocol where you have a broker (Mosquitto) and clients that publish and subscribe messages trough the broker. A message has a topic and a payload (content).

    In Mosquitto, appart from the broker you have 2 clients mosquitto_pub and mosquitto_sub.

    So a typical test is like subscribing to a topic:

    mosquitto_sub -d -h localhost -p 1883 -t "myfirst/test"

    And then other client publishes different contents to a topic:

    mosquitto_pub -d -h localhost -p 1883 -t "myfirst/test" -m "Sample content 1" mosquitto_pub -d -h localhost -p 1883 -t "myfirst/test" -m "Sample content 2" mosquitto_pub -d -h localhost -p 1883 -t "myfirst/test" -m "Sample content 3"

    Then you will see that the subscriber gets the contents.

    For python, c, c++, javascript & java you can use the Paho library: http://www.eclipse.org/paho/

    And for php you can use phpMQTT https://github.com/bluerhinos/phpMQTT or a PHP wrapper for the Mosquitto library: Mosquitto-PHP https://github.com/mgdm/Mosquitto-PHP

    But you can see different libraries here: http://mqtt.org/wiki/doku.php/libraries

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败