测试环境为centOS 7
#安装libwebsockets
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/l/libwebsockets-1.7.5-1.el7.x86_64.rpm
rpm -ivh libwebsockets-1.7.5-1.el7.x86_64.rpm
#安装mosquitto
wget https://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/home:oojah:mqtt.repo
mv home\:oojah\:mqtt.repo /etc/yum.repos.d/mosquitto.repo
yum install mosquitto mosquitto-clients
cd /etc/mosquitto/
cp mosquitto.conf.example conf.d/mosquitto.conf
vim conf.d/mosquitto.conf
# 修改内容为: allow_anonymous false
#password_file /etc/mosquitto/pwfile
cp pwfile.example pwfile
#创建admin用户
mosquitto_passwd -c /etc/mosquitto/pwfile admin
#使用【mosquitto -c /etc/mosquitto/mosquitto.conf -d 】可以后台运行,第一次启用不要加-d,这样如果运行出错可以立即看到反馈
mosquitto -c /etc/mosquitto/mosquitto.conf
#测试
mosquitto_pub -t test -u admin -P password -m "ok"
mosquitto_sub -t test -u admin -P password
# 如果sub收到ok说明测试成功
Publisher
package server;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class Publisher {
/**
* 代理服务器ip地址
*/
// public static final String MQTT_BROKER_HOST = "tcp://192.168.1.236:61613"; //apollo
public static final String MQTT_BROKER_HOST = "tcp://192.168.1.236:1883";
/**
* 订阅标识
*/
public static final String MQTT_TOPIC = "testTopic";
/**
* 客户端唯一标识
*/
public static final String MQTT_CLIENT_ID = "publisher01";
private static String userName = "admin";
private static String password = "password";
private static MqttTopic topic;
private static MqttClient client;
public static void main(String... args) {
// 推送消息
try {
client = new MqttClient(MQTT_BROKER_HOST, MQTT_CLIENT_ID, new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setUserName(userName);
options.setPassword(password.toCharArray());
options.setConnectionTimeout(10);
options.setKeepAliveInterval(20);
topic = client.getTopic(MQTT_TOPIC);
client.connect(options);
client.setCallback(new MqttCallback() {
@Override public void connectionLost(Throwable throwable) {
System.out.println("连接断开,正在重连......");
try {
client.connect(options);
}catch (Exception exception){
System.out.println(exception.getMessage());
}
}
@Override public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
}
@Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
int i=0;
while (true) {
MqttMessage message = new MqttMessage();
//0 最多一次传送 (只负责传送,发送过后就不管数据的传送情况)
//1 至少一次传送 (确认数据交付)
//2 正好一次传送 (保证数据交付成功)
message.setQos(2);
message.setRetained(true);
message.setPayload((""+i++).getBytes());
MqttDeliveryToken token = topic.publish(message);
token.waitForCompletion();
System.out.println(i);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Subscriber
package client;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class Subscriber {
/**
* 代理服务器ip地址
*/
// public static final String MQTT_BROKER_HOST = "tcp://192.168.1.236:61613";
public static final String MQTT_BROKER_HOST = "tcp://192.168.1.236:1883";
/**
* 客户端唯一标识
*/
public static final String MQTT_CLIENT_ID = "subscriber01";
/**
* 订阅标识
*/
public static final String MQTT_TOPIC = "testTopic";
/**
*
*/
public static final String USERNAME = "admin";
/**
* 密码
*/
public static final String PASSWORD = "password";
private volatile static MqttClient mqttClient;
private static MqttConnectOptions options;
public static void main(String... args) {
try {
// host为主机名,clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,
// MemoryPersistence设置clientid的保存形式,默认为以内存保存
mqttClient = new MqttClient(MQTT_BROKER_HOST, MQTT_CLIENT_ID, new MemoryPersistence());
// 配置参数信息
options = new MqttConnectOptions();
// 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,
// 这里设置为true表示每次连接到服务器都以新的身份连接
options.setCleanSession(false);
// 设置用户名
options.setUserName(USERNAME);
// 设置密码
options.setPassword(PASSWORD.toCharArray());
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
options.setKeepAliveInterval(20);
// 连接
mqttClient.connect(options);
// 订阅
mqttClient.subscribe(MQTT_TOPIC);
// 设置回调
mqttClient.setCallback(new MqttCallback() {
@Override public void connectionLost(Throwable throwable) {
// 连接丢失后,一般在这里面进行重连
System.out.println("连接断开,正在重连......");
try {
mqttClient.connect(options);
}catch (Exception exception){
exception.printStackTrace();
}
}
@Override public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
System.out.println("toppic:"+s+"\t message:\t"+ mqttMessage.toString());
}
@Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}