MQTT 實戰

sc_ik發表於2019-01-19

mqtt 協議版本: 3.1.1
eclipse paho client 版本: 1.2.0

eclipse paho client 介紹

Paho Java Client 是一個用Java編寫的MQTT客戶端庫, 用於開發在JVM或其他Java相容平臺(如Android)上執行的應用程式.

Paho Java Client提供了兩個API: MqttAsyncClient 提供完全非同步的API, 通過註冊的回撥完成. MqttClient 是 MqttAsyncClient 的同步包裝器.

例子

        String topic        = "MQTT Examples";
        String content      = "Message from MqttPublishSample";
        int qos             = 2;
        String broker       = "tcp://iot.eclipse.org:1883";
        String clientId     = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: "+broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }

這個就是官網提供的例子, 可以直接連上代理做測試的.

相關類介紹

MqttClient

此類主要用於連線代理、釋出訊息、訂閱訊息、斷開連線.

public void publish(String topic, byte[] payload,int qos, boolean retained) throws MqttException, MqttPersistenceException {}
public void publish(String topic, MqttMessage message) throws MqttException, MqttPersistenceException {}

public void subscribe(String topicFilter) throws MqttException {}
public void subscribe(String[] topicFilters) throws MqttException {}
public void subscribe(String topicFilter, int qos) throws MqttException {}
public void subscribe(String[] topicFilters, int[] qos) throws MqttException {}
public void subscribe(String topicFilter, IMqttMessageListener messageListener) throws MqttException {}
public void subscribe(String[] topicFilters, IMqttMessageListener[] messageListeners) throws MqttException {}
public void subscribe(String topicFilter, int qos, IMqttMessageListener messageListener) throws MqttException {}
public void subscribe(String[] topicFilters, int[] qos, IMqttMessageListener[] messageListeners) throws MqttException {}

public void connect() throws MqttSecurityException, MqttException {}
public void connect(MqttConnectOptions options) throws MqttSecurityException, MqttException {}

public void disconnect() throws MqttException {}
public void disconnect(long quiesceTimeout) throws MqttException {}

MqttConnectOptions

此類儲存了客戶端連線到代理時的一些引數.

public void setUserName(String userName) {}

public void setPassword(char[] password) {}

public void setConnectionTimeout(int connectionTimeout) {}

MqttMessage

要傳送的訊息正文, 指定訊息內容、Qos、Retained.

public void setQos(int qos) {}
public void setPayload(byte[] payload) {}
public void setRetained(boolean retained) {}

相關文章