//异步关闭 public void CloseClient() { try { mqttClient.DisconnectAsync(); } catch (Exception) { throw; } }
private static async Task DisConnectMqttServerAsync() { try { await mqttClient.DisconnectAsync(); } catch (Exception ex) { LogHelper.Log(ex.ToString()); } }
public override void Dispose() { if (_mqttClient != null) { if (_mqttClient.IsConnected) { var task = new Task(() => { _mqttClient.DisconnectAsync(); }); task.RunSynchronously(); } _mqttClient.Dispose(); } }
public async void DisconnectAsyncCallsSocketSendMessageAsyncWithParams() { var socketMock = new Mock <ISocketAdapter>(); var loggerMock = new Mock <ILogger>(); var client = new MqttClient(socketMock.Object, loggerMock.Object, SocketEncryption.None); await client.DisconnectAsync(); socketMock.Verify(socket => socket.WriteAsync( It.Is <SocketEventArgs>(a => a != null && a.MessageToSend.MessageType == MessageType.Disconnect)), Times.Once()); }
/// <summary> /// Disconnects from the MQTT server. /// </summary> public MqttClientHelper Disconnect() { if (mqttClient != null) { if (mqttClient.IsConnected) { mqttClient.DisconnectAsync(); } mqttClient.Dispose(); mqttClient = null; } return(this); }
protected override async Task ClientStop() { try { if (mqttClient == null) { return; } await mqttClient.DisconnectAsync(); mqttClient = null; } catch (Exception ex) { Console.WriteLine($"客户端尝试断开Server出错.>{ex.Message}"); } }
public void DisConnect() { if (this.mqttClient != null) { try { Task task = mqttClient.DisconnectAsync(); this.IsConnected = false; task.Wait(5000); this.RemoveEvents(); mqttClient.Dispose(); mqttClient = null; } catch { } } }
private async Task ClientStop() { try { if (mqttClient == null) { return; } await mqttClient.DisconnectAsync(); mqttClient = null; } catch (Exception ex) { lbxMonitor.BeginInvoke(_updateMonitorAction, Logger.TraceLog(Logger.Level.Fatal, $"客户端尝试断开Server出错.>{ex.Message}")); } }
private async Task Disconnect() { if (_Client != null) { _Client.Reconnect = false; await _Client.DisconnectAsync(); _Client.Dispose(); _Client = null; "关闭连接".SpeechTip(); } gbSetting.Enabled = true; gbSubscribe.Enabled = false; gbSend.Enabled = false; btnConnect.Text = "连接"; }
public void Stop() { //移除守护定时器 _daemonTimer?.Dispose(); _daemonTimer = null; _publishHelper.Stop(); _publishHelper.OnPublishMessage -= OnPublishMessage; try { //退出时,断开连接 _mqttClient?.DisconnectAsync(); _mqttClient?.Dispose(); _mqttClient = null; } catch (Exception e) { OnErrorMessage?.Invoke(e.Message); } }
public async Task DisposeAsync() { if (appContext != null) { await appContext .DisposeAsync() .ConfigureAwait(false); } await MqttClient .DisconnectAsync() .ConfigureAwait(false); if (mqttServer != null) { await mqttServer .StopAsync() .ConfigureAwait(false); } LogoHardwareMock?.Dispose(); }
public static async Task SendToServer(string topic, string payload) { int connectTryCount = 0; while (connectTryCount < 2 && !s_client.IsConnected) { if (!_loggedNotConnected) { Dbg.Write("MQTTPublish - Client NotConnected"); _loggedNotConnected = true; } await Connect().ConfigureAwait(false); connectTryCount++; if (!s_client.IsConnected) { Task.Delay(1000 * 2); } } if (s_client.IsConnected == false) { if (!_loggedNotConnected) { Dbg.Write("MQTTPublish - Client NotConnected"); _loggedNotConnected = true; } } else { _loggedNotConnected = false; _loggedError = false; var message = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(payload) .WithExactlyOnceQoS() .WithRetainFlag() .Build(); try { await s_client.PublishAsync(message).ConfigureAwait(false); Dbg.Write("MQTT Message Sent"); } catch (Exception ex) { Dbg.Write("MQTTPublish - Error publishing message: " + ex.Message); try { if (s_client.IsConnected) { await s_client.DisconnectAsync(); } } catch (Exception) { Dbg.Trace("MQTTPublish - Exception closing connection after publish error - client still connected"); // Expected if the publish failed (but, probably not if the client still thinks it is connected). } } } }
public void Dispose() { MqttClient.DisconnectAsync().Wait(); MqttClient.Dispose(); }
private void button_Disconnect_Click(object sender, EventArgs e) { isReconnect = false; Task.Run(async() => { await mqttClient.DisconnectAsync(); }); }
public async Task DisconnectAsync() { await _mqttClient.DisconnectAsync(); }