且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Eclipse中的MQTT读取数据Paho?

更新时间:2023-11-13 11:51:22

您不会从MQTT代理读取数据,您订阅一个主题并发送有一个新消息被发布到该主题的数据。

You don't read data from a MQTT broker, you subscribe to a topic and get sent the data when ever a new message is published to that topic.

所以你需要实现一个 MqttCallback 接口并在连接上设置

So you need to implement an instance of the MqttCallback interface and set it on the connection

client.setCallback(new MqttCallback() {
    pubic void connectionLost(Throwable cause) {
    }

    public void messageArrived(String topic,
                MqttMessage message)
                throws Exception {
        System.out.println(message.toString());
    }

    public void deliveryComplete(IMqttDeliveryToken token) {
    }
});

然后你需要告诉经纪人你感兴趣的话题。

Then you need to tell the broker which topics you are interested in.

client.subscribe("topic/foo")