1、搭建JAVA开发环境:JDK、JRE 环境搭建参考:https://www.cnblogs.com/smivico/p/6256313.html
2、网上下载apache,我这里用的是apache-apollo-1.7.1-windows,下载链接:http://activemq.apache.org/apollo/download.html
3、解压下载的apache:命令行 win+r 进入:D:\m_software\apache-apollo-1.7.1-windows-distro\apache-apollo-1.7.1\bin文件夹中,作如下操作:
输入:apollo.cmd create test 创建虚拟test服务器主机,在该文件夹下会生成一个test目录
4、进入 D:\m_software\apache-apollo-1.7.1-windows-distro\apache-apollo-1.7.1\bin\test\bin文件,开启服务器。
5、进入test目录下etc文件夹中,找到apollo.xml
<web_admin bind="http://127.0.0.1:61680"/>
<web_admin bind="https://127.0.0.1:61681"/>
打开浏览器:输入http://127.0.0.1:61680,任意一个都可以,即可进入管理的web界面,账户:admin ,密码:password。
6、测试连接,打开mqtt用户客户端,如下图所示:

7、查看服务器连接状态,如下图所示,已经连接上一个客户端:
8、测试一个订阅与发布:重新打开一个客户端作为管理客户端,负责发布:
此时,服务器显示有两个用户连接上了:
发布信息:
9、客户端实现
using MQTTnet;
using MQTTnet.Core;
using MQTTnet.Core.Client;
using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace mqtt_client
{
public partial class Form1 : Form
{
private MqttClient mqttClient = null;
public Form1()
{
InitializeComponent();
Task.Run(async () => { await ConnectMqttServerAsync(); });
}
private async Task ConnectMqttServerAsync()
{
if (mqttClient == null)
{
mqttClient = new MqttClientFactory().CreateMqttClient() as MqttClient;
mqttClient.ApplicationMessageReceived += MqttClient_ApplicationMessageReceived;
mqttClient.Connected += MqttClient_Connected;
mqttClient.Disconnected += MqttClient_Disconnected;
}
try
{
var options = new MqttClientTcpOptions
{
Server = "127.0.0.1",
Port=61613,
ClientId = Guid.NewGuid().ToString(),
UserName = "admin",
Password = "password",
CleanSession = true
};
await mqttClient.ConnectAsync(options);
}
catch (Exception ex)
{
Invoke((new Action(() =>
{
textBox2.AppendText($"连接到MQTT服务器失败!" + Environment.NewLine + ex.Message + Environment.NewLine);
})));
}
}
private void MqttClient_Connected(object sender, EventArgs e)
{
Invoke((new Action(() =>
{
textBox2.AppendText("已连接到MQTT服务器!" + Environment.NewLine);
})));
}
private void MqttClient_Disconnected(object sender, EventArgs e)
{
Invoke((new Action(() =>
{
textBox2.AppendText("已断开MQTT连接!" + Environment.NewLine);
})));
}
private void MqttClient_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
{
Invoke((new Action(() =>
{
textBox2.AppendText($">> {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}");
})));
}
private void button1_Click(object sender, EventArgs e)
{
string topic = textBox1.Text.Trim();
if (string.IsNullOrEmpty(topic))
{
MessageBox.Show("订阅主题不能为空!");
return;
}
if (!mqttClient.IsConnected)
{
MessageBox.Show("MQTT客户端尚未连接!");
return;
}
mqttClient.SubscribeAsync(new List<TopicFilter> {
new TopicFilter(topic, MqttQualityOfServiceLevel.AtMostOnce)
});
textBox2.AppendText($"已订阅[{topic}]主题" + Environment.NewLine);
textBox1.Enabled = false;
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
string topic = textBox4.Text.Trim();
if (string.IsNullOrEmpty(topic))
{
MessageBox.Show("发布主题不能为空!");
return;
}
string inputString = textBox3.Text.Trim();
var appMsg = new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes(inputString), MqttQualityOfServiceLevel.AtMostOnce, false);
mqttClient.PublishAsync(appMsg);
}
private void Form1_Load(object sender, EventArgs e)
{
Sunisoft.IrisSkin.SkinEngine skin = new Sunisoft.IrisSkin.SkinEngine
{
SkinFile = @"./RealOne.ssk"
};
}
}
}