.Net 示例
using System;
using System.Threading;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
class MqttClientExample
{
private MqttClient client;
private string broker;
private int port;
private string topic;
private string clientId;
private string username;
private string password;
private byte qos;
private bool isReconnecting = false;
public MqttClientExample(string broker, int port, string topic, string clientId, string username, string password, byte qos)
{
this.broker = broker;
this.port = port;
this.topic = topic;
this.clientId = clientId;
this.username = username;
this.password = password;
this.qos = qos;
}
public void Connect()
{
try
{
client = new MqttClient(broker, port, false, null, null, MqttSslProtocols.None);
client.ConnectionClosed += Client_ConnectionClosed;
client.Connect(clientId, username, password);
if (client.IsConnected)
{
Console.WriteLine("Successfully connected to the MQTT server.");
client.Subscribe(new string[] { topic }, new byte[] { qos });
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
Console.WriteLine("Subscribed to topic: " + topic);
Console.WriteLine("Waiting for messages...");
}
else
{
Console.WriteLine("Failed to connect to the MQTT server.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error occurred while connecting: " + ex.Message);
TryReconnect();
}
}
public void Disconnect()
{
if (client != null && client.IsConnected)
{
try
{
client.Unsubscribe(new string[] { topic });
client.Disconnect();
Console.WriteLine("Disconnected from the MQTT server.");
}
catch (Exception ex)
{
Console.WriteLine("Error occurred while disconnecting: " + ex.Message);
}
}
}
private void TryReconnect()
{
if (isReconnecting) return;
isReconnecting = true;
new Thread(() =>
{
while (!client.IsConnected)
{
try
{
Console.WriteLine("Trying to reconnect to the MQTT server...");
client.Connect(clientId, username, password);
if (client.IsConnected)
{
Console.WriteLine("Reconnected successfully.");
client.Subscribe(new string[] { topic }, new byte[] { qos });
isReconnecting = false;
break;
}
}
catch (Exception ex)
{
Console.WriteLine("Reconnection failed: " + ex.Message);
}
Thread.Sleep(5000);
}
}).Start();
}
private void Client_ConnectionClosed(object sender, EventArgs e)
{
Console.WriteLine("The connection to the MQTT server has been closed.");
TryReconnect();
}
private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
string message = System.Text.Encoding.UTF8.GetString(e.Message);
Console.WriteLine("Received message - Topic: " + e.Topic + ", Message content: " + message);
}
}
class Program
{
static void Main()
{
string broker = "mqtt.assetscontrols.com";
int port = 1883;
string topic = "upload/ {accessKey}/+/#";
string clientId = "39440B3BD98045AC99EC2CF93EB15461_consumer01";
string username = "XXXXX";
string password = "XXXXX";
byte qos = 1;
MqttClientExample mqttClient = new MqttClientExample(broker, port, topic, clientId, username, password, qos);
mqttClient.Connect();
Console.WriteLine("Press any key to disconnect...");
Console.ReadKey();
mqttClient.Disconnect();
}
}