MQTT 客戶端應用及常見問題(C#)
最近因為工作需要,需要使用C# 語言編寫一個通過MQTT協議 ,上傳資料到雲端的工具。因為之前沒有用過MQTT,所以 使用的時候遇到很多問題.下面將會把我遇到的問題一一解釋。
1.引用原始碼庫地址
https://github.com/eclipse/paho.mqtt.m2mqtt
2.說明
https://m2mqtt.wordpress.com/m2mqtt_doc/
3.使用後遇到的問題
當網路中斷後,MQTT 程式有時候不會自動重連。
解決方案 新增監控MQTT連線狀態
1.新增全域性靜態變數 uPLibrary.Networking.M2Mqtt.MQTTConfig.IsSocketRun;
class MQTTConfig{
public static bool IsSocketRun = false;
}
2.修改MqttClient 類 的Connect 方法,在連線成功後把IsSocketRun = true.
MQTTConfig.IsSocketRun = true;
/// <summary>
/// Connect to broker
/// </summary>
/// <param name="clientId">Client identifier</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="willRetain">Will retain flag</param>
/// <param name="willQosLevel">Will QOS level</param>
/// <param name="willFlag">Will flag</param>
/// <param name="willTopic">Will topic</param>
/// <param name="willMessage">Will message</param>
/// <param name="cleanSession">Clean sessione flag</param>
/// <param name="keepAlivePeriod">Keep alive period</param>
/// <returns>Return code of CONNACK message from broker</returns>
public byte Connect(string clientId,
string username,
string password,
bool willRetain,
byte willQosLevel,
bool willFlag,
string willTopic,
string willMessage,
bool cleanSession,
ushort keepAlivePeriod)
{
// create CONNECT message
MqttMsgConnect connect = new MqttMsgConnect(clientId,
username,
password,
willRetain,
willQosLevel,
willFlag,
willTopic,
willMessage,
cleanSession,
keepAlivePeriod,
(byte)this.ProtocolVersion);
try
{
// connect to the broker
this.channel.Connect();
}
catch (Exception ex)
{
throw new MqttConnectionException("Exception connecting to the broker", ex);
}
this.lastCommTime = 0;
this.isRunning = true;
MQTTConfig.IsSocketRun = true;
this.isConnectionClosing = false;
// start thread for receiving messages from broker
Fx.StartThread(this.ReceiveThread);
....
3.繼續修改 MqttClient .cs類中的Ping() 方法
/// <summary>
/// Execute ping to broker for keep alive
/// </summary>
/// <returns>PINGRESP message from broker</returns>
private MqttMsgPingResp Ping()
{
MqttMsgPingReq pingreq = new MqttMsgPingReq();
try
{
// broker must send PINGRESP within timeout equal to keep alive period
return (MqttMsgPingResp)this.SendReceive(pingreq, this.keepAlivePeriod);
}
catch (Exception e)
{
#if TRACE
MqttUtility.Trace.WriteLine(TraceLevel.Error, "Exception occurred: {0}", e.ToString());
#endif
MQTTConfig.IsSocketRun = false;
// client must close connection
this.OnConnectionClosing();
return null;
}
}
4.最後在我們程式集入口初始化程式的時候 新增執行緒呼叫 。當MQTT中斷後就會自動重連 ,另外提醒方法異常時一定要異常處理哦。
while (true)
{
LogWriter.DebugLog(string.Format("執行次數{0} IsSocketRun {1}", i, uPLibrary.Networking.M2Mqtt.MQTTConfig.IsSocketRun));
if (!uPLibrary.Networking.M2Mqtt.MQTTConfig.IsSocketRun)
{
程式執行到嗎。。。
}
System.Threading.Thread.Sleep(10000);
}
MQTT 訂閱
// create client instance
MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "/home/temperature" with QoS 2
client.Subscribe(new string[] { "/home/temperature" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
...
static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
// handle message received
}
MQTT 釋出
…
// create client instance
MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
string strValue = Convert.ToString(value);
// publish a message on "/home/temperature" topic with QoS 2
client.Publish("/home/temperature", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
...
相關文章
- C# MQTT客戶端C#MQQT客戶端
- mqtt 客戶端MQQT客戶端
- WebSphere客戶端遷移的常見問題解決二Web客戶端
- 客戶服務管理:CRM實現緊急應對客戶常見問題
- 客戶服務常見問題及解決方法介紹
- Python MQTT客戶端 paho-mqttPythonMQQT客戶端
- 快應用稽核常見問題
- MQTT X Web:線上的 MQTT 5.0 客戶端工具MQQTWeb客戶端
- MQTT客戶端JAVA程式碼----fusesource mqtt-clientMQQT客戶端Javaclient
- Android應用安全常見問題及解決方案Android
- Mqtt websocket javascript 客戶端例項MQQTWebJavaScript客戶端
- JAVA FTP客戶端問題JavaFTP客戶端
- 21_MQTT協議_客戶端和服務端MQQT協議客戶端服務端
- 子賬戶及STS臨時賬戶呼叫OSS的常見問題及排查
- C#實現組播源及客戶端C#客戶端
- 客戶端tnsping不通問題客戶端
- 【常見Web應用安全問題】—7、CRLFinjectionWeb
- 【常見Web應用安全問題】—12、URLredirectionWeb
- 常見問題及解決
- MQTT伺服器搭建服務端和客戶端MQQT伺服器服務端客戶端
- 使用測試客戶端「玩轉」MQTT 5.0客戶端MQQT
- ERP應用過程常見問題及解決建議(轉)
- 常見問題:文字框控制元件的訊息及應用 (轉)控制元件
- mysql客戶端連線的幾個常見錯誤MySql客戶端
- MySQL客戶端中文亂碼問題。MySql客戶端
- xfire 客戶端呼叫webservice的問題客戶端Web
- TSM客戶端的排程問題客戶端
- MQTT協議從服務端到客戶端詳解MQQT協議服務端客戶端
- Ceph常規操作及常見問題梳理
- 使用 WebSocket 客戶端連線 MQTT 伺服器Web客戶端MQQT伺服器
- Nacos - 客戶端心跳續約及客戶端總結客戶端
- Git常見問題及解決Git
- 常見問題及解決方案
- 移動端網路常見問題及優化對策優化
- 恆訊科技總結:“Tiktok直播專線”客戶的常見問題
- Anbox安卓apk應用安裝及使用說明和常見問題安卓APK
- 【常見Web應用安全問題】—4、DirectorytraversalWeb
- c# 獲取客戶端IPC#客戶端