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

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

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)