4

我可以使用此 Java 代码订阅 mosquitto 代理,无需用户名和密码。现在,我想订阅一个需要一些虚拟用户名和密码的 emqttd 代理。我怎样才能做到这一点?。谢谢。

http://tgrall.github.io/blog/2017/01/02/getting-started-with-mqtt/#disqus_thread

https://github.com/emqtt/emqttd

package com.mapr.demo.mqtt.simple;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;

public class Subscriber {

  public static void main(String[] args) throws MqttException {

    System.out.println("== START SUBSCRIBER ==");

    MqttClient client=new MqttClient("tcp://localhost:1883", MqttClient.generateClientId());
    client.setCallback( new SimpleMqttCallBack() );
    client.connect();

    client.subscribe("iot_data");

  }

}
4

2 回答 2

11

您可以使用MqttConnectOptions

 public class Subscriber {

    private static final String CONNECTION_URL = "tcp://localhost:1883";
    private static final String SUBSCRIPTION = "iot_data";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "top-secret";


    public static void main(String[] args) throws MqttException {

       System.out.println("== START SUBSCRIBER ==");

       MqttClient client = new MqttClient(CONNECTION_URL, 
       MqttClient.generateClientId());

       MqttConnectOptions connOpts = setUpConnectionOptions(USERNAME, PASSWORD);
       client.connect(connOpts);

       client.subscribe(SUBSCRIPTION);

    }

   private static MqttConnectOptions setUpConnectionOptions(String username, String password) {
       MqttConnectOptions connOpts = new MqttConnectOptions();
       connOpts.setCleanSession(true);
       connOpts.setUserName(username);
       connOpts.setPassword(password.toCharArray());
       return connOpts;
   }  

  }
于 2018-08-01T07:36:10.193 回答
0

这是我的最终工作代码:

没有这条线, client.setCallback(new SimpleMqttCallBack()); 我无法打印消息。不知道为什么?

package com.mapr.demo.mqtt.simple;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class Subscriber {

//    private static final String CONNECTION_URL = "tcp://localhost:1883";
    private static final String CONNECTION_URL = "tcp://192.168.1.102:1883";
    private static final String SUBSCRIPTION = "Area1/#";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "top-secret";

    public static void main(String[] args) throws MqttException {

    System.out.println("== START SUBSCRIBER ==");

    MqttClient client = new MqttClient(CONNECTION_URL, MqttClient.generateClientId());

    MqttConnectOptions connOpts = setUpConnectionOptions(USERNAME, PASSWORD);

// This callback is required to receive the message
    client.setCallback(new SimpleMqttCallBack());
    client.connect(connOpts);

    client.subscribe(SUBSCRIPTION);
    }

    public void messageArrived(String topic, MqttMessage message) throws MqttException {
    System.out.println(String.format("[%s] %s", topic, new String(message.getPayload())));
    System.out.println("\tMessage published on topic 'Area1'");
    }

    private static MqttConnectOptions setUpConnectionOptions(String username, String password) {
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setCleanSession(true);
    connOpts.setUserName(username);
    connOpts.setPassword(password.toCharArray());
    return connOpts;
    }

}
于 2018-08-01T12:23:48.420 回答