public async Task DisconnectAsync() { try { await _client.DisconnectAsync(); } catch (Exception ex) { _logger.LogError("DisconnectAsync error:\n" + ex.ToString()); } }
public async Task DisconnectAsync() { if (client == null) { return; } if (!client.IsConnected) { return; } await client.DisconnectAsync(); }
private void ConnectButton_Click(object sender, RoutedEventArgs e) { if (mqttClient.IsConnected) { Task.Run(async() => { try { await mqttClient.DisconnectAsync(); } catch { } }); } else { MqttIsConnected = true; var temp = new MqttClientOptionsBuilder() .WithClientId(Tools.Global.setting.mqttClientID) .WithTcpServer(Tools.Global.setting.mqttServer, Tools.Global.setting.mqttPort) .WithCredentials(Tools.Global.setting.mqttUser, Tools.Global.setting.mqttPassword) .WithKeepAlivePeriod(new TimeSpan(0, 0, Tools.Global.setting.mqttKeepAlive)); if (Tools.Global.setting.mqttTLS) { temp.WithTls(); } if (Tools.Global.setting.mqttCleanSession) { temp.WithCleanSession(); } var options = temp.Build(); Task.Run(async() => { try { await mqttClient.ConnectAsync(options, CancellationToken.None); } catch { } }); } }
public Task DisconnectAsync(CancellationToken cancellationToken) { client.UseConnectedHandler((Action <MqttClientConnectedEventArgs>)null); client.UseDisconnectedHandler((Action <MqttClientDisconnectedEventArgs>)null); return(client.DisconnectAsync(new MqttClientDisconnectOptions(), cancellationToken)); }