我可以使用此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");
}
}
答案 0 :(得分:4)
您可以使用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;
}
}
答案 1 :(得分: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;
}
}