old mode 100644
new mode 100755
| | |
| | | } |
| | | } |
| | | |
| | | #region variable |
| | | #region variable |
| | | /// <summary> |
| | | /// 远程连接成功时的时间 |
| | | /// </summary> |
| | |
| | | /// <summary> |
| | | /// 远程MqttClient |
| | | /// </summary> |
| | | public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | static bool remoteIsConnected; |
| | | public static IMqttClient RemoteMqttClient= new MqttFactory().CreateMqttClient(); |
| | | static bool remoteIsConnected; |
| | | private int IsLogin = 0; |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | static Action actionTemp; |
| | |
| | | .WithTcpServer(domain, int.Parse(port))
|
| | | .WithCredentials(connEmqUserName, connEmqPwd)
|
| | | .WithKeepAlivePeriod(TimeSpan.FromSeconds(20))
|
| | | .WithCleanSession() |
| | | //.WithCommunicationTimeout(TimeSpan.FromSeconds(10)) |
| | | .WithCleanSession()
|
| | | //.WithCommunicationTimeout(TimeSpan.FromSeconds(10))
|
| | | .Build();
|
| | | await DisConnectRemoteMqttClient("StartRemoteMqtt");
|
| | | await RemoteMqttClient.ConnectAsync(options, CancellationToken.None);
|
| | |
| | | localIsConnected = false;
|
| | | DebugPrintLog($"Local主动断开_{s}");
|
| | | //await localMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
|
| | | await localMqttClient.DisconnectAsync(); |
| | | await localMqttClient.DisconnectAsync(); |
| | | }
|
| | | } |
| | | catch (Exception ex) |
| | |
| | | /// <summary>
|
| | | /// 断开远程Mqtt的链接
|
| | | /// </summary> |
| | | public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient(string s = "")
|
| | | public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient(string s="")
|
| | | {
|
| | | try
|
| | | {
|
| | |
| | | {
|
| | | await localMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topic, Payload = message, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, Retain = retain });
|
| | | }
|
| | | catch (Exception e)
|
| | | catch(Exception e)
|
| | | { |
| | | DebugPrintLog($"Local主动断开_{e.Message}");
|
| | | await DisConnectLocalMqttClient(e.Message); |
| | |
| | | /// <param name="topic">Topic.</param> |
| | | /// <param name="message">Message.</param> |
| | | /// <param name="e">E.</param> |
| | | static void ReceiveMessage(string topic, string message, byte[] payload) |
| | | static void ReceiveMessage(string topic, string message, byte []payload) |
| | | { |
| | | try |
| | | { |