赞
踩
安装步骤
1.引入库
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
2.安装
sudo apt-get update
sudo apt-get install mosquitto
3.安装客户端
sudo apt-get install mosquitto-clients
添加账户及密码
sudo mosquitto_passwd -c /etc/mosquitto/pwfile.txt 用户名
回车后连续输入2次用户密码即可
3.启动mosquitto
sudo service mosquitto start
4.查看mosquitto运行状态
sudo service mosquitto status
运行测试
1.mosquitto-clients 运行测试
订阅主题(终端1)
mosquitto_sub -h localhost -t "lai001/#" -u 用户名 -P 密码 -i “client1”
发布主题(终端2)
mosquitto_pub -h localhost -t "lai001/testTopic" -u 用户名 -P 密码 -m "Hello MQTT from mosquitto-clients"
查看运行状态
systemctl status mosquitto
重启服务器程序
查看运行进程号:ps -aux | grep mosquitto
.emqtt下载地址:http://emqtt.com/downloads,找到自己要下载的版本信息,注意开发版、稳定版;
2.下载后放到硬盘根目录,进入下载路径目录,cmd进入dos窗口,如下图:
3.输入.\bin\emqttd consloe,之后弹出启动状态页面,代表启动成功;
4.登录:地址 :http://localhost:18083/
用户名为:admin 密码为:public
登录上去就可以看服务器的运行状态了。
测试
依赖
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.0.2</version>
</dependency>
Server
package com.mymqtt.myemqtt;
import java.util.Scanner;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
public class Server {
public static void main(String[] args) throws Exception {
String host = "tcp://127.0.0.1:1883";
String topic = "hello";
String clientId = "server";// clientId不能重复
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
MqttClient client = new MqttClient(host, clientId);
client.connect(options);
MqttMessage message = new MqttMessage();
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要发送的内容:");
while (true) {
String line = scanner.nextLine();
message.setPayload(line.getBytes());
client.publish(topic, message);
}
}
}
Client
package com.mymqtt.myemqtt;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
public class Client {
public static void main(String[] args) throws Exception {
String host = "tcp://127.0.0.1:1883";
String topic = "hello";
String clientId = "12345";// clientId不能重复
// 1.设置mqtt连接属性
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
// 2.实例化mqtt客户端
MqttClient client = new MqttClient(host, clientId);
// 3.连接
client.connect(options);
client.setCallback(new PushCallback());
while (true) {
client.subscribe(topic, 2);
}
// client.disconnect();
}
}
package com.mymqtt.myemqtt;
import java.util.Date;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PushCallback implements MqttCallback {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public void connectionLost(Throwable cause) {
// 连接丢失后,一般在这里面进行重连
System.out.println("连接断开,可以做重连");
logger.info("掉线时间:{}", new Date());
}
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("deliveryComplete---------" + token.isComplete());
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
// subscribe后得到的消息会执行到这里面
// System.out.println(message);
System.out.println("接收消息主题 : " + topic);
System.out.println("接收消息Qos : " + message.getQos());
System.out.println("接收消息内容 : " + new String(message.getPayload()));
}
}
结果:
参考资料:http://emqtt.com/
mosquitto_pub
d 打印debug信息
-f 将指定文件的内容作为发送消息的内容
-h 指定要连接的域名 默认为localhost
-i 指定要给哪个clientId的用户发送消息
-I 指定给哪个clientId前缀的用户发送消息
-m 消息内容
-n 发送一个空(null)消息
-p 连接端口号
-q 指定QoS的值(0,1,2)
-t 指定topic
-u 指定broker访问用户
-P 指定broker访问密码
-V 指定MQTT协议版本
--will-payload 指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与--will-topic一起使用
--will-qos Will的QoS值。该参数需要与--will-topic一起使用
--will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与--will-topic一起使用
--will-topic 用户发送Will消息的topic
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。