MQTT协议接收/发送消息
由于项目需要采用MQTT协议收/发送消息,首先购买了一个阿里云服务器,在其上安装 nginx服务器,安装Tomcat服务器,安装MQtt服务器。
说明:要下载一个 M2Mqtt.dll 文件,将这个dll文件引用到项目中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace mqtt_test
{
public partial class Form1 : Form
{
string clientId = "testclient_01";//测试客户端
MqttClient client = null;
public Form1()
{
InitializeComponent();
//client = new MqttClient("tcp://11x.xx.xx.xxx:8080");
client = new MqttClient(IPAddress.Parse("11x.xx.xx.xxx"), 8080, false, null);
client.Connect(clientId, "admin", "password");
}
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show(textBox1.Text);
client.Publish("/device", Encoding.UTF8.GetBytes(textBox1.Text), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
}
}
}
这样是没有加入认证文件的,