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", ClientId = Guid.NewGuid().ToString().Substring(0, 5), UserName = "******", Password = "******", CleanSession = true }; await mqttClient.ConnectAsync(options); } catch (Exception ex) { Application.Current.Dispatcher.Invoke((new Action(() => { txtReceiveMessage.AppendText($"连接到MQTT服务器失败!" + Environment.NewLine + ex.Message + Environment.NewLine); }))); } }
public static 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 = "114.55.171.95", //Server = "127.0.0.1", ClientId = Guid.NewGuid().ToString().Substring(0, 5), UserName = "******", Password = "******", CleanSession = true }; await mqttClient.ConnectAsync(options); } catch (Exception ex) { Console.WriteLine($"连接到MQTT服务器失败!" + Environment.NewLine + ex.Message + Environment.NewLine); } //Publish("[email protected]:App", "空调,false,off"); }
/// <summary> /// /// </summary> private static void Work() { running = true; Console.WriteLine("Work >>Begin"); try { var factory = new MqttFactory(); mqttClient = factory.CreateMqttClient() as MqttClient; options = new MqttClientOptionsBuilder() .WithTcpServer(ServerUrl, Port) .WithCredentials(UserId, Password) .WithClientId(Guid.NewGuid().ToString().Substring(0, 5)) .Build(); mqttClient.ConnectAsync(options); mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(new Func <MqttClientConnectedEventArgs, Task>(Connected)); mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(new Func <MqttClientDisconnectedEventArgs, Task>(Disconnected)); mqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(new Action <MqttApplicationMessageReceivedEventArgs>(MqttApplicationMessageReceived)); while (runState) { System.Threading.Thread.Sleep(100); } } catch (Exception exp) { Console.WriteLine(exp); } Console.WriteLine("Work >>End"); running = false; runState = false; }
/// <summary> /// 发布 /// <paramref name="QoS"/> /// <para>0 - 最多一次</para> /// <para>1 - 至少一次</para> /// <para>2 - 仅一次</para> /// </summary> /// <param name="Topic">发布主题</param> /// <param name="Message">发布内容</param> /// <returns></returns> public static void Publish(string Topic, string Message) { try { if (mqttClient == null) { return; } if (mqttClient.IsConnected == false) { mqttClient.ConnectAsync(options); } if (mqttClient.IsConnected == false) { Console.WriteLine("Publish >>Connected Failed! "); return; } Console.WriteLine("Publish >>Topic: " + Topic + "; QoS: " + QualityOfServiceLevel + "; Retained: " + Retained + ";"); Console.WriteLine("Publish >>Message: " + Message); MqttApplicationMessageBuilder mamb = new MqttApplicationMessageBuilder() .WithTopic(Topic) .WithPayload(Message).WithRetainFlag(Retained); if (QualityOfServiceLevel == 0) { mamb = mamb.WithAtMostOnceQoS(); } else if (QualityOfServiceLevel == 1) { mamb = mamb.WithAtLeastOnceQoS(); } else if (QualityOfServiceLevel == 2) { mamb = mamb.WithExactlyOnceQoS(); } mqttClient.PublishAsync(mamb.Build()); } catch (Exception exp) { Console.WriteLine("Publish >>" + exp.Message); } }